ShopCartLogic.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\workerapi\logic\shops;
  15. use app\common\model\shops\ShopCart;
  16. use app\common\model\shops\ShopGoods;
  17. use app\common\logic\BaseLogic;
  18. use app\common\model\shops\ShopGoodSpecsInventory;
  19. use think\facade\Db;
  20. /**
  21. * ShopGoods逻辑
  22. * Class ShopGoodsLogic
  23. * @package app\adminapi\logic\shops
  24. */
  25. class ShopCartLogic extends BaseLogic
  26. {
  27. /**
  28. * @notes 获取详情
  29. * @param $params
  30. * @return array
  31. * @author likeadmin
  32. * @date 2024/08/04 11:07
  33. */
  34. public static function add($params):bool
  35. {
  36. Db::startTrans();
  37. try{
  38. Db::commit();
  39. $goodsModel = ShopGoods::findOrEmpty($params['shop_goods_id']);
  40. if($goodsModel->isEmpty()){
  41. throw new \Exception('商品不存在');
  42. }
  43. $goodSpecInventoryModel = ShopGoodSpecsInventory::where(['id'=>$params['goods_specs_inventory_id'],'shop_goods_id'=>$params['shop_goods_id']])->findOrEmpty();
  44. if($goodSpecInventoryModel->isEmpty()){
  45. throw new \Exception('规格不存在');
  46. }
  47. $model = ShopCart::where(['shop_goods_id'=>$params['shop_goods_id'],'goods_specs_inventory_id'=>$params['goods_specs_inventory_id'],'worker_id'=>$params['worker_id']])->findOrEmpty();
  48. if(!$model->isEmpty()){
  49. if($goodSpecInventoryModel->remaining_inventory < $params['number'] + $model->number){
  50. throw new \Exception('库存不足');
  51. }
  52. $model->number = $model->number + $params['number'];
  53. $model->save();
  54. Db::commit();
  55. return true;
  56. }
  57. ShopCart::create([
  58. 'shop_goods_id' => $params['shop_goods_id'],
  59. 'goods_specs_inventory_id' => $params['goods_specs_inventory_id'],
  60. 'number' => $params['number'],
  61. 'worker_id' => $params['worker_id'],
  62. ]);
  63. Db::commit();
  64. return true;
  65. }catch(\Exception $e){
  66. Db::rollback();
  67. self::setError($e->getMessage());
  68. return false;
  69. }
  70. }
  71. public static function delete($params)
  72. {
  73. Db::startTrans();
  74. try{
  75. ShopCart::whereIn('id',$params['ids'])->where('worker_id',$params['worker_id'])->delete();
  76. Db::commit();
  77. return true;
  78. }catch (\Exception $e){
  79. Db::rollback();
  80. self::setError($e->getMessage());
  81. return false;
  82. }
  83. }
  84. public static function edit($params)
  85. {
  86. Db::startTrans();
  87. try{
  88. $cartModel = ShopCart::findOrEmpty($params['id']);
  89. if($cartModel->isEmpty()){
  90. throw new \Exception('数据不存在');
  91. }
  92. if($cartModel->worker_id != $params['worker_id']){
  93. throw new \Exception('非法操作');
  94. }
  95. $goodSpecInventoryModel = ShopGoodSpecsInventory::findOrEmpty($cartModel->goods_specs_inventory_id);
  96. if($goodSpecInventoryModel->isEmpty()){
  97. throw new \Exception('规格不存在');
  98. }
  99. if($params['number'] > $goodSpecInventoryModel->remaining_inventory){
  100. throw new \Exception('库存不足');
  101. }
  102. $cartModel->number = $params['number'];
  103. $cartModel->save();
  104. Db::commit();
  105. return true;
  106. }catch (\Exception $e){
  107. Db::rollback();
  108. self::setError($e->getMessage());
  109. return false;
  110. }
  111. }
  112. }