ThemeLogic.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. namespace app\admin\logic;
  3. use app\model\CommonModel;
  4. use app\model\GoodModel;
  5. use app\model\GroupModel;
  6. use app\model\ThemeModel;
  7. use think\Exception;
  8. use think\facade\Db;
  9. use think\facade\Validate;
  10. use think\response\Json;
  11. class ThemeLogic extends BaseLogic
  12. {
  13. //列表
  14. public static function list(array $data = []): Json
  15. {
  16. $db = ThemeModel::alias('a')
  17. ->leftJoin('group b', 'b.id=a.group_id AND b.is_del=' . CommonModel::$del_normal)
  18. ->leftJoin('company c', 'c.id=b.company_id AND c.is_del=' . CommonModel::$del_normal)
  19. ->leftJoin('card d', 'd.id=b.card_id AND d.is_del=' . CommonModel::$del_normal)
  20. ->where('a.is_del', CommonModel::$del_normal);
  21. if ($data['company_title'] !== '') $db->whereLike('c.title', '%' . $data['company_title'] . '%');
  22. if ($data['card_title']) $db->whereLike('d.title', '%' . $data['card_title'] . '%');
  23. if ($data['status']) $db->where('a.status', $data['status']);
  24. $count = $db->count('a.id');
  25. $video = $db
  26. ->field('a.id,c.title company_title,d.title card_title,a.code,a.status,a.creater,a.addtime')
  27. ->page($data['page'], $data['size'])
  28. ->order('a.addtime', 'desc')
  29. ->select()
  30. ->toArray();
  31. return json_show(CommonModel::$success, '获取成功', ['list' => $video, 'count' => $count]);
  32. }
  33. //添加
  34. public static function add(array $data = []): Json
  35. {
  36. Db::startTrans();
  37. try {
  38. $rs = GroupModel::field('id')
  39. ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['group_id']])
  40. ->findOrEmpty()
  41. ->isEmpty();
  42. if ($rs) throw new Exception('该分组不存在');
  43. $rs = ThemeModel::field('id')
  44. ->where(['is_del' => CommonModel::$del_normal, 'code' => $data['code']])
  45. ->findOrEmpty()
  46. ->isEmpty();
  47. if (!$rs) throw new Exception('该主题编码已存在');
  48. $rs = ThemeModel::field('id')
  49. ->where(['is_del' => CommonModel::$del_normal, 'group_id' => $data['group_id']])
  50. ->findOrEmpty()
  51. ->isEmpty();
  52. if (!$rs) throw new Exception('该分组已存在对应手机主题');
  53. $date = date('Y-m-d H:i:s');
  54. //手机主题
  55. $theme_id = Db::name('theme')->insertGetId([
  56. 'group_id' => $data['group_id'],
  57. 'code' => $data['code'],
  58. 'status' => CommonModel::$status_normal,
  59. 'is_del' => CommonModel::$del_normal,
  60. 'createrid' => self::$uid,
  61. 'creater' => self::$uname,
  62. 'addtime' => $date,
  63. 'updaterid' => self::$uid,
  64. 'updater' => self::$uname,
  65. 'updatetime' => $date,
  66. ]);
  67. //模块验证规则
  68. $val_modular = Validate::rule([
  69. 'type|模块类型' => 'require|number|in:' . ThemeModel::$type_advantage . ',' . ThemeModel::$type_banner . ',' . ThemeModel::$type_exhibition . ',' . ThemeModel::$type_propaganda,
  70. 'status|状态' => 'require|number|in:' . CommonModel::$status_normal . ',' . CommonModel::$status_disable,
  71. 'title|模块名称' => 'requireIf:type,' . ThemeModel::$type_advantage . '|requireIf:type,' . ThemeModel::$type_exhibition . '|max:255',
  72. 'data|模块数据' => 'require|array|max:100',
  73. ]);
  74. //数据验证规则
  75. $val_data = Validate::rule([
  76. 'img|图片' => 'require|max:255',
  77. 'jump_type|跳转类型' => 'require|number|in:' . ThemeModel::$jump_type_external . ',' . ThemeModel::$jump_type_inside . ',' . ThemeModel::$jump_type_no,
  78. 'jump_param|跳转参数' => 'max:255',
  79. 'good_id|商品id' => 'number|gt:0',
  80. 'title|模块数据名称名称' => 'requireIf:type,' . ThemeModel::$type_propaganda . '|max:255',
  81. ]);
  82. foreach ($data['modular'] as $modular) {
  83. if (!$val_modular->check($modular)) throw new Exception('模块参数有误,' . $val_modular->getError());
  84. //模块记录表
  85. $modular_id = Db::name('theme_modular')->insertGetId([
  86. 'theme_id' => $theme_id,
  87. 'title' => $modular['title'],
  88. 'type' => $modular['type'],
  89. 'status' => $modular['status'],
  90. 'is_del' => CommonModel::$del_normal,
  91. 'addtime' => $date,
  92. 'updatetime' => $date,
  93. ]);
  94. $insert_data = [];
  95. $da = $modular['data'];
  96. $goods = GoodModel::whereIn('id', array_column($da, 'good_id'))
  97. ->where('is_del', CommonModel::$del_normal)
  98. ->column('good_name', 'id');
  99. foreach ($da as &$v) {
  100. if ($modular['type'] == ThemeModel::$type_exhibition) $val_data->append('good_id', 'require');
  101. if (!$val_data->check($v)) throw new Exception('模块数据参数有误,' . $val_data->getError());
  102. $v['theme_modular_id'] = $modular_id;
  103. $v['is_del'] = CommonModel::$del_normal;
  104. $v['good_name'] = $modular['type'] == ThemeModel::$type_exhibition ? $goods[$v['good_id']] ?? '' : '';
  105. $v['addtime'] = $date;
  106. $v['updatetime'] = $date;
  107. }
  108. $insert_data = array_merge($insert_data, $da);
  109. //模块数据记录表
  110. Db::name('theme_modular_data')->insertAll($insert_data);
  111. }
  112. Db::commit();
  113. return json_show(CommonModel::$success, '添加手机主题成功');
  114. } catch (Exception $exception) {
  115. Db::rollback();
  116. return json_show(CommonModel::$error_param, '添加手机主题失败,' . $exception->getMessage());
  117. }
  118. }
  119. //详情
  120. public static function read(int $id = 0): Json
  121. {
  122. $rs = ThemeModel::field(true)
  123. ->where(['is_del' => CommonModel::$del_normal, 'id' => $id])
  124. ->append(['modular'])
  125. ->withAttr('modular', function ($val, $data) {
  126. return Db::name('theme_modular')
  127. ->field(true)
  128. ->where(['is_del' => CommonModel::$del_normal, 'theme_id' => $data['id']])
  129. ->order(['addtime' => 'desc', 'id' => 'desc'])
  130. ->append(['data'])
  131. ->withAttr('data', function ($v, $d) {
  132. return Db::name('theme_modular_data')
  133. ->field(true)
  134. ->where(['is_del' => CommonModel::$del_normal, 'theme_modular_id' => $d['id']])
  135. ->order(['addtime' => 'desc', 'id' => 'desc'])
  136. ->select()
  137. ->toArray();
  138. })
  139. ->select()
  140. ->toArray();
  141. })
  142. ->findOrEmpty()
  143. ->toArray();
  144. return empty($rs) ? json_show(CommonModel::$error_param, '该手机主题不存在') : json_show(CommonModel::$success, '获取手机主题详情成功', $rs);
  145. }
  146. //修改
  147. public static function edit(array $data = []): Json
  148. {
  149. Db::startTrans();
  150. try {
  151. $rs = ThemeModel::field('id,code,group_id')
  152. ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['id']])
  153. ->findOrEmpty();
  154. if ($rs->isEmpty()) throw new Exception('该主题不存在');
  155. if ($rs->code != $data['code']) {
  156. //校验code是否重复
  157. $temp = ThemeModel::field('id')
  158. ->where(['is_del' => CommonModel::$del_normal, 'code' => $data['code']])
  159. ->findOrEmpty()
  160. ->isEmpty();
  161. if (!$temp) throw new Exception('该编码已存在');
  162. }
  163. if ($rs->group_id != $data['group_id']) {
  164. //校验分组id是否存在
  165. $temp = GroupModel::field('id')
  166. ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['group_id']])
  167. ->findOrEmpty()
  168. ->isEmpty();
  169. if ($temp) throw new Exception('该分组不存在');
  170. //校验分组id是否重复
  171. $temp = ThemeModel::field('id')
  172. ->where(['is_del' => CommonModel::$del_normal, 'group_id' => $data['group_id']])
  173. ->findOrEmpty()
  174. ->isEmpty();
  175. if (!$temp) throw new Exception('该分组下手机主题重复');
  176. }
  177. $date = date('Y-m-d H:i:s');
  178. //手机主题
  179. ThemeModel::where(['is_del' => CommonModel::$del_normal, 'id' => $data['id']])->save([
  180. 'group_id' => $data['group_id'],
  181. 'code' => $data['code'],
  182. 'updaterid' => self::$uid,
  183. 'updater' => self::$uname,
  184. 'updatetime' => $date,
  185. ]);
  186. //模块验证规则
  187. $val_modular = Validate::rule([
  188. 'id|主题模块id' => 'require|number|gt:0',
  189. 'type|模块类型' => 'require|number|in:' . ThemeModel::$type_advantage . ',' . ThemeModel::$type_banner . ',' . ThemeModel::$type_exhibition . ',' . ThemeModel::$type_propaganda,
  190. 'status|状态' => 'require|number|in:' . CommonModel::$status_normal . ',' . CommonModel::$status_disable,
  191. 'title|模块名称' => 'requireIf:type,' . ThemeModel::$type_advantage . '|requireIf:type,' . ThemeModel::$type_exhibition . '|max:255',
  192. 'data|模块数据' => 'require|array|max:100',
  193. ]);
  194. //数据验证规则
  195. $val_data = Validate::rule([
  196. 'id|主题模块数据id' => 'number',
  197. 'img|图片' => 'require|max:255',
  198. 'jump_type|跳转类型' => 'require|number|in:' . ThemeModel::$jump_type_external . ',' . ThemeModel::$jump_type_inside . ',' . ThemeModel::$jump_type_no,
  199. 'jump_param|跳转参数' => 'max:255',
  200. 'good_id|商品id' => 'number',
  201. 'title|模块数据名称' => 'requireIf:type,' . ThemeModel::$type_propaganda . '|max:255',
  202. ]);
  203. $del = $ins = [];
  204. foreach ($data['modular'] as $modular) {
  205. if (!$val_modular->check($modular)) throw new Exception('模块参数有误,' . $val_modular->getError());
  206. //模块记录表
  207. Db::name('theme_modular')
  208. ->where(['is_del' => CommonModel::$del_normal, 'id' => $modular['id']])
  209. ->update([
  210. 'title' => $modular['title'],
  211. 'status' => $modular['status'],
  212. 'updatetime' => $date,
  213. ]);
  214. $da = $modular['data'];
  215. $goods = GoodModel::whereIn('id', array_column($da, 'good_id'))
  216. ->where('is_del', CommonModel::$del_normal)
  217. ->column('good_name', 'id');
  218. foreach ($da as &$v) {
  219. if ($modular['type'] == ThemeModel::$type_exhibition) $val_data->append('good_id', 'require');
  220. if (!$val_data->check($v)) throw new Exception('模块数据参数有误,' . $val_data->getError());
  221. if (isset($v['id'])) {
  222. if ($v['is_del'] == CommonModel::$del_deleted) $del[] = $v['id'];
  223. else {
  224. Db::name('theme_modular_data')
  225. ->where(['is_del' => CommonModel::$del_normal, 'id' => $v['id']])
  226. ->update([
  227. 'img' => $v['img'],
  228. 'jump_type' => $v['jump_type'],
  229. 'jump_param' => $v['jump_param'],
  230. 'good_name' => $modular['type'] == ThemeModel::$type_exhibition ? $goods[$v['good_id']] ?? '' : '',
  231. 'good_id' => $v['good_id'],
  232. 'style_type' => $v['style_type'],
  233. 'title' => $v['title'],
  234. 'updatetime' => $date,
  235. ]);
  236. }
  237. } else {
  238. $ins[] = array_merge($v, [
  239. 'theme_modular_id' => $modular['id'],
  240. 'is_del' => CommonModel::$del_normal,
  241. 'good_name' => $modular['type'] == ThemeModel::$type_exhibition ? $goods[$v['good_id']] ?? '' : '',
  242. 'addtime' => $date,
  243. 'updatetime' => $date,
  244. ]);
  245. }
  246. }
  247. }
  248. if ($del) {
  249. Db::name('theme_modular_data')
  250. ->where(['is_del' => CommonModel::$del_normal])
  251. ->whereIn('id', $del)
  252. ->update([
  253. 'is_del' => CommonModel::$del_deleted,
  254. 'updatetime' => $date,
  255. ]);
  256. }
  257. if ($ins) {
  258. Db::name('theme_modular_data')->insertAll($ins);
  259. }
  260. Db::commit();
  261. return json_show(CommonModel::$success, '修改手机主题成功');
  262. } catch (Exception $exception) {
  263. Db::rollback();
  264. return json_show(CommonModel::$error_param, '修改手机主题失败,' . $exception->getMessage());
  265. }
  266. }
  267. //启禁用
  268. public static function status(array $data = []): Json
  269. {
  270. Db::startTrans();
  271. try {
  272. $where = [
  273. ['id', '=', $data['id']],
  274. ['is_del', '=', CommonModel::$del_normal],
  275. ['status', '<>', $data['status']],
  276. ];
  277. $date = date('Y-m-d H:i:s');
  278. $res = ThemeModel::where($where)->save(array_merge($data, ['updater' => self::$uname, 'updaterid' => self::$uid, 'updatetime' => $date]));
  279. if (!$res) throw new Exception('该手机主题不存在或重复操作');
  280. Db::name('theme_modular')
  281. ->where(['is_del' => CommonModel::$del_normal, 'theme_id' => $data['id']])
  282. ->update(['status' => $data['status'], 'updatetime' => $date]);
  283. Db::commit();
  284. return json_show(CommonModel::$success, '操作成功');
  285. } catch (Exception $exception) {
  286. Db::rollback();
  287. return json_show(CommonModel::$error_param, '操作失败,' . $exception->getMessage());
  288. }
  289. }
  290. }