<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\workerapi\logic\shops;


use app\common\model\shops\ShopCart;
use app\common\model\shops\ShopGoods;
use app\common\logic\BaseLogic;
use app\common\model\shops\ShopGoodSpecsInventory;
use think\facade\Db;


/**
 * ShopGoods逻辑
 * Class ShopGoodsLogic
 * @package app\adminapi\logic\shops
 */
class ShopCartLogic extends BaseLogic
{


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/08/04 11:07
     */
    public static function add($params):bool
    {
        Db::startTrans();
        try{
            Db::commit();
            $goodsModel = ShopGoods::findOrEmpty($params['shop_goods_id']);
            if($goodsModel->isEmpty()){
                throw new \Exception('商品不存在');
            }
            $goodSpecInventoryModel = ShopGoodSpecsInventory::where(['id'=>$params['goods_specs_inventory_id'],'shop_goods_id'=>$params['shop_goods_id']])->findOrEmpty();
            if($goodSpecInventoryModel->isEmpty()){
                throw new \Exception('规格不存在');
            }
            $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();
            if(!$model->isEmpty()){
                if($goodSpecInventoryModel->remaining_inventory < $params['number'] + $model->number){
                    throw new \Exception('库存不足');
                }
                $model->number = $model->number + $params['number'];
                $model->save();
                Db::commit();
                return true;
            }
           ShopCart::create([
                'shop_goods_id' => $params['shop_goods_id'],
                'goods_specs_inventory_id' => $params['goods_specs_inventory_id'],
                'number' => $params['number'],
                'worker_id' => $params['worker_id'],

            ]);
            Db::commit();
            return true;
        }catch(\Exception $e){
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    public static function delete($params)
    {
        Db::startTrans();
        try{
            ShopCart::whereIn('id',$params['ids'])->where('worker_id',$params['worker_id'])->delete();
            Db::commit();
            return true;
        }catch (\Exception $e){
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }

    public static function edit($params)
    {
        Db::startTrans();
        try{
            $cartModel = ShopCart::findOrEmpty($params['id']);
            if($cartModel->isEmpty()){
                throw new \Exception('数据不存在');
            }
            if($cartModel->worker_id != $params['worker_id']){
                throw new \Exception('非法操作');
            }
            $goodSpecInventoryModel = ShopGoodSpecsInventory::findOrEmpty($cartModel->goods_specs_inventory_id);
            if($goodSpecInventoryModel->isEmpty()){
                throw new \Exception('规格不存在');
            }
            if($params['number'] > $goodSpecInventoryModel->remaining_inventory){
                throw new \Exception('库存不足');
            }
            $cartModel->number = $params['number'];
            $cartModel->save();
            Db::commit();
            return true;
        }catch (\Exception $e){
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }
}