123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace app\admin\logic;
- use app\model\CommonModel;
- use app\model\GoodGroupModel;
- use app\model\GoodModel;
- use think\Exception;
- use think\exception\ValidateException;
- use think\facade\Db;
- use think\response\Json;
- class GoodGroupItemLogic extends BaseLogic
- {
- //列表
- public static function list(array $data = []): Json
- {
- $db = Db::name('good_group_item')
- ->alias('a')
- ->leftJoin('good b', 'b.id=a.good_id AND b.is_del=' . CommonModel::$del_normal)
- ->where(['a.is_del' => CommonModel::$del_normal, 'good_group_id' => $data['good_group_id']]);
- if ($data['good_title'] != '') $db->whereLike('b.good_name', '%' . $data['good_title'] . '%');
- $count = $db->count('a.id');
- $list = $db->field('a.id,b.good_code,b.good_name,a.weight')
- ->page($data['page'], $data['size'])
- ->order(['a.weight' => 'desc', '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(['id' => $data['good_group_id'], 'is_del' => CommonModel::$del_normal])
- ->findOrEmpty()
- ->isEmpty();
- if ($rs) throw new Exception('该商品分组不存在');
- $rs = GoodModel::field('id')
- ->where(['id' => $data['good_id'], 'is_del' => CommonModel::$del_normal])
- ->findOrEmpty()
- ->isEmpty();
- if ($rs) throw new Exception('该商品不存在');
- $rs = Db::name('good_group_item')
- ->field('id')
- ->where([
- 'is_del' => CommonModel::$del_normal,
- 'good_group_id' => $data['good_group_id'],
- 'good_id' => $data['good_id']])
- ->findOrEmpty();
- if (!empty($rs)) throw new Exception('该商品已存在,不能在同一商品分组下重复添加');
- $res = Db::name('good_group_item')
- ->insert(array_merge($data, [
- 'is_del' => CommonModel::$del_normal,
- 'addtime' => date('Y-m-d H:i:s'),
- 'updatetime' => date('Y-m-d H:i:s'),
- ]));
- Db::name('good_group')
- ->where(['id' => $data['good_group_id'], 'is_del' => CommonModel::$del_normal])
- ->inc('num', $res)
- ->update(['updatetime' => date('Y-m-d H:i:s')]);
- Db::commit();
- return $res ? json_show(CommonModel::$success, '添加商品分组明细成功') : 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 = Db::name('good_group_item')
- ->field(true)
- ->where(['id' => $id, 'is_del' => CommonModel::$del_normal])
- ->findOrEmpty();
- if (empty($res)) throw new ValidateException('该商品分组明细为空');
- return json_show(CommonModel::$success, '获取商品分组明细详情成功', $res);
- }
- //编辑
- public static function edit(array $data = []): Json
- {
- $rs = GoodModel::field('id')
- ->where(['id' => $data['good_id'], 'is_del' => CommonModel::$del_normal])
- ->findOrEmpty()
- ->isEmpty();
- if ($rs) return json_show(CommonModel::$error_param, '该商品不存在');
- $res = Db::name('good_group_item')
- ->where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
- ->save(array_merge($data, ['updatetime' => date('Y-m-d H:i:s')]));
- return $res ? json_show(CommonModel::$success, '编辑商品分组明细成功') : json_show(CommonModel::$error_param, '编辑商品分组明细失败');
- }
- //删除
- public static function delete(array $ids = []): Json
- {
- Db::startTrans();
- try {
- $good_group_id = Db::name('good_group_item')
- ->whereIn('id', $ids)
- ->where('is_del', CommonModel::$del_normal)
- ->group('good_group_id')
- ->column('good_group_id');
- if (count($good_group_id) > 1) throw new Exception('不能删除不同分组下的商品');
- $num = Db::name('good_group_item')
- ->whereIn('id', $ids)
- ->where('is_del', CommonModel::$del_normal)
- ->update([
- 'is_del' => CommonModel::$del_deleted,
- 'updatetime' => date('Y-m-d H:i:s'),
- ]);
- Db::name('good_group')
- ->where(['id' => $good_group_id[0], 'is_del' => CommonModel::$del_normal])
- ->dec('num', $num)
- ->update([
- 'updatetime' => date('Y-m-d H:i:s'),
- 'updaterid' => self::$uid,
- 'updater' => self::$uid
- ]);
- Db::commit();
- return $num ? json_show(CommonModel::$success, '删除成功') : json_show(CommonModel::$error_param, '删除失败,该商品分组明细不存在');
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(CommonModel::$error_param, '删除商品分组明细失败,' . $exception->getMessage());
- }
- }
- }
|