CompanyGoodLogic.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace app\admin\logic;
  3. use app\model\CommonModel;
  4. use app\model\CompanyGoodModel;
  5. use app\model\GroupModel;
  6. use app\model\GoodModel;
  7. use app\model\VideoModel;
  8. use think\Exception;
  9. use think\exception\ValidateException;
  10. use think\facade\Db;
  11. use think\response\Json;
  12. class CompanyGoodLogic extends BaseLogic
  13. {
  14. //列表
  15. public static function list(array $data = []): Json
  16. {
  17. $db = CompanyGoodModel::alias('a')
  18. ->leftJoin('good b', 'b.id=a.good_id AND b.is_del=' . CommonModel::$del_normal)
  19. ->leftJoin('group c', 'c.id=a.group_id AND c.is_del=' . CommonModel::$del_normal)
  20. ->leftJoin('company d', 'd.id=c.company_id AND d.is_del=' . CommonModel::$del_normal)
  21. ->leftJoin('card e', 'e.id=c.card_id AND e.is_del=' . CommonModel::$del_normal)
  22. ->leftJoin('unit f', 'f.id=b.unit_id AND f.is_del=' . CommonModel::$del_normal)
  23. ->where(['a.is_del' => CommonModel::$del_normal]);
  24. if ($data['group_id'] != '') $db->where('a.group_id', $data['group_id']);
  25. if ($data['good_name'] != '') $db->whereLike('b.good_name', '%' . $data['good_name'] . '%');
  26. if ($data['good_code'] != '') $db->whereLike('b.good_code', '%' . $data['good_code'] . '%');
  27. if ($data['status'] != '') $db->where('a.status', $data['status']);
  28. if ($data['type'] != '') $db->where('b.type', $data['type']);
  29. $count = $db->count('a.id');
  30. $list = $db->field('a.id,d.title company_title,e.title card_title,a.code,b.good_code,b.good_name,b.good_cover_img,b.moq,b.type,b.step,f.unit,b.price,a.status,a.creater,a.addtime')
  31. ->page($data['page'], $data['size'])
  32. ->order(['a.is_top' => 'desc', '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. $data['good_id'] = GoodModel::where('is_del', CommonModel::$del_normal)
  48. ->whereIn('id', $data['good_id'])
  49. ->column('id');
  50. if (empty($data['good_id'])) throw new Exception('该商品不存在');
  51. $rs = CompanyGoodModel::field('a.id,a.good_id,b.good_name,b.good_code')
  52. ->alias('a')
  53. ->leftJoin('good b', 'b.id=a.good_id AND b.is_del=' . CommonModel::$del_normal)
  54. ->where(['a.is_del' => CommonModel::$del_normal, 'a.group_id' => $data['group_id']])
  55. ->whereIn('a.good_id', $data['good_id'])
  56. ->findOrEmpty()
  57. ->toArray();
  58. if (!empty($rs)) throw new Exception("【{$rs['good_code']}】【{$rs['good_name']}】该商品重复添加");
  59. $insert = [];
  60. $date = date('Y-m-d H:i:s');
  61. foreach ($data['good_id'] as $good_id) {
  62. $insert[] = [
  63. 'group_id' => $data['group_id'],
  64. 'code' => make_no('CG'),
  65. 'good_id' => $good_id,
  66. 'is_del' => CommonModel::$del_normal,
  67. 'status' => CommonModel::$status_normal,
  68. 'is_top' => CommonModel::$top_no,
  69. 'weight' => 0,
  70. 'creater' => self::$uname,
  71. 'createrid' => self::$uid,
  72. 'addtime' => $date,
  73. 'updater' => self::$uname,
  74. 'updaterid' => self::$uid,
  75. 'updatetime' => $date,
  76. ];
  77. }
  78. $res = Db::name('company_good')->insertAll($insert);
  79. Db::commit();
  80. if ($res) return json_show(CommonModel::$success, '添加公司商品成功');
  81. else throw new Exception();
  82. } catch (Exception $exception) {
  83. Db::rollback();
  84. return json_show(CommonModel::$error_param, '添加公司商品失败,' . $exception->getMessage());
  85. }
  86. }
  87. //获取详情
  88. public static function read(int $id = 0): Json
  89. {
  90. $res = CompanyGoodModel::field(true)
  91. ->where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  92. ->findOrEmpty()
  93. ->toArray();
  94. return empty($res) ? json_show(CommonModel::$error_param, '该公司商品为空') : json_show(CommonModel::$success, '获取公司商品详情成功', $res);
  95. }
  96. //编辑
  97. public static function edit(array $data = []): Json
  98. {
  99. $rs = CompanyGoodModel::field('id')
  100. ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['id']])
  101. ->findOrEmpty()
  102. ->isEmpty();
  103. if ($rs) return json_show(CommonModel::$error_param, '该公司商品不存在');
  104. $rs = GroupModel::field('id')
  105. ->where(['id' => $data['group_id'], 'is_del' => CommonModel::$del_normal])
  106. ->findOrEmpty()
  107. ->isEmpty();
  108. if ($rs) return json_show(CommonModel::$error_param, '该分组不存在');
  109. $rs = GoodModel::field('id')
  110. ->where(['id' => $data['good_id'], 'is_del' => CommonModel::$del_normal])
  111. ->findOrEmpty()
  112. ->isEmpty();
  113. if ($rs) return json_show(CommonModel::$error_param, '该商品不存在');
  114. $rs = CompanyGoodModel::field('id')
  115. ->where(['is_del' => CommonModel::$del_normal, 'group_id' => $data['group_id'], 'good_id' => $data['good_id']])
  116. ->where('id', '<>', $data['id'])
  117. ->findOrEmpty()
  118. ->isEmpty();
  119. if (!$rs) return json_show(CommonModel::$error_param, '该公司商品已存在');
  120. $res = CompanyGoodModel::where('id', $data['id'])->save((array_merge($data, [
  121. 'updater' => self::$uname,
  122. 'updaterid' => self::$uid,
  123. 'updatetime' => date('Y-m-d H:i:s'),
  124. ])));
  125. return $res ? json_show(CommonModel::$success, '编辑公司商品成功') : json_show(CommonModel::$error_param, '编辑公司商品失败');
  126. }
  127. //删除
  128. public static function delete(array $ids = []): Json
  129. {
  130. $res = CompanyGoodModel::whereIn('id', $ids)
  131. ->where('is_del', CommonModel::$del_normal)
  132. ->update([
  133. 'is_del' => CommonModel::$del_deleted,
  134. 'updatetime' => date('Y-m-d H:i:s'),
  135. 'updaterid' => self::$uid,
  136. 'updater' => self::$uname
  137. ]);
  138. return $res ? json_show(CommonModel::$success, '删除成功') : json_show(CommonModel::$error_param, '删除失败,该公司商品不存在');
  139. }
  140. //置顶
  141. public static function top(array $data = []): Json
  142. {
  143. $res = CompanyGoodModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  144. ->where('is_top', '<>', $data['is_top'])
  145. ->update([
  146. 'is_top' => $data['is_top'],
  147. 'updaterid' => self::$uid,
  148. 'updater' => self::$uname,
  149. 'updatetime' => date('Y-m-d H:i:s')
  150. ]);
  151. return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$error_param, '操作失败,该公司商品不存在或重复操作');
  152. }
  153. //启禁用
  154. public static function status(array $data = []): Json
  155. {
  156. $res = CompanyGoodModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  157. ->where('status', '<>', $data['status'])
  158. ->update([
  159. 'status' => $data['status'],
  160. 'updaterid' => self::$uid,
  161. 'updater' => self::$uname,
  162. 'updatetime' => date('Y-m-d H:i:s')
  163. ]);
  164. return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$error_param, '操作失败,该公司商品不存在或重复操作');
  165. }
  166. }