<?php

namespace app\models\user;

use app\models\store\StoreOrderCartInfo;
use crmeb\basic\BaseModel;
use crmeb\traits\ModelTrait;
use think\Exception;


class UserCode extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'user_code';

    use ModelTrait;

    /**
     * 增加库存
     * @param $order
     */
    public static function add_stock($order)
    {
        if(self::be(['uid'=>$order['uid'],'code'=>$order['code']]))
        {
            $cartlist = StoreOrderCartInfo::getProductList($order['id']);
            $sum = 0;
            foreach ($cartlist as $v)
            {
                $data['code'] = $order['code'];
                $data['product_id'] = $v['goods_id'];
                $sum += intval($v['attrInfo']['suk']);
                if(UserCodeInfo::be($data) && isset($v['attrInfo']['suk']))
                {

                    UserCodeInfo::where($data)->inc('goods_stock',$v['attrInfo']['suk'])->update();
                }
                else
                {
                    if(isset($v['attrInfo']['suk']))
                    {
                        $data['goods_stock'] = $v['attrInfo']['suk'];
                        $data['goods_name'] = $v['store_name'];
                        $data['goods_pic'] = $v['image'];
                        $data['add_time'] = time();
                        UserCodeInfo::create($data);
                    }
                }
            }
            self::where(['uid'=>$order['uid'],'code'=>$order['code']])->inc('total_stock',$sum);
        }
    }

    /**
     * 增加销量  大于2500单的 不记录销量
     * @param $order
     */
    public static function add_sales($order)
    {
        if(!self::be(['uid'=>$order['uid'],'code'=>$order['code']]) && $order['code']!='')
        {
            $cartlist = StoreOrderCartInfo::getProductList($order['id']);
            self::beginTrans();
            $sum = 0;
            foreach ($cartlist as $v)
            {
                $data['code'] = $order['code'];
                $data['product_id'] = $v['goods_id'];
                if(isset($v['attrInfo']['suk']))
                {
                    if(UserCodeInfo::be($data))
                    {
                        UserCodeInfo::where($data)->dec('goods_stock',$v['attrInfo']['suk'])->inc('goods_sales',$v['attrInfo']['suk'])->update();
                    }
                    else
                    {
                        $data['uid'] = UserCode::where('code',$order['code'])->value('code');
                        $data['goods_stock'] = -intval($v['attrInfo']['suk']);
                        $data['goods_name'] = $v['store_name'];
                        $data['goods_pic'] = $v['image'];
                        $data['add_time'] = time();
                        UserCodeInfo::create($data);
                    }
                    $sum += intval($v['attrInfo']['suk']);
                }
            }
            self::where(['code'=>$order['code']])->dec('total_stock',$sum)-inc('total_sales',$sum);
            if($sum>=2500)
            {
                self::rollbackTrans();
            }
            else
            {
                self::commitTrans();
            }

        }
    }

    /**
     * 增加总代
     * @param $uid
     * @param $cart_info
     */
    public static function  add_code($uid,$cart_info,$sum)
    {
        $code = uniqid('',true);
        $add_time = time();
        $total_stock = $sum;
        self::beginTrans();
        try {
            self::create(compact('uid', 'code', 'add_time', 'total_stock'));
            foreach ($cart_info as $v) {
                $data['code'] = $code;
                $data['product_id'] = $v['goods_id'];
                $sum += intval($v['attrInfo']['suk']);
                if (isset($v['attrInfo']['suk'])) {
                    $data['goods_stock'] = $v['attrInfo']['suk'];
                    $data['goods_name'] = $v['store_name'];
                    $data['goods_pic'] = $v['image'];
                    $data['add_time'] = time();
                    UserCodeInfo::create($data);
                }
            }
            self::commitTrans();
        }catch (Exception $e)
        {
            self::rollbackTrans();
        }
    }


}