ThemeLogic.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. ]);
  81. foreach ($data['modular'] as $modular) {
  82. if (!$val_modular->check($modular)) throw new Exception('模块参数有误,' . $val_modular->getError());
  83. //模块记录表
  84. $modular_id = Db::name('theme_modular')->insertGetId([
  85. 'theme_id' => $theme_id,
  86. 'title' => $modular['title'],
  87. 'type' => $modular['type'],
  88. 'status' => $modular['status'],
  89. 'is_del' => CommonModel::$del_normal,
  90. 'addtime' => $date,
  91. 'updatetime' => $date,
  92. ]);
  93. $insert_data = [];
  94. $da = $modular['data'];
  95. $goods = GoodModel::whereIn('id', array_column($da, 'good_id'))
  96. ->where('is_del', CommonModel::$del_normal)
  97. ->column('good_name', 'id');
  98. foreach ($da as &$v) {
  99. if ($modular['type'] == ThemeModel::$type_exhibition) $val_data->append('good_id', 'require');
  100. if (!$val_data->check($v)) throw new Exception('模块数据参数有误,' . $val_data->getError());
  101. $insert_data[] = [
  102. 'theme_modular_id' => $modular_id,
  103. 'img' => $v['img'],
  104. 'jump_type' => $v['jump_type'],
  105. 'jump_param' => $v['jump_param'],
  106. 'title' => $v['title'] ?? '',
  107. 'good_id' => $v['good_id'],
  108. 'style_type' => $v['style_type'],
  109. 'good_name' => $modular['type'] == ThemeModel::$type_exhibition ? $goods[$v['good_id']] ?? '' : '',
  110. 'is_del' => CommonModel::$del_normal,
  111. 'addtime' => $date,
  112. 'updatetime' => $date,
  113. ];
  114. }
  115. //模块数据记录表
  116. Db::name('theme_modular_data')->insertAll($insert_data);
  117. }
  118. Db::commit();
  119. return json_show(CommonModel::$success, '添加手机主题成功');
  120. } catch (Exception $exception) {
  121. Db::rollback();
  122. return json_show(CommonModel::$error_param, '添加手机主题失败,' . $exception->getMessage() . '|' . $exception->getFile() . '|' . $exception->getLine());
  123. }
  124. }
  125. //详情
  126. public static function read(int $id = 0): Json
  127. {
  128. $rs = ThemeModel::field(true)
  129. ->where(['is_del' => CommonModel::$del_normal, 'id' => $id])
  130. ->append(['modular'])
  131. ->withAttr('modular', function ($val, $data) {
  132. return Db::name('theme_modular')
  133. ->field(true)
  134. ->where(['is_del' => CommonModel::$del_normal, 'theme_id' => $data['id']])
  135. ->order(['addtime' => 'desc', 'id' => 'desc'])
  136. ->append(['data'])
  137. ->withAttr('data', function ($v, $d) {
  138. return Db::name('theme_modular_data')
  139. ->field(true)
  140. ->where(['is_del' => CommonModel::$del_normal, 'theme_modular_id' => $d['id']])
  141. ->order(['addtime' => 'desc', 'id' => 'desc'])
  142. ->select()
  143. ->toArray();
  144. })
  145. ->select()
  146. ->toArray();
  147. })
  148. ->findOrEmpty()
  149. ->toArray();
  150. return empty($rs) ? json_show(CommonModel::$error_param, '该手机主题不存在') : json_show(CommonModel::$success, '获取手机主题详情成功', $rs);
  151. }
  152. //修改
  153. public static function edit(array $data = []): Json
  154. {
  155. Db::startTrans();
  156. try {
  157. $rs = ThemeModel::field('id,code,group_id')
  158. ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['id']])
  159. ->findOrEmpty();
  160. if ($rs->isEmpty()) throw new Exception('该主题不存在');
  161. if ($rs->code != $data['code']) {
  162. //校验code是否重复
  163. $temp = ThemeModel::field('id')
  164. ->where(['is_del' => CommonModel::$del_normal, 'code' => $data['code']])
  165. ->findOrEmpty()
  166. ->isEmpty();
  167. if (!$temp) throw new Exception('该编码已存在');
  168. }
  169. if ($rs->group_id != $data['group_id']) {
  170. //校验分组id是否存在
  171. $temp = GroupModel::field('id')
  172. ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['group_id']])
  173. ->findOrEmpty()
  174. ->isEmpty();
  175. if ($temp) throw new Exception('该分组不存在');
  176. //校验分组id是否重复
  177. $temp = ThemeModel::field('id')
  178. ->where(['is_del' => CommonModel::$del_normal, 'group_id' => $data['group_id']])
  179. ->findOrEmpty()
  180. ->isEmpty();
  181. if (!$temp) throw new Exception('该分组下手机主题重复');
  182. }
  183. $date = date('Y-m-d H:i:s');
  184. //手机主题
  185. ThemeModel::where(['is_del' => CommonModel::$del_normal, 'id' => $data['id']])->save([
  186. 'group_id' => $data['group_id'],
  187. 'code' => $data['code'],
  188. 'updaterid' => self::$uid,
  189. 'updater' => self::$uname,
  190. 'updatetime' => $date,
  191. ]);
  192. $all_theme_modular_id = Db::name('theme_modular')
  193. ->where([
  194. 'theme_id' => $data['id'],
  195. 'is_del' => CommonModel::$del_normal,
  196. ])->column('id');
  197. Db::name('theme_modular')
  198. ->where('is_del', CommonModel::$del_normal)
  199. ->whereIn('id', $all_theme_modular_id)
  200. ->update([
  201. 'is_del' => CommonModel::$del_deleted,
  202. 'updatetime' => $date,
  203. ]);
  204. Db::name('theme_modular_data')
  205. ->where('is_del', CommonModel::$del_normal)
  206. ->whereIn('theme_modular_id', $all_theme_modular_id)
  207. ->update([
  208. 'is_del' => CommonModel::$del_deleted,
  209. 'updatetime' => $date,
  210. ]);
  211. //模块验证规则
  212. $val_modular = Validate::rule([
  213. 'type|模块类型' => 'require|number|in:' . ThemeModel::$type_advantage . ',' . ThemeModel::$type_banner . ',' . ThemeModel::$type_exhibition . ',' . ThemeModel::$type_propaganda,
  214. 'status|状态' => 'require|number|in:' . CommonModel::$status_normal . ',' . CommonModel::$status_disable,
  215. 'title|模块名称' => 'requireIf:type,' . ThemeModel::$type_advantage . '|requireIf:type,' . ThemeModel::$type_exhibition . '|max:255',
  216. 'data|模块数据' => 'require|array|max:100',
  217. ]);
  218. //数据验证规则
  219. $val_data = Validate::rule([
  220. 'img|图片' => 'require|max:255',
  221. 'jump_type|跳转类型' => 'require|number|in:' . ThemeModel::$jump_type_external . ',' . ThemeModel::$jump_type_inside . ',' . ThemeModel::$jump_type_no,
  222. 'jump_param|跳转参数' => 'max:255',
  223. 'good_id|商品id' => 'number|egt:0',
  224. ]);
  225. foreach ($data['modular'] as $modular) {
  226. if (!$val_modular->check($modular)) throw new Exception('模块参数有误,' . $val_modular->getError());
  227. //模块记录表
  228. $modular_id = Db::name('theme_modular')->insertGetId([
  229. 'theme_id' => $data['id'],
  230. 'title' => $modular['title'],
  231. 'type' => $modular['type'],
  232. 'status' => $modular['status'],
  233. 'is_del' => CommonModel::$del_normal,
  234. 'addtime' => $date,
  235. 'updatetime' => $date,
  236. ]);
  237. $insert_data = [];
  238. $da = $modular['data'];
  239. $goods = GoodModel::whereIn('id', array_column($da, 'good_id'))
  240. ->where('is_del', CommonModel::$del_normal)
  241. ->column('good_name', 'id');
  242. foreach ($da as &$v) {
  243. if ($modular['type'] == ThemeModel::$type_exhibition) $val_data->append('good_id', 'require');
  244. if (!$val_data->check($v)) throw new Exception('模块数据参数有误,' . $val_data->getError());
  245. $insert_data[] = [
  246. 'theme_modular_id' => $modular_id,
  247. 'img' => $v['img'],
  248. 'jump_type' => $v['jump_type'],
  249. 'jump_param' => $v['jump_param'],
  250. 'title' => $v['title'] ?? '',
  251. 'good_id' => $v['good_id'],
  252. 'style_type' => $v['style_type'],
  253. 'good_name' => $modular['type'] == ThemeModel::$type_exhibition ? $goods[$v['good_id']] ?? '' : '',
  254. 'is_del' => CommonModel::$del_normal,
  255. 'addtime' => $date,
  256. 'updatetime' => $date,
  257. ];
  258. }
  259. //模块数据记录表
  260. Db::name('theme_modular_data')->insertAll($insert_data);
  261. }
  262. Db::commit();
  263. return json_show(CommonModel::$success, '修改手机主题成功');
  264. } catch (Exception $exception) {
  265. Db::rollback();
  266. return json_show(CommonModel::$error_param, '修改手机主题失败,' . $exception->getMessage());
  267. }
  268. }
  269. //启禁用
  270. public static function status(array $data = []): Json
  271. {
  272. Db::startTrans();
  273. try {
  274. $where = [
  275. ['id', '=', $data['id']],
  276. ['is_del', '=', CommonModel::$del_normal],
  277. ['status', '<>', $data['status']],
  278. ];
  279. $date = date('Y-m-d H:i:s');
  280. $res = ThemeModel::where($where)->save(array_merge($data, ['updater' => self::$uname, 'updaterid' => self::$uid, 'updatetime' => $date]));
  281. if (!$res) throw new Exception('该手机主题不存在或重复操作');
  282. Db::name('theme_modular')
  283. ->where(['is_del' => CommonModel::$del_normal, 'theme_id' => $data['id']])
  284. ->update(['status' => $data['status'], 'updatetime' => $date]);
  285. Db::commit();
  286. return json_show(CommonModel::$success, '操作成功');
  287. } catch (Exception $exception) {
  288. Db::rollback();
  289. return json_show(CommonModel::$error_param, '操作失败,' . $exception->getMessage());
  290. }
  291. }
  292. }