<?php
/**
 *
 *User:Administrator
 *Date:2021/10/14
 */

namespace app\api\model;

use think\Db;
use think\Model;

class Cyd extends Model
{
    function unitProduction()
    {
        return $this->belongsTo('unitProduction', 'product_unit','id');
    }

    function unitTest()//受检单位
    {
        return $this->belongsTo('unitTest', 'units_id', 'id');
    }

    function unitSample()
    {
        return $this->belongsTo('unitSample', 'sample_unit', 'id');
    }

    function undertake()
    {
        return $this->belongsTo('underTake', 'undertake_id', 'id');
    }

    //监管端获取抽样单
    public function getCydListDetail($where,$pageNum=1,$pageSize=null)
    {
        $result = $this
            ->alias('cyd')
            ->field('sample_id,sample_name,cyd.task_id,place,sample_date,sample_status,test_status,brand,level,sample_base,sample_ground,unit_id as lead_id')

            ->leftjoin('t_undertake a','a.id=cyd.undertake_id')
            ->field('bear_name,check_name,report_name,a.task_id as underTake_task_id')            //bear-抽样单位，report-报告单位，check-检测单位

            ->leftjoin('t_jcdw e','e.id=a.unit_id')
            ->field('e.name lead_name')//lead_name牵头单位名称


            ->leftjoin('t_unit_test b','cyd.id=b.cyd_id')//受检单位
            ->leftjoin('t_unit_production d','cyd.id=d.cyd_id')//生产单位
            ->field('unit_test_name,unit_production_name,cyd.undertake_id')
            ->where($where)
            ->page($pageNum,$pageSize)
            ->select();
        return $result;
    }

    public function getCydListByUndertakeId($undertake_id,$pageNum,$pageSize)
    {
        $data = $this->with(['undertake','unitTest'])
            ->where('undertake_id', '=', $undertake_id)
            ->page($pageNum,$pageSize)
            ->select();
        return $data;
    }
    public function countCydListByUndertakeId($undertake_id)
    {
        $data = $this->with(['undertake','unitTest'])
            ->where('undertake_id', '=', $undertake_id)
            ->count();
        return $data;
    }

    public function getCydDetailBySampleID($sample_id)
    {
        $data = $this->with(['unitProduction', 'unitTest','unitSample'])
            ->where('sample_id','=',$sample_id)
            ->find();
        return $data;
    }

    public function selectCydInfoByTaskID($task_id)
    {
        return $this->where([
            'task_id' => $task_id,
            'status'=>1
        ])->select();
    }

    public function countCydInfoByTaskID($task_id)
    {
        return $this->where([
            'task_id' => $task_id,
            'status'=>1
        ])->count();
    }


    public function selectSampleListByTaskIdAndUid($where,$pageNum=1,$pageSize=null)
    {

        //这里保留两个task_id是为了以后可能会删除一个
        $result = $this
            ->alias('cyd')
            ->field('sample_id,sample_name,cyd.task_id,place,sample_date,sample_status,test_status,brand,level,sample_base,sample_ground')
            ->field('unit_test_name,unit_production_name,cyd.undertake_id')
            ->field('bear_name,check_name,report_name,a.task_id as underTake_task_id')

            ->leftjoin('t_undertake a','a.id=cyd.undertake_id')
            //bear-抽样单位，report-报告单位，check-检测单位
            ->leftjoin('t_unit_test b','cyd.id=b.cyd_id')//受检单位
            ->leftjoin('t_unit_production d','cyd.id=d.cyd_id')//生产单位
            ->where($where)
            ->page($pageNum,$pageSize)
            ->select();
        return $result;


    }
    public function countSampleListByTaskIdAndUid($where)
    {
        $count = $this
            ->alias('cyd')
            ->field('sample_id,sample_name,cyd.task_id,place,sample_date,sample_status,test_status')
            ->leftjoin('t_undertake a','a.id=cyd.undertake_id')
            //bear-抽样单位，report-报告单位，check-检测单位
            ->field('bear_name,check_name,report_name,a.task_id as underTake_task_id')

            ->leftjoin('t_unit_test b','cyd.id=b.cyd_id')//受检单位
            ->leftjoin('t_unit_production d','cyd.id=d.cyd_id')//生产单位
            ->field('unit_test_name,unit_production_name')
            ->where($where)
            ->count();
        return $count;
    }

    public function createSample($cyd_data)
    {
        $result = $this->insertGetId($cyd_data);
        return $result;
    }

    public function updateCydDataByID($id, $data)
    {
        $result = $this->where('id','=',$id)->save($data);
        return $result;
    }


    //用于生成抽样单编号的
    public function countSampleCreateID()
    {
        return $this->whereDay('createtime','today')->count();
    }

    public function getCydInfoBySampleId($sample_id)
    {
        return $this->where('sample_id', '=', $sample_id)->find();
    }


    public function updateCydDataBySampleID($sample_id, $data)
    {
        return $this->where('sample_id', '=', $sample_id)->save($data);
    }
}