GoodsLogic.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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\businessapi\logic;
  20. use app\common\enum\GoodsEnum;
  21. use app\common\logic\BaseLogic;
  22. use app\common\logic\GoodsActivityLogic;
  23. use app\common\model\Goods;
  24. use app\common\model\GoodsItem;
  25. /**
  26. * 商品逻辑层
  27. * Class GoodsLogic
  28. * @package app\businessapi\logic
  29. */
  30. class GoodsLogic extends BaseLogic
  31. {
  32. public function detail(int $id)
  33. {
  34. $goods = Goods::with(['spec_value.spec_list','spec_value_list'])
  35. ->field('id,name,type,code,image,video,video_cover,total_stock,click_num,virtual_sales_num+sales_num as sales_num,unit_id,spec_type,content,poster,virtual_click_num,express_type,express_money,is_express,is_selffetch,min_price,max_price')
  36. ->append(['goods_image'])
  37. ->find($id);
  38. if($goods->is_express){
  39. $goods->delivery_type = '快递配送 ';
  40. }
  41. if($goods->is_selffetch){
  42. $goods->delivery_type && $goods->delivery_type.='、';
  43. $goods->delivery_type .= '上门自提';
  44. }
  45. if(GoodsEnum::GOODS_VIRTUAL == $goods->type){
  46. $goods->delivery_type = '虚拟发货';
  47. }
  48. $goods->sell_price = $goods->spec_value_list[0]->sell_price;
  49. $goods->lineation_price = $goods->spec_value_list[0]->lineation_price;
  50. $goods->price = '¥' . $goods->min_price;
  51. if ($goods->min_price != $goods->max_price) {
  52. $goods->price = '¥' . $goods->min_price . '~' . '¥' . $goods->max_price;
  53. }
  54. //获取参与活动的商品
  55. $goodsActivityList = GoodsActivityLogic::activityInfo([$goods['id']]);
  56. $goods->click_num += $goods->virtual_click_num;
  57. $goods->unit_name = '';
  58. if($goods->unit_id){
  59. $goods->unit_name = $goods->unit->name;
  60. }
  61. $goods->hidden(['unit_id','unit']);
  62. //商品参加了哪些活动
  63. $goods->goods_activity = array_keys($goodsActivityList[$goods['id']] ?? []);
  64. return $goods->toArray();
  65. }
  66. /**
  67. * @notes 修改商品状态
  68. * @param int $id
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. * @author cjhao
  73. * @date 2023/2/16 10:34
  74. */
  75. public function status(int $id)
  76. {
  77. $goods = Goods::find($id);
  78. $goods->status = $goods->status ? 0 : 1;
  79. $goods->save();
  80. }
  81. /**
  82. * @notes 修改商品价格
  83. * @param $params
  84. * @return bool
  85. * @throws \Exception
  86. * @author cjhao
  87. * @date 2023/2/16 10:52
  88. */
  89. public function edit($params)
  90. {
  91. try{
  92. $goodsItem = new GoodsItem();
  93. $totalStock = array_sum(array_column($params['spec_value_list'],'stock'));
  94. $goodsItem->saveAll($params['spec_value_list']);
  95. $spellPrice = array_column($params['spec_value_list'],'sell_price');
  96. $minPrice = min($spellPrice);
  97. $maxPrice = max($spellPrice);
  98. Goods::where(['id'=>$params['id']])->update(['min_price'=>$minPrice,'max_price'=>$maxPrice,'total_stock'=>$totalStock]);
  99. return true;
  100. }catch (\Exception $e) {
  101. return $e->getMessage();
  102. }
  103. }
  104. }