|
@@ -46,42 +46,61 @@ class CompanyGoodLogic extends BaseLogic
|
|
|
public static function add(array $data = []): Json
|
|
|
{
|
|
|
|
|
|
- $rs = GroupModel::field('id')
|
|
|
- ->where(['id' => $data['group_id'], 'is_del' => CommonModel::$del_normal])
|
|
|
- ->findOrEmpty()
|
|
|
- ->isEmpty();
|
|
|
- if ($rs) return json_show(CommonModel::$error_param, '该分组不存在');
|
|
|
-
|
|
|
- $data['good_id'] = GoodModel::where('is_del', CommonModel::$del_normal)
|
|
|
- ->whereIn('id', $data['good_id'])
|
|
|
- ->column('id');
|
|
|
- if (empty($data['good_id'])) return json_show(CommonModel::$error_param, '该商品不存在');
|
|
|
-
|
|
|
- $rs = CompanyGoodModel::field('a.id,a.good_id,b.good_name,b.good_code')
|
|
|
- ->alias('a')
|
|
|
- ->leftJoin('good b', 'b.id=a.good_id AND b.is_del=' . CommonModel::$del_normal)
|
|
|
- ->where(['a.is_del' => CommonModel::$del_normal, 'a.group_id' => $data['group_id']])
|
|
|
- ->whereIn('a.good_id', $data['good_id'])
|
|
|
- ->findOrEmpty()
|
|
|
- ->toArray();
|
|
|
- if (empty($rs)) return json_show(CommonModel::$error_layer, "【{$rs['good_code']}】【{$rs['good_name']}】该商品重复添加");
|
|
|
-
|
|
|
- $res = CompanyGoodModel::create((array_merge($data, [
|
|
|
- 'code' => make_no('CG'),
|
|
|
- 'is_del' => CommonModel::$del_normal,
|
|
|
- 'status' => CommonModel::$status_normal,
|
|
|
- 'is_top' => CommonModel::$top_no,
|
|
|
- 'weight' => 0,
|
|
|
- 'creater' => self::$uname,
|
|
|
- 'createrid' => self::$uid,
|
|
|
- 'addtime' => date('Y-m-d H:i:s'),
|
|
|
- 'updater' => self::$uname,
|
|
|
- 'updaterid' => self::$uid,
|
|
|
- 'updatetime' => date('Y-m-d H:i:s'),
|
|
|
- ])));
|
|
|
-
|
|
|
- return $res ? json_show(CommonModel::$success, '添加公司商品成功') : json_show(CommonModel::$success, '添加公司商品失败');
|
|
|
-
|
|
|
+ Db::startTrans();
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ $rs = GroupModel::field('id')
|
|
|
+ ->where(['id' => $data['group_id'], 'is_del' => CommonModel::$del_normal])
|
|
|
+ ->findOrEmpty()
|
|
|
+ ->isEmpty();
|
|
|
+ if ($rs) throw new Exception('该分组不存在');
|
|
|
+
|
|
|
+ $data['good_id'] = GoodModel::where('is_del', CommonModel::$del_normal)
|
|
|
+ ->whereIn('id', $data['good_id'])
|
|
|
+ ->column('id');
|
|
|
+ if (empty($data['good_id'])) throw new Exception('该商品不存在');
|
|
|
+
|
|
|
+ $rs = CompanyGoodModel::field('a.id,a.good_id,b.good_name,b.good_code')
|
|
|
+ ->alias('a')
|
|
|
+ ->leftJoin('good b', 'b.id=a.good_id AND b.is_del=' . CommonModel::$del_normal)
|
|
|
+ ->where(['a.is_del' => CommonModel::$del_normal, 'a.group_id' => $data['group_id']])
|
|
|
+ ->whereIn('a.good_id', $data['good_id'])
|
|
|
+ ->findOrEmpty()
|
|
|
+ ->toArray();
|
|
|
+ if (!empty($rs)) throw new Exception("【{$rs['good_code']}】【{$rs['good_name']}】该商品重复添加");
|
|
|
+
|
|
|
+ $insert = [];
|
|
|
+ $date = date('Y-m-d H:i:s');
|
|
|
+ foreach ($data['good_id'] as $good_id) {
|
|
|
+ $insert[] = [
|
|
|
+ 'group_id' => $data['group_id'],
|
|
|
+ 'code' => make_no('CG'),
|
|
|
+ 'good_id' => $good_id,
|
|
|
+ 'is_del' => CommonModel::$del_normal,
|
|
|
+ 'status' => CommonModel::$status_normal,
|
|
|
+ 'is_top' => CommonModel::$top_no,
|
|
|
+ 'weight' => 0,
|
|
|
+ 'creater' => self::$uname,
|
|
|
+ 'createrid' => self::$uid,
|
|
|
+ 'addtime' => $date,
|
|
|
+ 'updater' => self::$uname,
|
|
|
+ 'updaterid' => self::$uid,
|
|
|
+ 'updatetime' => $date,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ $res = Db::name('company_good')->insertAll($insert);
|
|
|
+
|
|
|
+ Db::commit();
|
|
|
+
|
|
|
+ if ($res) return json_show(CommonModel::$success, '添加公司商品成功');
|
|
|
+ else throw new Exception();
|
|
|
+
|
|
|
+ } catch (Exception $exception) {
|
|
|
+ Db::rollback();
|
|
|
+ return json_show(CommonModel::$error_param, '添加公司商品失败,' . $exception->getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//获取详情
|