ShopCartLogic.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\db\Query;
  20. use think\facade\Db;
  21. /**
  22. * ShopGoods逻辑
  23. * Class ShopGoodsLogic
  24. * @package app\adminapi\logic\shops
  25. */
  26. class ShopCartLogic extends BaseLogic
  27. {
  28. /**
  29. * @notes 获取详情
  30. * @param $params
  31. * @return array
  32. * @author likeadmin
  33. * @date 2024/08/04 11:07
  34. */
  35. public static function add($params):bool
  36. {
  37. Db::startTrans();
  38. try{
  39. Db::commit();
  40. $goodsModel = ShopGoods::findOrEmpty($params['shop_goods_id']);
  41. if($goodsModel->isEmpty()){
  42. throw new \Exception('商品不存在');
  43. }
  44. $goodSpecInventoryModel = ShopGoodSpecsInventory::where(['id'=>$params['goods_specs_inventory_id'],'shop_goods_id'=>$params['shop_goods_id']])->findOrEmpty();
  45. if($goodSpecInventoryModel->isEmpty()){
  46. throw new \Exception('规格不存在');
  47. }
  48. $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();
  49. if(!$model->isEmpty()){
  50. $model->number = $model->number + $params['number'];
  51. $model->save();
  52. Db::commit();
  53. return true;
  54. }
  55. ShopCart::create([
  56. 'shop_goods_id' => $params['shop_goods_id'],
  57. 'goods_specs_inventory_id' => $params['goods_specs_inventory_id'],
  58. 'number' => $params['number'],
  59. 'worker_id' => $params['worker_id'],
  60. ]);
  61. Db::commit();
  62. return true;
  63. }catch(\Exception $e){
  64. Db::rollback();
  65. self::setError($e->getMessage());
  66. return false;
  67. }
  68. }
  69. public static function delete($params)
  70. {
  71. try{
  72. ShopCart::whereIn('id',$params['ids'])->where('worker_id',$params['worker_id'])->delete();
  73. Db::commit();
  74. return true;
  75. }catch (\Exception $e){
  76. Db::rollback();
  77. self::setError($e->getMessage());
  78. return false;
  79. }
  80. }
  81. public static function edit($params)
  82. {
  83. try{
  84. $cartModel = ShopCart::findOrEmpty($params['id']);
  85. if($cartModel->isEmpty()){
  86. throw new \Exception('数据不存在');
  87. }
  88. if($cartModel->worker_id != $params['worker_id']){
  89. throw new \Exception('非法操作');
  90. }
  91. $cartModel->number = $params['number'];
  92. $cartModel->save();
  93. Db::commit();
  94. return true;
  95. }catch (\Exception $e){
  96. Db::rollback();
  97. self::setError($e->getMessage());
  98. return false;
  99. }
  100. }
  101. }