<?php
namespace app\admin\controller;

use app\common\model\MateGoodsLog;
use think\App;
use think\Db;
use think\Exception;

class MateCheck extends Auth
{
    public function __construct(App $app = null) {
        parent::__construct($app);
        $this->model = new \app\common\model\MateCheck();
        $this->table = $this->model->table;
    }
    public function index(){
        if(request()->isAjax()){
            //分页参数
            $length = input('rows',10,'intval');   //每页条数
            $page = input('page',1,'intval');      //第几页
            $start = ($page - 1) * $length;     //分页开始位置
            //排序
            $sortRow = input('sidx','id','trim');      //排序列
            $sort = input('sord','desc','trim');        //排序方式
            $order = $sortRow.' '.$sort;

            $enable = input('enable','','trim');
            if($enable != ''){
                $map[] = ['status','=',$enable];
            }
            $map[] = ['del','=',0];
            $map[] = ['org_id','=',$this->orgId];
            $map= empty($map) ? true: $map;
            //数据查询
            $lists =db($this->table)
                ->where($map)->limit($start,$length)
                ->order($order)->select();
            $lists = $lists?$lists:[];
            foreach ($lists as $k=>$v){
                $lists[$k]['userName'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
            }

            //数据返回
            $totalCount = db($this->table)->where($map)->count();
            $totalPage = ceil($totalCount/$length);
            $result['page'] = $page;
            $result['total'] = $totalPage;
            $result['records'] = $totalCount;
            $result['rows'] = $lists;
            return json($result);
        }else{
            $this->assign('meta_title','盘库列表');
            return $this->fetch();
        }
    }

    /**
     * 新增/编辑
     */
    public function add(){
        if(request()->isPost()){
            $res = $this->model->updates($this->userId);
            if($res){
                $this->success('操作成功',url('index'));
            }else{
                $this->error($this->model->getError());
            }
        }else{
            $meta_title = '新增盘库';
            $this->assign('meta_title',$meta_title);
            return $this->fetch();
        }
    }

    //详情
    public function info($id){
        $info = $this->model->getInfo($id);
        if(!$info) $this->error('记录不存在');
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function check($id=0){
        if(request()->isPost()){
            $res = $this->model->checkGoods($this->userId);
            if($res){
                $this->success('操作成功',url('index'));
            }else{
                $this->error($this->model->getError());
            }
        }else{
            $this->assign('id',$id);
            return $this->fetch();
        }
    }

    public function delGoods($id=0){
        $info = Db::name('mate_check_goods')->where('id',$id)->where('del',0)->find();
        if(!$info){
            $this->error('记录不存在');
        }
        $ret = Db::name('mate_check_goods')->where('id',$id)->update(['user_id'=>$this->userId,'del'=>1]);
        if($ret){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }

    public function del($id=0){
        $info = Db::name('mate_check')->where('id',$id)->where('del',0)->find();
        if(!$info){
            $this->error('记录不存在');
        }
        Db::startTrans();
        try{
            $ret = Db::name('mate_check')->where('id',$id)->update(['del_user'=>$this->userId,'del'=>1,'del_time'=>date('Y-m-d H:i:s')]);
            if(!$ret){
                \exception('操作成功');
            }

            Db::name('mate_check_goods')->where('check_id',$id)->update(['user_id'=>$this->userId,'del'=>1]);

            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }

    public function finish($id=0){
        $info = Db::name('mate_check')->where('id',$id)->where('del',0)->find();
        if(!$info){
            $this->error('记录不存在');
        }
        if($info['status'] != 0){
            $this->error('盘库已完成');
        }
        $ginfo = Db::name('mate_check_goods')->where('check_id',$id)->where('status',0)->where('del',0)->find();
        if($ginfo){
            $this->error('还有物品未盘库');
        }
        $ret = Db::name('mate_check')->where('id',$id)->update(['status'=>1,'finish_time'=>date('Y-m-d H:i:s')]);
        if(!$ret){
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }

}