<?php

namespace app\api\logic\order;


use app\common\model\order\Cart;
use app\common\logic\BaseLogic;
use app\common\model\store_product_log\StoreProductLog;
use think\facade\Db;


/**
 * 购物车表逻辑
 * Class CartLogic
 * @package app\admin\logic\order
 */
class CartLogic extends BaseLogic
{


    /**
     * @notes 添加购物车表
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/04/24 10:37
     */
    public static function add(array $params)
    {
        if ($params['store_id'] <= 0) {
            self::setError('门店ID不能为空');
            return false;
        }
        Db::startTrans();
        try {
            //check
            $check = Cart::where([
                'uid' => $params['uid'],
                'store_id' => $params['store_id'],
                'product_id' => $params['product_id'],
                'is_pay' => 0
            ])->field('id')->find();
            if ($check) {
                Cart::where('id', $check['id'])->inc('cart_num', $params['cart_num'])
                    ->update();
                $cart['id'] = $check['id'];
            } else {
                $cart = Cart::create([
                    'uid' => $params['uid'],
                    'type' => $params['type'] ?? '',
                    'product_id' => $params['product_id'],
                    'store_id' => $params['store_id'] ?? 0,
                    'staff_id' => $params['staff_id'] ?? 0,
                    'cart_num' => $params['cart_num'],
                    'is_new' => $params['is_new'] ?? 0,
                ]);
            }
            StoreProductLog::create([
                'type' => 'cart',
                'uid' => $params['uid'],
                'cart_id' => $cart['id'],
                'store_id' => $params['store_id'] ?? 0,
                'visit_num' => 1,
                'product_id' => $params['product_id'],
                'cart_num' => $params['cart_num'],
            ]);
            Db::commit();
            return $cart;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑购物车表
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/04/24 10:37
     */
    public static function edit(array $params, $type = 'inc'): bool
    {
        Db::startTrans();
        try {
            Cart::where([
                'uid' => $params['uid'],
                'store_id' => $params['store_id'],
                'product_id' => $params['product_id']
            ])
                ->update(['cart_num' => $params['cart_num']]);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除购物车表
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/04/24 10:37
     */
    public static function delete(array $params): bool
    {
        return Cart::destroy($params['id']);
    }


    /**
     * @notes 获取购物车表详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/04/24 10:37
     */
    public static function detail($params): array
    {
        return Cart::findOrEmpty($params['id'])->toArray();
    }
}