GoodGroupItemLogic.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace app\admin\logic;
  3. use app\model\CommonModel;
  4. use app\model\GoodGroupModel;
  5. use app\model\GoodModel;
  6. use think\Exception;
  7. use think\exception\ValidateException;
  8. use think\facade\Db;
  9. use think\response\Json;
  10. class GoodGroupItemLogic extends BaseLogic
  11. {
  12. //列表
  13. public static function list(array $data = []): Json
  14. {
  15. $db = Db::name('good_group_item')
  16. ->alias('a')
  17. ->leftJoin('good b', 'b.id=a.good_id AND b.is_del=' . CommonModel::$del_normal)
  18. ->where(['a.is_del' => CommonModel::$del_normal, 'good_group_id' => $data['good_group_id']]);
  19. if ($data['good_title'] != '') $db->whereLike('b.good_name', '%' . $data['good_title'] . '%');
  20. $count = $db->count('a.id');
  21. $list = $db->field('a.id,b.good_code,b.good_name,a.weight')
  22. ->page($data['page'], $data['size'])
  23. ->order(['a.weight' => 'desc', 'a.id' => 'desc'])
  24. ->select()
  25. ->toArray();
  26. return json_show(CommonModel::$success, '获取商品分组明细列表成功', ['count' => $count, 'list' => $list]);
  27. }
  28. //添加
  29. public static function add(array $data = []): Json
  30. {
  31. Db::startTrans();
  32. try {
  33. $rs = GoodGroupModel::field('id')
  34. ->where(['id' => $data['good_group_id'], 'is_del' => CommonModel::$del_normal])
  35. ->findOrEmpty()
  36. ->isEmpty();
  37. if ($rs) throw new Exception('该商品分组不存在');
  38. $rs = GoodModel::field('id')
  39. ->where(['id' => $data['good_id'], 'is_del' => CommonModel::$del_normal])
  40. ->findOrEmpty()
  41. ->isEmpty();
  42. if ($rs) throw new Exception('该商品不存在');
  43. $rs = Db::name('good_group_item')
  44. ->field('id')
  45. ->where([
  46. 'is_del' => CommonModel::$del_normal,
  47. 'good_group_id' => $data['good_group_id'],
  48. 'good_id' => $data['good_id']])
  49. ->findOrEmpty();
  50. if (!empty($rs)) throw new Exception('该商品已存在,不能在同一商品分组下重复添加');
  51. $res = Db::name('good_group_item')
  52. ->insert(array_merge($data, [
  53. 'is_del' => CommonModel::$del_normal,
  54. 'addtime' => date('Y-m-d H:i:s'),
  55. 'updatetime' => date('Y-m-d H:i:s'),
  56. ]));
  57. Db::name('good_group')
  58. ->where(['id' => $data['good_group_id'], 'is_del' => CommonModel::$del_normal])
  59. ->inc('num', $res)
  60. ->update(['updatetime' => date('Y-m-d H:i:s')]);
  61. Db::commit();
  62. return $res ? json_show(CommonModel::$success, '添加商品分组明细成功') : json_show(CommonModel::$success, '添加商品分组明细失败');
  63. } catch (Exception $exception) {
  64. Db::rollback();
  65. return json_show(CommonModel::$error_param, '添加商品分组明细失败,' . $exception->getMessage());
  66. }
  67. }
  68. //获取详情
  69. public static function read(int $id = 0): Json
  70. {
  71. $res = Db::name('good_group_item')
  72. ->field(true)
  73. ->where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  74. ->findOrEmpty();
  75. if (empty($res)) throw new ValidateException('该商品分组明细为空');
  76. return json_show(CommonModel::$success, '获取商品分组明细详情成功', $res);
  77. }
  78. //编辑
  79. public static function edit(array $data = []): Json
  80. {
  81. $rs = GoodModel::field('id')
  82. ->where(['id' => $data['good_id'], 'is_del' => CommonModel::$del_normal])
  83. ->findOrEmpty()
  84. ->isEmpty();
  85. if ($rs) return json_show(CommonModel::$error_param, '该商品不存在');
  86. $res = Db::name('good_group_item')
  87. ->where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  88. ->save(array_merge($data, ['updatetime' => date('Y-m-d H:i:s')]));
  89. return $res ? json_show(CommonModel::$success, '编辑商品分组明细成功') : json_show(CommonModel::$error_param, '编辑商品分组明细失败');
  90. }
  91. //删除
  92. public static function delete(array $ids = []): Json
  93. {
  94. Db::startTrans();
  95. try {
  96. $good_group_id = Db::name('good_group_item')
  97. ->whereIn('id', $ids)
  98. ->where('is_del', CommonModel::$del_normal)
  99. ->group('good_group_id')
  100. ->column('good_group_id');
  101. if (count($good_group_id) > 1) throw new Exception('不能删除不同分组下的商品');
  102. $num = Db::name('good_group_item')
  103. ->whereIn('id', $ids)
  104. ->where('is_del', CommonModel::$del_normal)
  105. ->update([
  106. 'is_del' => CommonModel::$del_deleted,
  107. 'updatetime' => date('Y-m-d H:i:s'),
  108. ]);
  109. Db::name('good_group')
  110. ->where(['id' => $good_group_id[0], 'is_del' => CommonModel::$del_normal])
  111. ->dec('num', $num)
  112. ->update([
  113. 'updatetime' => date('Y-m-d H:i:s'),
  114. 'updaterid' => self::$uid,
  115. 'updater' => self::$uid
  116. ]);
  117. Db::commit();
  118. return $num ? json_show(CommonModel::$success, '删除成功') : json_show(CommonModel::$error_param, '删除失败,该商品分组明细不存在');
  119. } catch (Exception $exception) {
  120. Db::rollback();
  121. return json_show(CommonModel::$error_param, '删除商品分组明细失败,' . $exception->getMessage());
  122. }
  123. }
  124. }