<?php
namespace app\common\model;

use think\Db;
use think\Exception;

class MateCheck extends Base
{
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    public $table = 'mate_check';
    protected $validateName = 'MateCheck';

    public function updates($userId){
        $data = request()->post();
        $data['org_id'] = cur_org_id();
        $result = validate($this->validateName)->check($data,[],'');
        if(true !== $result){
            $this->error = validate($this->validateName)->getError();
            return false;
        }
        $curTime = date('Y-m-d H:i:s');
        Db::startTrans();
        try{
            $applyId = Db::name('mate_check')->insertGetId([
                'org_id' => $data['org_id'],
                'create_time' => $curTime,
                'user_id' => $userId,
                'remark' => $data['remark'],
                'status' => 0
            ]);
            if(!$applyId){
                \exception('操作失败');
            }
            $arr = [];
            foreach ($data['goods'] as $k=>$v){
                $info = Db::name('mate_goods')->where('id',$v)->find();
                $arr[] = [
                    'check_id' => $applyId,
                    'goods_id' => $v,
                    'nums' => $info['nums'],
                    'price' => $info['price'],
                    'status' => 0
                ];
            }

            $ret = Db::name('mate_check_goods')->insertAll($arr);
            if($ret != count($arr)){
                \exception('操作失败');
            }

            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            $this->error = $e->getMessage();
            return false;
        }

        return true;
    }

    public function checkGoods($userId){
        $id = input('id/d',0);
        $checknums = input('check_nums/d',0);
        $remark = input('remark','','trim');
        if($checknums < 0){
            $this->error = '实盘数量不能小于0';
            return false;
        }
        $info = Db::name('mate_check_goods')->where('id',$id)->where('del',0)->find();
        if(!$info){
            $this->error = '记录不存在';
            return false;
        }
        if($info['status'] != 0){
            $this->error = '该记录已处理';
            return false;
        }
        $status = 2;
        if($checknums > $info['nums']){
            $status = 3;
        }else if($checknums < $info['nums']){
            $status = 1;
        }
        $ret = Db::name('mate_check_goods')->where('id',$id)->update([
            'user_id' => $userId,
            'status' => $status,
            'check_time' => date('Y-m-d H:i:s'),
            'check_nums' => $checknums,
            'remark' => $remark
        ]);
        if(!$ret){
            $this->error = '记录不存在';
            return false;
        }
        return true;
    }

    public function getInfo($id){
        $info = $this
            ->where('id',$id)
            ->find()
            ->toArray();
        if(!$info){
            return false;
        }
        $info['userName'] = Db::name('user')->where('id',$info['user_id'])->value('real_name');
        $goods = Db::name('mate_check_goods')
            ->alias('a')
            ->join('mate_goods c','c.id=a.goods_id')
            ->where('a.check_id',$info['id'])
            ->where('a.del',0)
            ->field('a.*,c.title,c.unit,c.brand,c.spec')
            ->select();
        $goods = $goods?$goods:[];
        $tnums = 0;
        $tchecknums = 0;
        foreach ($goods as $k=>$v){
            $tnums += $v['nums'];
            $tchecknums += $v['check_nums'];
        }
        $info['tnums'] = $tnums;
        $info['tchecknums'] = $tchecknums;

        $info['goods'] = $goods;
        return $info;
    }



}