<?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['goods_id'] = $v['product_id'];
                $sum += intval($v['productInfo']['attrInfo']['suk']);
                if(UserCodeInfo::be($data) && isset($v['productInfo']['attrInfo']['suk']))
                {

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

    /**
     * 增加销量  大于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['goods_id'] = $v['product_id'];
                if(isset($v['productInfo']['attrInfo']['suk']))
                {
                    if(UserCodeInfo::be($data))
                    {
                        UserCodeInfo::where($data)->dec('goods_stock',$v['productInfo']['attrInfo']['suk'])->inc('goods_sales',$v['productInfo']['attrInfo']['suk'])->update();
                    }
                    else
                    {
                        $data['uid'] = UserCode::where('code',$order['code'])->value('code');
                        $data['goods_stock'] = -intval($v['productInfo']['attrInfo']['suk']);
                        $data['goods_name'] = $v['productInfo']['store_name'];
                        $data['goods_pic'] = $v['productInfo']['image'];
                        $data['add_time'] = time();
                        UserCodeInfo::create($data);
                    }
                    $sum += intval($v['productInfo']['attrInfo']['suk']);
                }
            }
            self::where(['code'=>$order['code']])->dec('total_stock',$sum)->inc('total_sales',$sum)->update();
            self::commitTrans();
        }
    }

    /**
     * 增加总代
     * @param $uid
     * @param $cart_info
     */
    public static function  add_code($uid,$cart_info,$sum)
    {

        $add_time = time();
        $total_stock = $sum;
        self::beginTrans();
        $data['uid'] = $uid;
        try {
            if(!self::be(['uid'=>$uid])) {
                $code = uniqid('',true);
                self::create(compact('uid', 'code', 'add_time', 'total_stock'));
                User::where('uid',$uid)->update(['code'=>$code,'code_top'=>1]);
                UserBill::where('uid',$uid)->where('type','in','give_stock_right,code_stock_right')->update(['code'=>$code]);
                foreach ($cart_info as $v) {
                    $data['code'] = $code;
                    $data['goods_id'] = $v['product_id'];
                    $sum += intval($v['productInfo']['attrInfo']['suk']);
                    if (isset($v['productInfo']['attrInfo']['suk'])) {
                        $data['goods_stock'] = $v['productInfo']['attrInfo']['suk'];
                        $data['goods_name'] = $v['productInfo']['store_name'];
                        $data['goods_pic'] = $v['productInfo']['image'];
                        $data['add_time'] = time();
                        UserCodeInfo::create($data);
                    }
                }
            }
            else
            {
                self::where('uid',$uid)->inc('total_stock',$sum)->update();
                foreach ($cart_info as $v) {
                    $data['code'] = User::where('uid',$uid)->value('code');
                    $data['goods_id'] = $v['product_id'];
                    $sum += intval($v['productInfo']['attrInfo']['suk']);
                    if(!UserCodeInfo::be($data)) {
                        if (isset($v['productInfo']['attrInfo']['suk'])) {
                            $data['goods_stock'] = $v['productInfo']['attrInfo']['suk'];
                            $data['goods_name'] = $v['productInfo']['store_name'];
                            $data['goods_pic'] = $v['productInfo']['image'];
                            $data['add_time'] = time();
                            UserCodeInfo::create($data);
                        }
                    }
                    else
                    {
                        UserCodeInfo::where($data)->inc('goods_stock',$v['productInfo']['attrInfo']['suk'])->update();
                    }
                }
            }
            unset($data);
            self::commitTrans();
        }catch (Exception $e)
        {
            echo $e->getMessage();
            self::rollbackTrans();
        }
    }
    public static function lst($where)
    {
        $model = new self;
        $model = $model->alias('c')->join('user u',"c.uid=u.uid","left");


    }


}