DiscountLogic.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\marketing;
  20. use app\common\enum\DiscountEnum;
  21. use app\common\enum\GoodsEnum;
  22. use app\common\model\DiscountGoods;
  23. use app\common\model\DiscountGoodsItem;
  24. use app\common\model\Goods;
  25. use app\common\model\GoodsItem;
  26. use app\common\model\UserLevel;
  27. use think\Exception;
  28. use think\facade\Db;
  29. /**
  30. * 会员折扣逻辑层
  31. * Class DiscountLogic
  32. * @package app\adminapi\logic\marketing
  33. */
  34. class DiscountLogic
  35. {
  36. /**
  37. * @notes 返回其他状态列表
  38. * @return array
  39. * @author cjhao
  40. * @date 2022/5/9 18:56
  41. */
  42. public function otherLists()
  43. {
  44. return [
  45. 'status_list' => GoodsEnum::getStatusDesc(),
  46. 'type_list' => GoodsEnum::getGoodsTypeDesc(),
  47. 'discount_list' => DiscountEnum::getDiscountStatusDesc(),
  48. ];
  49. }
  50. /**
  51. * @notes 参与活动
  52. * @param $params
  53. * @return bool
  54. * @throws \Exception
  55. * @author cjhao
  56. * @date 2022/5/5 16:56
  57. */
  58. public function join($params)
  59. {
  60. $goods = Goods::column('id');
  61. $discountGoods = DiscountGoods::where(['goods_id' => $params['goods_ids']])->column('goods_id');
  62. $updateData = [];
  63. $addData = [];
  64. foreach ($params['goods_ids'] as $goodsId) {
  65. if (!in_array($goodsId, $goods)) {
  66. continue;
  67. }
  68. //更新
  69. if (in_array($goodsId, $discountGoods)) {
  70. $updateData[] = $goodsId;
  71. } else {
  72. //写入
  73. $addData[] = [
  74. 'goods_id' => $goodsId,
  75. 'is_discount' => 1,
  76. 'discount_rule' => 1,
  77. ];
  78. }
  79. }
  80. if ($updateData) {
  81. DiscountGoods::where(['goods_id' => $updateData])->update(['is_discount' => 1, 'update_time' => time()]);
  82. }
  83. if ($addData) {
  84. (new DiscountGoods)->saveAll($addData);
  85. }
  86. return true;
  87. }
  88. /**
  89. * @notes 退出折扣活动
  90. * @param $params
  91. * @return bool
  92. * @author cjhao
  93. * @date 2022/5/5 17:13
  94. */
  95. public function quit($params)
  96. {
  97. DiscountGoods::where(['goods_id' => $params['goods_ids']])->update(['is_discount' => 0, 'update_time' => time()]);
  98. return true;
  99. }
  100. /**
  101. * @notes 设置折扣详情
  102. * @param $params
  103. * @return array
  104. * @throws \think\db\exception\DataNotFoundException
  105. * @throws \think\db\exception\DbException
  106. * @throws \think\db\exception\ModelNotFoundException
  107. * @author cjhao
  108. * @date 2022/5/6 18:31
  109. */
  110. public function detail($params)
  111. {
  112. //商品
  113. $goods = Goods::where(['id' => $params['goods_id']])->with('spec_value_list')->field('id,name,code,image,spec_type')->find();
  114. //折扣商品
  115. $discountGoods = DiscountGoods::where(['goods_id' => $params['goods_id']])->find();
  116. //合成会员等级和商品规格数据
  117. $levelGoodsItem = \app\common\logic\DiscountLogic::leveGoodsItem($goods);
  118. return [
  119. 'goods_id' => $goods->id,
  120. 'name' => $goods->name,
  121. 'image' => $goods->image,
  122. 'code' => $goods->code,
  123. 'spec_type' => $goods->spec_type,
  124. 'is_discount' => $discountGoods->is_discount ?? 0,
  125. 'discount_rule' => $discountGoods->discount_rule ?? 1,
  126. 'level_goods_item' => $levelGoodsItem,
  127. ];
  128. }
  129. /**
  130. * @notes 设置会员价
  131. * @param $params
  132. * @return bool|string
  133. * @author cjhao
  134. * @date 2022/5/7 9:25
  135. */
  136. public function setDiscount($params)
  137. {
  138. Db::startTrans();
  139. try {
  140. $discountGoods = DiscountGoods::where(['goods_id' => $params['goods_id']])->find();
  141. $discountGoods || $discountGoods = new DiscountGoods();
  142. $discountGoods->goods_id = $params['goods_id'];
  143. $discountGoods->is_discount = $params['is_discount'];
  144. $discountGoods->discount_rule = $params['discount_rule'];
  145. $discountGoods->save();
  146. //参与会员折扣,按会员等级折扣
  147. if (DiscountEnum::DISCOUNT_RULE_LEVEL == $params['discount_rule']) {
  148. //不删除会员折扣商品表,直接提交数据
  149. Db::commit();
  150. return true;
  151. }
  152. //自定义会员价
  153. $goodsItemList = GoodsItem::where(['goods_id' => $params['goods_id']])->column('spec_value_str,sell_price', 'id');
  154. $userLevel = UserLevel::column('rank','id');
  155. $userLevelIds = array_keys($userLevel);
  156. $goodsItemIds = array_keys($goodsItemList);
  157. //验证数据
  158. $data = [];
  159. foreach ($params['level_goods_item'] as $level) {
  160. if (!isset($level['id']) || empty($level['id'])) {
  161. throw new Exception('数据错误');
  162. }
  163. //会员等级不存在,不处理
  164. if (!in_array($level['id'], $userLevelIds)) {
  165. continue;
  166. }
  167. foreach ($level['goods_item'] as $goodsItem) {
  168. //规格不存在,不处理
  169. if (!in_array($goodsItem['item_id'], $goodsItemIds)) {
  170. continue;
  171. }
  172. $specValueStr = $goodsItemList[$goodsItem['item_id']]['spec_value_str'] ?? '';
  173. if (!isset($goodsItem['discount_price']) || $goodsItem['discount_price'] < 0) {
  174. throw new Exception('请输入商品规格:' . $specValueStr . '的会员价');
  175. }
  176. $sellPrice = $goodsItemList[$goodsItem['item_id']]['sell_price'] ?? 0;
  177. if ($goodsItem['discount_price'] > $sellPrice) {
  178. throw new Exception('商品规格:' . $specValueStr . '的会员价不能高于售价');
  179. }
  180. $data[] = [
  181. 'goods_id' => $params['goods_id'],
  182. 'item_id' => $goodsItem['item_id'],
  183. 'level' => $level['id'],
  184. 'discount_price' => $goodsItem['discount_price'],
  185. ];
  186. }
  187. }
  188. if (empty($data)) {
  189. throw new Exception('数据错误,请刷新页面');
  190. }
  191. //先清除之前的数据
  192. DiscountGoodsItem::where(['goods_id' => $params['goods_id']])->delete();
  193. $discountGoods->discountGoodsItem()->saveAll($data);
  194. Db::commit();
  195. return true;
  196. } catch (Exception $e) {
  197. Db::rollback();
  198. return $e->getMessage();
  199. }
  200. }
  201. }