alias('a') ->where(['a.is_del' => CommonModel::$del_normal, 'group_id' => $data['group_id']]); if ($data['type'] == GroupModel::$type_good) { $db->leftJoin('good b', 'b.id=a.item_id AND b.is_del=' . CommonModel::$del_normal); $name = 'good_name'; $field = 'a.id,b.good_code code,b.good_name title,b.good_remark remark,a.is_top,a.weight'; } else { $db->leftJoin('video b', 'b.id=a.item_id AND b.is_del=' . CommonModel::$del_normal); $name = 'video_name'; $field = 'a.id,b.video_sn code,b.video_name title,b.remark,a.is_top,a.weight'; } if ($data['title'] != '') $db->whereLike($name, '%' . $data['title'] . '%'); $count = $db->count('a.id'); $list = $db->field($field) ->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 = GroupModel::field('id') ->where(['id' => $data['group_id'], 'is_del' => CommonModel::$del_normal]) ->findOrEmpty() ->isEmpty(); if ($rs) throw new Exception('该分组不存在'); if ($data['type'] == GroupModel::$type_good) { $rs = GoodModel::field('id') ->where(['id' => $data['item_id'], 'is_del' => CommonModel::$del_normal]) ->findOrEmpty() ->isEmpty(); if ($rs) throw new Exception('该商品不存在'); Db::name('group') ->where(['id' => $data['group_id'], 'is_del' => CommonModel::$del_normal]) ->inc('good_num', 1) ->update(['updatetime' => date('Y-m-d H:i:s')]); } else { $rs = VideoModel::field('id') ->where(['id' => $data['item_id'], 'is_del' => CommonModel::$del_normal]) ->findOrEmpty() ->isEmpty(); if ($rs) throw new Exception('该视频不存在'); Db::name('group') ->where(['id' => $data['group_id'], 'is_del' => CommonModel::$del_normal]) ->inc('video_num', 1) ->update(['updatetime' => date('Y-m-d H:i:s')]); } $rs = Db::name('group_item') ->field('id') ->where([ 'is_del' => CommonModel::$del_normal, 'group_id' => $data['group_id'], 'type' => $data['type'], 'item_id' => $data['item_id'] ]) ->findOrEmpty(); if (!empty($rs)) throw new Exception('该明细已存在,不能在同一分组下重复添加'); $res = Db::name('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::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('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 = Db::name('group_item') ->field('id,group_id,type,item_id') ->where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal]) ->findOrEmpty(); if (empty($rs)) return json_show(CommonModel::$error_param, '该分组明细不存在'); if ($rs['item_id'] != $data['item_id']) { $temp = Db::name('group_item') ->field('id,group_id,type,item_id') ->where([ 'is_del' => CommonModel::$del_normal, 'group_id' => $rs['group_id'], 'type' => $rs['type'], 'item_id' => $data['item_id'] ])->findOrEmpty(); if (!empty($temp)) return json_show(CommonModel::$error_param, '该明细已存在,不能在同一分组下重复添加'); } if ($rs['type'] == GroupModel::$type_good) { $result = GoodModel::field('id') ->where(['id' => $data['item_id'], 'is_del' => CommonModel::$del_normal]) ->findOrEmpty() ->isEmpty(); if ($result) return json_show(CommonModel::$error_param, '该商品不存在'); } else { $result = VideoModel::field('id') ->where(['id' => $data['item_id'], 'is_del' => CommonModel::$del_normal]) ->findOrEmpty() ->isEmpty(); if ($result) return json_show(CommonModel::$error_param, '该视频不存在'); } $res = Db::name('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 { $group_id = Db::name('group_item') ->whereIn('id', $ids) ->where('is_del', CommonModel::$del_normal) ->group('group_id,type') ->column('group_id,type'); if (count($group_id) > 1) throw new Exception('不能删除不同分组下不同类型的明细'); $num = Db::name('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'), ]); if ($group_id[0]['type'] == GroupModel::$type_good) $field = 'good_num'; else $field = 'video_num'; Db::name('group') ->where(['id' => $group_id[0]['group_id'], 'is_del' => CommonModel::$del_normal]) ->dec($field, $num) ->update([ 'updatetime' => date('Y-m-d H:i:s'), 'updaterid' => self::$uid, 'updater' => self::$uname ]); 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()); } } //置顶 public static function top(array $data = []): Json { $res = Db::name('group_item') ->where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal]) ->where('is_top', '<>', $data['is_top']) ->update(['is_top' => $data['is_top'], 'updatetime' => date('Y-m-d H:i:s')]); return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$error_param, '操作失败,该明细不存在或重复操作'); } }