ThemeLogic.php 15 KB

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