<?php


namespace app\cxinv\controller;


use app\cxinv\model\ProductSeal;use think\App;
class FinancialSeal extends Base{
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model = new \app\cxinv\model\ProductFz();
    }
    // 列表
    public function list(){
        $params = $this->request->param(["page"=>1,"size"=>10,"company_code"=>"","status"=>"","fz_date"=>""],"post","trim");
        $where=[];
        if($params['company_code']!=''){
            $where[]=['company_code','=',$params['company_code']];
        }
        if($params['status']!=''){
            $where[]=['status','=',$params['status']];
        }
        if($params['fz_date']!=''){
            $where[]=['fz_date','=',$params['fz_date']];
        }
        $list = $this->model->where($where)->order('id desc')->paginate(['list_rows'=>$params['size'],'page'=>$params['page']]);
       return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
    }

    public function getQuery(){
         $params = $this->request->param(['company_code'=>'','status'=>''],'post','trim');
         $where=[];
         if($params['company_code']!='') $where[]=['company_code','=',$params['company_code']];
         if($params['status']!='') $where[]=['status','=',$params['status']];
         $list=$this->model->where($where)->select();
         return success('获取成功',$list);
    }
    //0解封 1封账中 2 封账完成 3 封账失败
    public function status(){
        $params = $this->request->param(["id"=>"","status"=>""],"post","trim");
        $valid=$this->validate($params,[
            'id'=>'require',
            'status'=>'require|in:0,1,2,3'
        ]);
        if($valid!==true) return error($valid);
        $row = $this->model->findOrEmpty($params['id']);
        if($row->isEmpty()) return error('数据不存在');
        $getLastMonth= date("Y-m",strtotime("-1 month"));
        if($row['fz_date']!=$getLastMonth && $params['status']==0) return error('非上月数据不可解封');
        if($params['status']==1){
            $montah =   date('Y-m',strtotime('-1 month',strtotime($row['fz_date'])));
            $lasMonth= $this->model->where('fz_date',$montah)->where('company_code',$row->company_code)->findOrEmpty();
            if(!$lasMonth->isEmpty()&& $lasMonth['status']!=2) return error($montah.'数据未封账');
        }
        $row->status=$params['status'];
        $row->apply_id=$this->uid;
        $row->apply_name=$this->uname;
        $as = $row->save();
        if($as==false) return error('操作失败');
        return success('操作成功');
    }

    public function FzList(){
        $params = $this->request->param(["page"=>1,"size"=>10,"company_code"=>"","skuCode"=>"",'product_id'=>"","fz_date"=>""],"post","trim");
        $where=[];
        if($params['company_code']!=''){
            $where[]=['company_code','=',$params['company_code']];
        }
        if($params['product_id']!=''){
            $where[]=['product_id','=',$params['product_id']];
        }
        if($params['skuCode']!='') $where[]=['skuCode','like','%'.$params];
        if($params['fz_date']!=''){
            $where[]=['fz_date','=',$params['fz_date']];
        }
        $list = ProductSeal::with(["PorductFz",'Porduct'])->withJoin(["PorductFz"],"left")->where($where)->order('product_seal.id desc')->paginate(['list_rows'=>$params['size'],'page'=>$params['page']]);
        return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
    }

    public function Export(){
        $params = $this->request->param(['company_code'=>'','status'=>'','fz_date'=>''],'post','trim');
        $where=[];
        if($params['company_code']!=''){
            $where[]=['company_code','=',$params['company_code']];
        }
        if($params['status']!=''){
            $where[]=['status','=',$params['status']];
        }
        if($params['fz_date']!=''){
            $where[]=['fz_date','=',$params['fz_date']];
        }
        $list = ProductSeal::with(["PorductFz",'Porduct'])->withJoin(['PorductFz'],'left')->where($where)->order('product_seal.id desc')->select();
        return success('获取成功',$list);
    }
}