123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- namespace app\admin\logic;
- use app\model\CommonModel;
- use app\model\GoodGroupModel;
- use think\Exception;
- use think\exception\ValidateException;
- use think\facade\Db;
- use think\response\Json;
- class GoodGroupLogic extends BaseLogic
- {
- //获取商品分组列表
- public static function list(array $data = []): Json
- {
- $db = GoodGroupModel::alias('a')
- ->leftJoin('company b', 'b.id=a.company_id')
- ->leftJoin('card c', 'c.id=a.card_id')
- ->where('a.is_del', CommonModel::$del_normal);
- if ($data['status'] != '') $db->where('a.status', $data['status']);
- if ($data['company_id'] != '') $db->where('a.company_id', $data['company_id']);
- if ($data['card_id'] != '') $db->where('a.card_id', $data['card_id']);
- $count = $db->count('a.id');
- $list = $db->field('a.id,b.title company_title,c.title card_title,a.num,a.status,a.addtime')
- ->page($data['page'], $data['size'])
- ->order(['a.id' => 'desc'])
- ->select()
- ->toArray();
- return json_show(CommonModel::$success, '获取商品分组列表成功', ['count' => $count, 'list' => $list]);
- }
- //添加商品分组
- public static function add(array $data = []): Json
- {
- Db::startTrans();
- try {
- $rs = GoodGroupModel::field('id')
- ->where([
- 'is_del' => CommonModel::$del_normal,
- 'company_id' => $data['company_id'],
- 'card_id' => $data['card_id']
- ])
- ->findOrEmpty()
- ->isEmpty();
- if (!$rs) throw new Exception('该分组已存在');
- $date = date('Y-m-d H:i:s');
- $good_group_id = Db::name('good_group')
- ->strict(false)
- ->insertGetId(array_merge($data, [
- 'num' => count($data['good_list']),
- 'status' => CommonModel::$status_normal,
- 'is_del' => CommonModel::$del_normal,
- 'createrid' => self::$uid,
- 'creater' => self::$uname,
- 'addtime' => $date,
- 'updaterid' => self::$uid,
- 'updater' => self::$uname,
- 'updatetime' => $date,
- ]));
- $da = [];
- foreach ($data['good_list'] as $item) {
- $da[] = [
- 'good_group_id' => $good_group_id,
- 'good_id' => $item['good_id'],
- 'weight' => $item['weight'],
- 'is_del' => CommonModel::$del_normal,
- 'addtime' => $date,
- 'updatetime' => $date,
- ];
- }
- Db::name('good_group_item')->insertAll($da);
- Db::commit();
- return json_show(CommonModel::$success, '添加商品分组成功');
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(CommonModel::$error_param, '添加商品分组失败,' . $exception->getMessage());
- }
- }
- //获取商品分组详情
- public static function read(int $id = 0): Json
- {
- $res = GoodGroupModel::field(true)
- ->where(['id' => $id, 'is_del' => CommonModel::$del_normal])
- ->findOrEmpty()
- ->toArray();
- if (empty($res)) throw new ValidateException('该商品分组为空');
- $res['child'] = Db::name('good_group_item')
- ->alias('a')
- ->field('a.id,a.good_id,a.weight,b.good_name,b.good_code')
- ->leftJoin('good b','b.id=a.good_id AND b.is_del='.CommonModel::$del_normal)
- ->where(['a.is_del' => CommonModel::$del_normal, 'a.good_group_id' => $id])
- ->order(['a.weight' => 'desc', 'a.id' => 'desc'])
- ->select()
- ->toArray();
- return json_show(CommonModel::$success, '获取商品分组详情成功', $res);
- }
- //编辑商品分组
- public static function edit(array $data = []): Json
- {
- Db::startTrans();
- try {
- $rs = GoodGroupModel::field('id,company_id,card_id')
- ->where([
- 'is_del' => CommonModel::$del_normal,
- 'id' => $data['id']
- ])
- ->findOrEmpty();
- if ($rs->isEmpty()) throw new Exception('该商品分组不存在');
- if (($rs->company_id != $data['company_id']) || ($rs->card_id != $data['card_id'])) {
- $temp = GoodGroupModel::field('id')
- ->where([
- 'is_del' => CommonModel::$del_normal,
- 'company_id' => $data['company_id'],
- 'card_id' => $data['card_id']
- ])
- ->findOrEmpty()
- ->isEmpty();
- if (!$temp) throw new Exception('该分组已存在');
- }
- $date = date('Y-m-d H:i:s');
- GoodGroupModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
- ->strict(false)
- ->save(array_merge($data, ['updatetime' => $date,'updaterid'=>self::$uid,'updater'=>self::$uid]));
- $del = $ins = [];
- foreach ($data['good_list'] as $item) {
- if (isset($item['id']) && $item['id'] != 0) {
- if ($item['is_del'] == CommonModel::$del_deleted) $del[] = $item['id'];
- else Db::name('good_group_item')->where('is_del', CommonModel::$del_normal)->where('id', $item['id'])->save(array_merge($item, ['updatetime' => $date]));
- } else $ins[] = [
- 'good_group_id' => $data['id'],
- 'good_id' => $item['good_id'],
- 'weight' => $item['weight'],
- 'is_del' => CommonModel::$del_normal,
- 'addtime' => $date,
- 'updatetime' => $date,
- ];
- }
- if ($del) Db::name('good_group_item')
- ->whereIn('id', $del)
- ->where('is_del', CommonModel::$del_normal)
- ->update([
- 'is_del' => CommonModel::$del_deleted,
- 'updatetime' => $date,
- ]);
- if ($ins) Db::name('good_group_item')->insertAll($ins);
- Db::commit();
- return json_show(CommonModel::$success, '编辑商品分组成功');
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(CommonModel::$error_param, '编辑商品分组失败,' . $exception->getMessage());
- }
- }
- //商品分组启禁用
- public static function status(array $data = []): Json
- {
- $res = GoodGroupModel::where('id', $data['id'])
- ->where('status', '<>', $data['status'])
- ->save([
- 'status' => $data['status'],
- 'updatetime' => date('Y-m-d H:i:s'),
- ]);
- return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$success, '操作失败');
- }
- //删除商品分组
- public static function delete(int $id = 0): Json
- {
- $res = GoodGroupModel::where('id', $id)
- ->where('is_del', CommonModel::$del_normal)
- ->save([
- 'is_del' => CommonModel::$del_deleted,
- 'updatetime' => date('Y-m-d H:i:s'),
- ]);
- return $res ? json_show(CommonModel::$success, '删除成功') : json_show(CommonModel::$error_param, '删除失败');
- }
- }
|