|
@@ -0,0 +1,358 @@
|
|
|
+<?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 . '|requireIf:type,' . ThemeModel::$type_propaganda . '|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());
|
|
|
+
|
|
|
+ $v['theme_modular_id'] = $modular_id;
|
|
|
+ $v['is_del'] = CommonModel::$del_normal;
|
|
|
+ $v['good_name'] = $modular['type'] == ThemeModel::$type_exhibition ? $goods[$v['good_id']] ?? '' : '';
|
|
|
+ $v['addtime'] = $date;
|
|
|
+ $v['updatetime'] = $date;
|
|
|
+ }
|
|
|
+
|
|
|
+ $insert_data = array_merge($insert_data, $da);
|
|
|
+
|
|
|
+ //模块数据记录表
|
|
|
+ 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 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,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ //模块验证规则
|
|
|
+ $val_modular = Validate::rule([
|
|
|
+ 'id|主题模块id' => 'require|number|gt:0',
|
|
|
+ '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 . '|requireIf:type,' . ThemeModel::$type_propaganda . '|max:255',
|
|
|
+ 'data|模块数据' => 'require|array|max:100',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ //数据验证规则
|
|
|
+ $val_data = Validate::rule([
|
|
|
+ 'id|主题模块数据id' => 'number',
|
|
|
+ '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'
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $del = $ins = [];
|
|
|
+
|
|
|
+ foreach ($data['modular'] as $modular) {
|
|
|
+
|
|
|
+ if (!$val_modular->check($modular)) throw new Exception('模块参数有误,' . $val_modular->getError());
|
|
|
+
|
|
|
+ //模块记录表
|
|
|
+ Db::name('theme_modular')
|
|
|
+ ->where(['is_del' => CommonModel::$del_normal, 'id' => $modular['id']])
|
|
|
+ ->update([
|
|
|
+ 'title' => $modular['title'],
|
|
|
+ 'status' => $modular['status'],
|
|
|
+ 'updatetime' => $date,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $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());
|
|
|
+
|
|
|
+ if (isset($v['id'])) {
|
|
|
+ if ($v['is_del'] == CommonModel::$del_deleted) $del[] = $v['id'];
|
|
|
+ else {
|
|
|
+ Db::name('theme_modular_data')
|
|
|
+ ->where(['is_del' => CommonModel::$del_normal, 'id' => $v['id']])
|
|
|
+ ->update([
|
|
|
+ 'img' => $v['img'],
|
|
|
+ 'jump_type' => $v['jump_type'],
|
|
|
+ 'jump_param' => $v['jump_param'],
|
|
|
+ 'good_name' => $modular['type'] == ThemeModel::$type_exhibition ? $goods[$v['good_id']] ?? '' : '',
|
|
|
+ 'good_id' => $v['good_id'],
|
|
|
+ 'style_type' => $v['style_type'],
|
|
|
+ 'updatetime' => $date,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $ins[] = array_merge($v, [
|
|
|
+ 'theme_modular_id' => $modular['id'],
|
|
|
+ 'is_del' => CommonModel::$del_normal,
|
|
|
+ 'good_name' => $modular['type'] == ThemeModel::$type_exhibition ? $goods[$v['good_id']] ?? '' : '',
|
|
|
+ 'addtime' => $date,
|
|
|
+ 'updatetime' => $date,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($del) {
|
|
|
+ Db::name('theme_modular_data')
|
|
|
+ ->where(['is_del' => CommonModel::$del_normal])
|
|
|
+ ->whereIn('id', $del)
|
|
|
+ ->update([
|
|
|
+ 'is_del' => CommonModel::$del_deleted,
|
|
|
+ 'updatetime' => $date,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ if ($ins) {
|
|
|
+
|
|
|
+ Db::name('theme_modular_data')->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
|
|
|
+ {
|
|
|
+ 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());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|