GroupItemLogic.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace app\admin\logic;
  3. use app\model\CommonModel;
  4. use app\model\GroupModel;
  5. use app\model\GoodModel;
  6. use app\model\VideoModel;
  7. use think\Exception;
  8. use think\exception\ValidateException;
  9. use think\facade\Db;
  10. use think\response\Json;
  11. class GroupItemLogic extends BaseLogic
  12. {
  13. //列表
  14. public static function list(array $data = []): Json
  15. {
  16. $db = Db::name('group_item')
  17. ->alias('a')
  18. ->where(['a.is_del' => CommonModel::$del_normal, 'group_id' => $data['group_id']]);
  19. if ($data['type'] == GroupModel::$type_good) {
  20. $db->leftJoin('good b', 'b.id=a.item_id AND b.is_del=' . CommonModel::$del_normal);
  21. $name = 'good_name';
  22. $field = 'a.id,b.good_code code,b.good_name title,b.good_remark remark,a.is_top,a.weight';
  23. } else {
  24. $db->leftJoin('video b', 'b.id=a.item_id AND b.is_del=' . CommonModel::$del_normal);
  25. $name = 'video_name';
  26. $field = 'a.id,b.video_sn code,b.video_name title,b.remark,a.is_top,a.weight';
  27. }
  28. if ($data['title'] != '') $db->whereLike($name, '%' . $data['title'] . '%');
  29. $count = $db->count('a.id');
  30. $list = $db->field($field)
  31. ->page($data['page'], $data['size'])
  32. ->order(['a.weight' => 'desc', 'a.id' => 'desc'])
  33. ->select()
  34. ->toArray();
  35. return json_show(CommonModel::$success, '获取分组明细列表成功', ['count' => $count, 'list' => $list]);
  36. }
  37. //添加
  38. public static function add(array $data = []): Json
  39. {
  40. Db::startTrans();
  41. try {
  42. $rs = GroupModel::field('id')
  43. ->where(['id' => $data['group_id'], 'is_del' => CommonModel::$del_normal])
  44. ->findOrEmpty()
  45. ->isEmpty();
  46. if ($rs) throw new Exception('该分组不存在');
  47. if ($data['type'] == GroupModel::$type_good) {
  48. $rs = GoodModel::field('id')
  49. ->where(['id' => $data['item_id'], 'is_del' => CommonModel::$del_normal])
  50. ->findOrEmpty()
  51. ->isEmpty();
  52. if ($rs) throw new Exception('该商品不存在');
  53. Db::name('group')
  54. ->where(['id' => $data['group_id'], 'is_del' => CommonModel::$del_normal])
  55. ->inc('good_num', 1)
  56. ->update(['updatetime' => date('Y-m-d H:i:s')]);
  57. } else {
  58. $rs = VideoModel::field('id')
  59. ->where(['id' => $data['item_id'], 'is_del' => CommonModel::$del_normal])
  60. ->findOrEmpty()
  61. ->isEmpty();
  62. if ($rs) throw new Exception('该视频不存在');
  63. Db::name('group')
  64. ->where(['id' => $data['group_id'], 'is_del' => CommonModel::$del_normal])
  65. ->inc('video_num', 1)
  66. ->update(['updatetime' => date('Y-m-d H:i:s')]);
  67. }
  68. $rs = Db::name('group_item')
  69. ->field('id')
  70. ->where([
  71. 'is_del' => CommonModel::$del_normal,
  72. 'group_id' => $data['group_id'],
  73. 'type' => $data['type'],
  74. 'item_id' => $data['item_id']
  75. ])
  76. ->findOrEmpty();
  77. if (!empty($rs)) throw new Exception('该明细已存在,不能在同一分组下重复添加');
  78. $res = Db::name('group_item')
  79. ->insert(array_merge($data, [
  80. 'is_del' => CommonModel::$del_normal,
  81. 'addtime' => date('Y-m-d H:i:s'),
  82. 'updatetime' => date('Y-m-d H:i:s'),
  83. ]));
  84. Db::commit();
  85. return $res ? json_show(CommonModel::$success, '添加分组明细成功') : json_show(CommonModel::$success, '添加分组明细失败');
  86. } catch (Exception $exception) {
  87. Db::rollback();
  88. return json_show(CommonModel::$error_param, '添加分组明细失败,' . $exception->getMessage());
  89. }
  90. }
  91. //获取详情
  92. public static function read(int $id = 0): Json
  93. {
  94. $res = Db::name('group_item')
  95. ->field(true)
  96. ->where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  97. ->findOrEmpty();
  98. if (empty($res)) throw new ValidateException('该分组明细为空');
  99. return json_show(CommonModel::$success, '获取分组明细详情成功', $res);
  100. }
  101. //编辑
  102. public static function edit(array $data = []): Json
  103. {
  104. $rs = Db::name('group_item')
  105. ->field('id,group_id,type,item_id')
  106. ->where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  107. ->findOrEmpty();
  108. if (empty($rs)) return json_show(CommonModel::$error_param, '该分组明细不存在');
  109. if ($rs['item_id'] != $data['item_id']) {
  110. $temp = Db::name('group_item')
  111. ->field('id,group_id,type,item_id')
  112. ->where([
  113. 'is_del' => CommonModel::$del_normal,
  114. 'group_id' => $rs['group_id'],
  115. 'type' => $rs['type'],
  116. 'item_id' => $data['item_id']
  117. ])->findOrEmpty();
  118. if (!empty($temp)) return json_show(CommonModel::$error_param, '该明细已存在,不能在同一分组下重复添加');
  119. }
  120. if ($rs['type'] == GroupModel::$type_good) {
  121. $result = GoodModel::field('id')
  122. ->where(['id' => $data['item_id'], 'is_del' => CommonModel::$del_normal])
  123. ->findOrEmpty()
  124. ->isEmpty();
  125. if ($result) return json_show(CommonModel::$error_param, '该商品不存在');
  126. } else {
  127. $result = VideoModel::field('id')
  128. ->where(['id' => $data['item_id'], 'is_del' => CommonModel::$del_normal])
  129. ->findOrEmpty()
  130. ->isEmpty();
  131. if ($result) return json_show(CommonModel::$error_param, '该视频不存在');
  132. }
  133. $res = Db::name('group_item')
  134. ->where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  135. ->save(array_merge($data, ['updatetime' => date('Y-m-d H:i:s')]));
  136. return $res ? json_show(CommonModel::$success, '编辑分组明细成功') : json_show(CommonModel::$error_param, '编辑分组明细失败');
  137. }
  138. //删除
  139. public static function delete(array $ids = []): Json
  140. {
  141. Db::startTrans();
  142. try {
  143. $group_id = Db::name('group_item')
  144. ->whereIn('id', $ids)
  145. ->where('is_del', CommonModel::$del_normal)
  146. ->group('group_id,type')
  147. ->column('group_id,type');
  148. if (count($group_id) > 1) throw new Exception('不能删除不同分组下不同类型的明细');
  149. $num = Db::name('group_item')
  150. ->whereIn('id', $ids)
  151. ->where('is_del', CommonModel::$del_normal)
  152. ->update([
  153. 'is_del' => CommonModel::$del_deleted,
  154. 'updatetime' => date('Y-m-d H:i:s'),
  155. ]);
  156. if ($group_id[0]['type'] == GroupModel::$type_good) $field = 'good_num';
  157. else $field = 'video_num';
  158. Db::name('group')
  159. ->where(['id' => $group_id[0]['group_id'], 'is_del' => CommonModel::$del_normal])
  160. ->dec($field, $num)
  161. ->update([
  162. 'updatetime' => date('Y-m-d H:i:s'),
  163. 'updaterid' => self::$uid,
  164. 'updater' => self::$uname
  165. ]);
  166. Db::commit();
  167. return $num ? json_show(CommonModel::$success, '删除成功') : json_show(CommonModel::$error_param, '删除失败,该分组明细不存在');
  168. } catch (Exception $exception) {
  169. Db::rollback();
  170. return json_show(CommonModel::$error_param, '删除分组明细失败,' . $exception->getMessage());
  171. }
  172. }
  173. //置顶
  174. public static function top(array $data = []): Json
  175. {
  176. $res = Db::name('group_item')
  177. ->where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  178. ->where('is_top', '<>', $data['is_top'])
  179. ->update(['is_top' => $data['is_top'], 'updatetime' => date('Y-m-d H:i:s')]);
  180. return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$error_param, '操作失败,该明细不存在或重复操作');
  181. }
  182. }