123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- <?php
- namespace app\admin\logic;
- use app\model\CommonModel;
- use app\model\GoodModel;
- use app\model\GroupModel;
- use app\model\ThemeModel;
- use think\Exception;
- use think\facade\Db;
- use think\facade\Validate;
- use think\response\Json;
- class ThemeLogic extends BaseLogic
- {
- //列表
- public static function list(array $data = []): Json
- {
- $db = ThemeModel::alias('a')
- ->leftJoin('group b', 'b.id=a.group_id AND b.is_del=' . CommonModel::$del_normal)
- ->leftJoin('company c', 'c.id=b.company_id AND c.is_del=' . CommonModel::$del_normal)
- ->leftJoin('card d', 'd.id=b.card_id AND d.is_del=' . CommonModel::$del_normal)
- ->where('a.is_del', CommonModel::$del_normal);
- if ($data['company_title'] !== '') $db->whereLike('c.title', '%' . $data['company_title'] . '%');
- if ($data['card_title']) $db->whereLike('d.title', '%' . $data['card_title'] . '%');
- if ($data['status']) $db->where('a.status', $data['status']);
- $count = $db->count('a.id');
- $video = $db
- ->field('a.id,c.title company_title,d.title card_title,a.code,a.status,a.creater,a.addtime')
- ->page($data['page'], $data['size'])
- ->order('a.addtime', 'desc')
- ->select()
- ->toArray();
- return json_show(CommonModel::$success, '获取成功', ['list' => $video, 'count' => $count]);
- }
- //添加
- public static function add(array $data = []): Json
- {
- Db::startTrans();
- try {
- $rs = GroupModel::field('id')
- ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['group_id']])
- ->findOrEmpty()
- ->isEmpty();
- if ($rs) throw new Exception('该分组不存在');
- $rs = ThemeModel::field('id')
- ->where(['is_del' => CommonModel::$del_normal, 'code' => $data['code']])
- ->findOrEmpty()
- ->isEmpty();
- if (!$rs) throw new Exception('该主题编码已存在');
- $rs = ThemeModel::field('id')
- ->where(['is_del' => CommonModel::$del_normal, 'group_id' => $data['group_id']])
- ->findOrEmpty()
- ->isEmpty();
- if (!$rs) throw new Exception('该分组已存在对应手机主题');
- $date = date('Y-m-d H:i:s');
- //手机主题
- $theme_id = Db::name('theme')->insertGetId([
- 'group_id' => $data['group_id'],
- 'code' => $data['code'],
- '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,
- ]);
- //模块验证规则
- $val_modular = Validate::rule([
- 'type|模块类型' => 'require|number|in:' . ThemeModel::$type_advantage . ',' . ThemeModel::$type_banner . ',' . ThemeModel::$type_exhibition . ',' . ThemeModel::$type_propaganda,
- 'status|状态' => 'require|number|in:' . CommonModel::$status_normal . ',' . CommonModel::$status_disable,
- 'title|模块名称' => 'requireIf:type,' . ThemeModel::$type_advantage . '|requireIf:type,' . ThemeModel::$type_exhibition . '|max:255',
- 'data|模块数据' => 'require|array|max:100',
- ]);
- //数据验证规则
- $val_data = Validate::rule([
- 'img|图片' => 'require|max:255',
- 'jump_type|跳转类型' => 'require|number|in:' . ThemeModel::$jump_type_external . ',' . ThemeModel::$jump_type_inside . ',' . ThemeModel::$jump_type_no,
- 'jump_param|跳转参数' => 'max:255',
- 'good_id|商品id' => 'number|gt:0',
- ]);
- foreach ($data['modular'] as $modular) {
- if (!$val_modular->check($modular)) throw new Exception('模块参数有误,' . $val_modular->getError());
- //模块记录表
- $modular_id = Db::name('theme_modular')->insertGetId([
- 'theme_id' => $theme_id,
- 'title' => $modular['title'],
- 'type' => $modular['type'],
- 'status' => $modular['status'],
- 'is_del' => CommonModel::$del_normal,
- 'addtime' => $date,
- 'updatetime' => $date,
- ]);
- $insert_data = [];
- $da = $modular['data'];
- $goods = GoodModel::whereIn('id', array_column($da, 'good_id'))
- ->where('is_del', CommonModel::$del_normal)
- ->column('good_name', 'id');
- foreach ($da as &$v) {
- if ($modular['type'] == ThemeModel::$type_exhibition) $val_data->append('good_id', 'require');
- if (!$val_data->check($v)) throw new Exception('模块数据参数有误,' . $val_data->getError());
- $insert_data[] = [
- 'theme_modular_id' => $modular_id,
- 'img' => $v['img'],
- 'jump_type' => $v['jump_type'],
- 'jump_param' => $v['jump_param'],
- 'title' => $v['title'] ?? '',
- 'good_id' => $v['good_id'],
- 'style_type' => $v['style_type'],
- 'good_name' => $modular['type'] == ThemeModel::$type_exhibition ? $goods[$v['good_id']] ?? '' : '',
- 'is_del' => CommonModel::$del_normal,
- 'addtime' => $date,
- 'updatetime' => $date,
- ];
- }
- //模块数据记录表
- Db::name('theme_modular_data')->insertAll($insert_data);
- }
- Db::commit();
- return json_show(CommonModel::$success, '添加手机主题成功');
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(CommonModel::$error_param, '添加手机主题失败,' . $exception->getMessage() . '|' . $exception->getFile() . '|' . $exception->getLine());
- }
- }
- //详情
- public static function read(int $id = 0): Json
- {
- $rs = ThemeModel::field(true)
- ->where(['is_del' => CommonModel::$del_normal, 'id' => $id])
- ->append(['modular'])
- ->withAttr('modular', function ($val, $data) {
- return Db::name('theme_modular')
- ->field(true)
- ->where(['is_del' => CommonModel::$del_normal, 'theme_id' => $data['id']])
- ->order(['addtime' => 'desc', 'id' => 'desc'])
- ->append(['data'])
- ->withAttr('data', function ($v, $d) {
- return Db::name('theme_modular_data')
- ->field(true)
- ->where(['is_del' => CommonModel::$del_normal, 'theme_modular_id' => $d['id']])
- ->order(['addtime' => 'desc', 'id' => 'desc'])
- ->select()
- ->toArray();
- })
- ->select()
- ->toArray();
- })
- ->findOrEmpty()
- ->toArray();
- return empty($rs) ? json_show(CommonModel::$error_param, '该手机主题不存在') : json_show(CommonModel::$success, '获取手机主题详情成功', $rs);
- }
- //修改
- public static function edit(array $data = []): Json
- {
- Db::startTrans();
- try {
- $rs = ThemeModel::field('id,code,group_id')
- ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['id']])
- ->findOrEmpty();
- if ($rs->isEmpty()) throw new Exception('该主题不存在');
- if ($rs->code != $data['code']) {
- //校验code是否重复
- $temp = ThemeModel::field('id')
- ->where(['is_del' => CommonModel::$del_normal, 'code' => $data['code']])
- ->findOrEmpty()
- ->isEmpty();
- if (!$temp) throw new Exception('该编码已存在');
- }
- if ($rs->group_id != $data['group_id']) {
- //校验分组id是否存在
- $temp = GroupModel::field('id')
- ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['group_id']])
- ->findOrEmpty()
- ->isEmpty();
- if ($temp) throw new Exception('该分组不存在');
- //校验分组id是否重复
- $temp = ThemeModel::field('id')
- ->where(['is_del' => CommonModel::$del_normal, 'group_id' => $data['group_id']])
- ->findOrEmpty()
- ->isEmpty();
- if (!$temp) throw new Exception('该分组下手机主题重复');
- }
- $date = date('Y-m-d H:i:s');
- //手机主题
- ThemeModel::where(['is_del' => CommonModel::$del_normal, 'id' => $data['id']])->save([
- 'group_id' => $data['group_id'],
- 'code' => $data['code'],
- 'updaterid' => self::$uid,
- 'updater' => self::$uname,
- 'updatetime' => $date,
- ]);
- $all_theme_modular_id = Db::name('theme_modular')
- ->where([
- 'theme_id' => $data['id'],
- 'is_del' => CommonModel::$del_normal,
- ])->column('id');
- Db::name('theme_modular')
- ->where('is_del', CommonModel::$del_normal)
- ->whereIn('id', $all_theme_modular_id)
- ->update([
- 'is_del' => CommonModel::$del_deleted,
- 'updatetime' => $date,
- ]);
- Db::name('theme_modular_data')
- ->where('is_del', CommonModel::$del_normal)
- ->whereIn('theme_modular_id', $all_theme_modular_id)
- ->update([
- 'is_del' => CommonModel::$del_deleted,
- 'updatetime' => $date,
- ]);
- //模块验证规则
- $val_modular = Validate::rule([
- 'type|模块类型' => 'require|number|in:' . ThemeModel::$type_advantage . ',' . ThemeModel::$type_banner . ',' . ThemeModel::$type_exhibition . ',' . ThemeModel::$type_propaganda,
- 'status|状态' => 'require|number|in:' . CommonModel::$status_normal . ',' . CommonModel::$status_disable,
- 'title|模块名称' => 'requireIf:type,' . ThemeModel::$type_advantage . '|requireIf:type,' . ThemeModel::$type_exhibition . '|max:255',
- 'data|模块数据' => 'require|array|max:100',
- ]);
- //数据验证规则
- $val_data = Validate::rule([
- 'img|图片' => 'require|max:255',
- 'jump_type|跳转类型' => 'require|number|in:' . ThemeModel::$jump_type_external . ',' . ThemeModel::$jump_type_inside . ',' . ThemeModel::$jump_type_no,
- 'jump_param|跳转参数' => 'max:255',
- 'good_id|商品id' => 'number|egt:0',
- ]);
- foreach ($data['modular'] as $modular) {
- if (!$val_modular->check($modular)) throw new Exception('模块参数有误,' . $val_modular->getError());
- //模块记录表
- $modular_id = Db::name('theme_modular')->insertGetId([
- 'theme_id' => $data['id'],
- 'title' => $modular['title'],
- 'type' => $modular['type'],
- 'status' => $modular['status'],
- 'is_del' => CommonModel::$del_normal,
- 'addtime' => $date,
- 'updatetime' => $date,
- ]);
- $insert_data = [];
- $da = $modular['data'];
- $goods = GoodModel::whereIn('id', array_column($da, 'good_id'))
- ->where('is_del', CommonModel::$del_normal)
- ->column('good_name', 'id');
- foreach ($da as &$v) {
- if ($modular['type'] == ThemeModel::$type_exhibition) $val_data->append('good_id', 'require');
- if (!$val_data->check($v)) throw new Exception('模块数据参数有误,' . $val_data->getError());
- $insert_data[] = [
- 'theme_modular_id' => $modular_id,
- 'img' => $v['img'],
- 'jump_type' => $v['jump_type'],
- 'jump_param' => $v['jump_param'],
- 'title' => $v['title'] ?? '',
- 'good_id' => $v['good_id'],
- 'style_type' => $v['style_type'],
- 'good_name' => $modular['type'] == ThemeModel::$type_exhibition ? $goods[$v['good_id']] ?? '' : '',
- 'is_del' => CommonModel::$del_normal,
- 'addtime' => $date,
- 'updatetime' => $date,
- ];
- }
- //模块数据记录表
- Db::name('theme_modular_data')->insertAll($insert_data);
- }
- 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
- {
- Db::startTrans();
- try {
- $where = [
- ['id', '=', $data['id']],
- ['is_del', '=', CommonModel::$del_normal],
- ['status', '<>', $data['status']],
- ];
- $date = date('Y-m-d H:i:s');
- $res = ThemeModel::where($where)->save(array_merge($data, ['updater' => self::$uname, 'updaterid' => self::$uid, 'updatetime' => $date]));
- if (!$res) throw new Exception('该手机主题不存在或重复操作');
- Db::name('theme_modular')
- ->where(['is_del' => CommonModel::$del_normal, 'theme_id' => $data['id']])
- ->update(['status' => $data['status'], 'updatetime' => $date]);
- Db::commit();
- return json_show(CommonModel::$success, '操作成功');
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(CommonModel::$error_param, '操作失败,' . $exception->getMessage());
- }
- }
- }
|