GoodGroupLogic.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace app\admin\logic;
  3. use app\model\CommonModel;
  4. use app\model\GoodGroupModel;
  5. use think\Exception;
  6. use think\exception\ValidateException;
  7. use think\facade\Db;
  8. use think\response\Json;
  9. class GoodGroupLogic extends BaseLogic
  10. {
  11. //获取商品分组列表
  12. public static function list(array $data = []): Json
  13. {
  14. $db = GoodGroupModel::alias('a')
  15. ->leftJoin('company b', 'b.id=a.company_id')
  16. ->leftJoin('card c', 'c.id=a.card_id')
  17. ->where('a.is_del', CommonModel::$del_normal);
  18. if ($data['status'] != '') $db->where('a.status', $data['status']);
  19. if ($data['company_id'] != '') $db->where('a.company_id', $data['company_id']);
  20. if ($data['card_id'] != '') $db->where('a.card_id', $data['card_id']);
  21. $count = $db->count('a.id');
  22. $list = $db->field('a.id,b.title company_title,c.title card_title,a.num,a.status,a.addtime')
  23. ->page($data['page'], $data['size'])
  24. ->order(['a.id' => 'desc'])
  25. ->select()
  26. ->toArray();
  27. return json_show(CommonModel::$success, '获取商品分组列表成功', ['count' => $count, 'list' => $list]);
  28. }
  29. //添加商品分组
  30. public static function add(array $data = []): Json
  31. {
  32. Db::startTrans();
  33. try {
  34. $rs = GoodGroupModel::field('id')
  35. ->where([
  36. 'is_del' => CommonModel::$del_normal,
  37. 'company_id' => $data['company_id'],
  38. 'card_id' => $data['card_id']
  39. ])
  40. ->findOrEmpty()
  41. ->isEmpty();
  42. if (!$rs) throw new Exception('该分组已存在');
  43. $date = date('Y-m-d H:i:s');
  44. $good_group_id = Db::name('good_group')
  45. ->strict(false)
  46. ->insertGetId(array_merge($data, [
  47. 'num' => count($data['good_list']),
  48. 'status' => CommonModel::$status_normal,
  49. 'is_del' => CommonModel::$del_normal,
  50. 'createrid' => self::$uid,
  51. 'creater' => self::$uname,
  52. 'addtime' => $date,
  53. 'updaterid' => self::$uid,
  54. 'updater' => self::$uname,
  55. 'updatetime' => $date,
  56. ]));
  57. $da = [];
  58. foreach ($data['good_list'] as $item) {
  59. $da[] = [
  60. 'good_group_id' => $good_group_id,
  61. 'good_id' => $item['good_id'],
  62. 'weight' => $item['weight'],
  63. 'is_del' => CommonModel::$del_normal,
  64. 'addtime' => $date,
  65. 'updatetime' => $date,
  66. ];
  67. }
  68. Db::name('good_group_item')->insertAll($da);
  69. Db::commit();
  70. return json_show(CommonModel::$success, '添加商品分组成功');
  71. } catch (Exception $exception) {
  72. Db::rollback();
  73. return json_show(CommonModel::$error_param, '添加商品分组失败,' . $exception->getMessage());
  74. }
  75. }
  76. //获取商品分组详情
  77. public static function read(int $id = 0): Json
  78. {
  79. $res = GoodGroupModel::field(true)
  80. ->where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  81. ->findOrEmpty()
  82. ->toArray();
  83. if (empty($res)) throw new ValidateException('该商品分组为空');
  84. $res['child'] = Db::name('good_group_item')
  85. ->alias('a')
  86. ->field('a.id,a.good_id,a.weight,b.good_name,b.good_code')
  87. ->leftJoin('good b','b.id=a.good_id AND b.is_del='.CommonModel::$del_normal)
  88. ->where(['a.is_del' => CommonModel::$del_normal, 'a.good_group_id' => $id])
  89. ->order(['a.weight' => 'desc', 'a.id' => 'desc'])
  90. ->select()
  91. ->toArray();
  92. return json_show(CommonModel::$success, '获取商品分组详情成功', $res);
  93. }
  94. //编辑商品分组
  95. public static function edit(array $data = []): Json
  96. {
  97. Db::startTrans();
  98. try {
  99. $rs = GoodGroupModel::field('id,company_id,card_id')
  100. ->where([
  101. 'is_del' => CommonModel::$del_normal,
  102. 'id' => $data['id']
  103. ])
  104. ->findOrEmpty();
  105. if ($rs->isEmpty()) throw new Exception('该商品分组不存在');
  106. if (($rs->company_id != $data['company_id']) || ($rs->card_id != $data['card_id'])) {
  107. $temp = GoodGroupModel::field('id')
  108. ->where([
  109. 'is_del' => CommonModel::$del_normal,
  110. 'company_id' => $data['company_id'],
  111. 'card_id' => $data['card_id']
  112. ])
  113. ->findOrEmpty()
  114. ->isEmpty();
  115. if (!$temp) throw new Exception('该分组已存在');
  116. }
  117. $date = date('Y-m-d H:i:s');
  118. GoodGroupModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  119. ->strict(false)
  120. ->save(array_merge($data, ['updatetime' => $date,'updaterid'=>self::$uid,'updater'=>self::$uid]));
  121. $del = $ins = [];
  122. foreach ($data['good_list'] as $item) {
  123. if (isset($item['id']) && $item['id'] != 0) {
  124. if ($item['is_del'] == CommonModel::$del_deleted) $del[] = $item['id'];
  125. else Db::name('good_group_item')->where('is_del', CommonModel::$del_normal)->where('id', $item['id'])->save(array_merge($item, ['updatetime' => $date]));
  126. } else $ins[] = [
  127. 'good_group_id' => $data['id'],
  128. 'good_id' => $item['good_id'],
  129. 'weight' => $item['weight'],
  130. 'is_del' => CommonModel::$del_normal,
  131. 'addtime' => $date,
  132. 'updatetime' => $date,
  133. ];
  134. }
  135. if ($del) Db::name('good_group_item')
  136. ->whereIn('id', $del)
  137. ->where('is_del', CommonModel::$del_normal)
  138. ->update([
  139. 'is_del' => CommonModel::$del_deleted,
  140. 'updatetime' => $date,
  141. ]);
  142. if ($ins) Db::name('good_group_item')->insertAll($ins);
  143. Db::commit();
  144. return json_show(CommonModel::$success, '编辑商品分组成功');
  145. } catch (Exception $exception) {
  146. Db::rollback();
  147. return json_show(CommonModel::$error_param, '编辑商品分组失败,' . $exception->getMessage());
  148. }
  149. }
  150. //商品分组启禁用
  151. public static function status(array $data = []): Json
  152. {
  153. $res = GoodGroupModel::where('id', $data['id'])
  154. ->where('status', '<>', $data['status'])
  155. ->save([
  156. 'status' => $data['status'],
  157. 'updatetime' => date('Y-m-d H:i:s'),
  158. ]);
  159. return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$success, '操作失败');
  160. }
  161. //删除商品分组
  162. public static function delete(int $id = 0): Json
  163. {
  164. $res = GoodGroupModel::where('id', $id)
  165. ->where('is_del', CommonModel::$del_normal)
  166. ->save([
  167. 'is_del' => CommonModel::$del_deleted,
  168. 'updatetime' => date('Y-m-d H:i:s'),
  169. ]);
  170. return $res ? json_show(CommonModel::$success, '删除成功') : json_show(CommonModel::$error_param, '删除失败');
  171. }
  172. }