GoodLogic.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace app\admin\logic;
  3. use app\model\CommonModel;
  4. use app\model\GoodModel;
  5. use think\exception\ValidateException;
  6. use think\response\Json;
  7. class GoodLogic extends BaseLogic
  8. {
  9. //获取商品列表
  10. public static function list(array $data = []): Json
  11. {
  12. $db = GoodModel::alias('a')
  13. ->leftJoin('unit b', 'b.id=a.unit_id AND b.is_del=' . CommonModel::$del_normal)
  14. ->where('a.is_del', CommonModel::$del_normal);
  15. if ($data['good_code'] !== '') $db->whereLike('a.good_code', '%' . $data['good_code'] . '%');
  16. if ($data['good_name']) $db->whereLike('a.good_name', '%' . $data['good_name'] . '%');
  17. if ($data['status'] != '') $db->where('a.status', $data['status']);
  18. if ($data['type'] != '') $db->where('a.type', $data['type']);
  19. $count = $db->count('a.id');
  20. $list = $db
  21. ->field('a.id,a.good_cover_img,a.good_code,a.good_name,a.moq,a.step,b.unit,a.good_remark,a.type,a.price,a.status,a.creater,a.addtime')
  22. ->page($data['page'], $data['size'])
  23. ->order('a.addtime desc')
  24. ->select()
  25. ->toArray();
  26. return json_show(CommonModel::$success, '获取商品列表成功', ['list' => $list, 'count' => $count]);
  27. }
  28. //添加商品
  29. public static function add(array $data = []): Json
  30. {
  31. $rs = GoodModel::field('id')
  32. ->where(['is_del' => CommonModel::$del_normal, 'good_name' => $data['good_name']])
  33. ->findOrEmpty()
  34. ->isEmpty();
  35. if (!$rs) throw new ValidateException('该商品名称已存在');
  36. $good_code = make_no("SP");
  37. $res = GoodModel::create([
  38. 'good_cover_img' => $data['good_cover_img'],
  39. 'good_code' => $good_code,
  40. 'good_name' => $data['good_name'],
  41. 'moq' => $data['moq'],
  42. 'step' => $data['step'],
  43. 'type' => $data['type'],
  44. 'price' => $data['price'],
  45. 'good_banner_img' => implode(',', $data['good_banner_img']),
  46. 'good_img' => implode(',', $data['good_img']),
  47. 'good_param' => json_encode($data['good_param'], JSON_UNESCAPED_UNICODE),
  48. 'good_remark' => $data['good_remark'],
  49. 'unit_id' => $data['unit_id'],
  50. 'is_del' => CommonModel::$del_normal,
  51. 'status' => CommonModel::$status_normal,
  52. 'createrid' => self::$uid,
  53. 'creater' => self::$uname,
  54. 'addtime' => date("Y-m-d H:i:s"),
  55. 'updaterid' => self::$uid,
  56. 'updater' => self::$uname,
  57. 'updatetime' => date("Y-m-d H:i:s"),
  58. ])->save();
  59. return $res ? json_show(CommonModel::$success, '新建商品成功') : json_show(CommonModel::$error_param, '新建商品失败');
  60. }
  61. //读取商品详情
  62. public static function read(int $id = 0): Json
  63. {
  64. $rs = GoodModel::field(true)
  65. ->where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  66. ->withAttr('good_banner_img', function ($val) {
  67. return explode(',', $val);
  68. })->withAttr('good_img', function ($val) {
  69. return explode(',', $val);
  70. })->withAttr('good_param', function ($val) {
  71. return json_decode($val);
  72. })
  73. ->findOrEmpty()
  74. ->toArray();
  75. return json_show(CommonModel::$success, '获取商品详情成功', $rs);
  76. }
  77. //编辑商品
  78. public static function edit(array $data = []): Json
  79. {
  80. $rs = GoodModel::field('id,good_name')
  81. ->where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  82. ->findOrEmpty();
  83. if ($rs->isEmpty()) throw new ValidateException('该商品不存在');
  84. //修改了商品名称,检查商品名称是否重复
  85. if ($rs->good_name != $data['good_name']) {
  86. $temp = GoodModel::field('id')
  87. ->where(['is_del' => CommonModel::$del_normal, 'good_name' => $data['good_name']])
  88. ->findOrEmpty()
  89. ->isEmpty();
  90. if (!$temp) throw new ValidateException('该商品名称重复');
  91. }
  92. $res = GoodModel::where(['id' => $data['id']])->save([
  93. 'good_cover_img' => $data['good_cover_img'],
  94. 'good_name' => $data['good_name'],
  95. 'moq' => $data['moq'],
  96. 'step' => $data['step'],
  97. 'price' => $data['price'],
  98. 'good_banner_img' => implode(',', $data['good_banner_img']),
  99. 'good_img' => implode(',', $data['good_img']),
  100. 'good_param' => json_encode($data['good_param'], JSON_UNESCAPED_UNICODE),
  101. 'good_remark' => $data['good_remark'],
  102. 'unit_id' => $data['unit_id'],
  103. 'updaterid' => self::$uid,
  104. 'updater' => self::$uname,
  105. 'updatetime' => date("Y-m-d H:i:s"),
  106. ]);
  107. return $res ? json_show(CommonModel::$success, '编辑商品成功') : json_show(CommonModel::$error_param, '编辑商品失败');
  108. }
  109. //商品启禁用
  110. public static function status(array $data = []): Json
  111. {
  112. $where = [
  113. ['id', '=', $data['id']],
  114. ['is_del', '=', CommonModel::$del_normal],
  115. ['status', '<>', $data['status']],
  116. ];
  117. $rs = GoodModel::field('id,status')
  118. ->where($where)
  119. ->findOrEmpty()
  120. ->isEmpty();
  121. if ($rs) throw new ValidateException('该商品不存在或重复操作');
  122. $res = GoodModel::where($where)->save($data);
  123. return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$error_param, '操作失败');
  124. }
  125. //删除商品
  126. public static function delete(int $id = 0): Json
  127. {
  128. $rs = GoodModel::where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  129. ->save(['is_del' => CommonModel::$del_deleted]);
  130. return $rs ? json_show(CommonModel::$success, '商品删除成功') : json_show(CommonModel::$error_param, '商品不存在或重复删除');
  131. }
  132. }