ShopCartLogic.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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)
  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. ShopCart::create([
  49. 'shop_goods_id' => $params['shop_goods_id'],
  50. 'goods_specs_inventory_id' => $params['goods_specs_inventory_id'],
  51. 'number' => $params['number'],
  52. 'worker_id' => $params['worker_id'],
  53. ]);
  54. Db::commit();
  55. return true;
  56. }catch(\Exception $e){
  57. Db::rollback();
  58. self::setError($e->getMessage());
  59. return false;
  60. }
  61. }
  62. public static function delete($params){
  63. try{
  64. ShopCart::whereIn('id',$params['ids'])->where('worker_id',$params['worker_id'])->delete();
  65. Db::commit();
  66. return true;
  67. }catch (\Exception $e){
  68. Db::rollback();
  69. self::setError($e->getMessage());
  70. return false;
  71. }
  72. }
  73. }