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

namespace app\api\business;

use app\api\model\Ccjc;
use app\api\model\Cyd;
use app\api\model\Jcdw;
use app\api\model\TestModel;
use app\api\model\Undertake;
use app\api\model\UnitProduction;
use app\api\model\UnitSample;
use app\api\model\UnitTest;
use app\common\lib\Arr;
use app\common\lib\auth\JwtAuth;

class SampleBus
{
    private $uid;

    public function __construct()
    {
        $JwtAuth = JwtAuth::getInstance();
        $this->uid = $JwtAuth->getUid();
    }

    public function getCydDetail($sample_id)
    {
        $result = (new Cyd())->getCydDetailBySampleID($sample_id);
        return $result;
    }

    public function selectSampleList($data)
    {
        $uid = $this->uid;


        $where=[
            ['cyd.task_id', '=', $data['task_id']],//这里绑定成cyd.task_id进行查询
            ['undertake_id','=',$data['undertake_id']],
            //['uid', '=', $uid]
        ];
        //注释掉的是搜索功能
//        if ($data['sample_name']!='') $where[]=['sample_name', 'LIKE', '%'.$data['sample_name'].'%'];//样品名称
//        if ($data['sample_id']!='') $where[]=['sample_id', 'LIKE', '%'.$data['sample_id'].'%'];//样品编码
//        if ($data['sample_ground']!='') $where[]=['sample_ground', 'LIKE', '%'.$data['sample_ground'].'%'];//抽样场所
//        if ($data['undertake_id']!='') $where[] = ['undertake_id', '=', $data['undertake_id']];
//
//        //bear-抽样单位，report-报告单位，check-检测单位
//
//        if($data['unit_test_name']!='') $where[]=['unit_test_name','LIKE','%'.$data['unit_test_name'].'%'];//受检单位名称
//        if($data['unit_sample_name']!='') $where[]=['unit_sample_name','LIKE','%'.$data['unit_sample_name'].'%'];//抽样单位名称
//        if($data['unit_production_name']!='') $where[]=['unit_production_name','LIKE','%'.$data['unit_production_name'].'%'];//生产单位名称
//
//        if($data['sample_data']!='') $where[]=['sample_date','=',$data['sample_data']];//抽样日期

        $result = (new Cyd())->selectSampleListByTaskIdAndUid($where, $data['pageNum'], $data['pageSize']);
        $count = (new Cyd())->countSampleListByTaskIdAndUid($where);
        return ['rows'=>$result,'total'=>$count];
    }

    public function getSampleObj($data)
    {

        $model_id = (new Ccjc())->getTaskInfoByTaskID($data['task_id'])['test_model_id'];
        $data_tmp = (new TestModel())->getProductInfo($model_id);
        $result=(new Arr())->Arr_columns($data_tmp, 'product_name,product_id');
        //根据$data['undertake_id']获取抽样单位和检测单位信息
        $undertaek_info=(new Undertake())->getItemByUndertakeId($data['undertake_id']);


        //根据信息获取抽样单位和检测单位的信息
        $sample_info = (new Jcdw())->getInfoById($undertaek_info['bear_id']);
        $check_info = (new Jcdw())->getInfoById($undertaek_info['check_id']);
        return ['list'=>$result,'sample_info'=>$sample_info,'check_info'=>$check_info];
//        return $result;

    }

    public function createSampleId($task_id)
    {
        $task_class = (new Ccjc())->getTaskInfoByTaskID($task_id)['task_class'];
        $count = (new Cyd())->countSampleCreateID();

        if ($task_class=="例行监测") {
            $id['id'] = 'LXJC' . date('Y') . date('m') . date('d') . date('H') . date('m') . str_pad(($count + 1), 4, 0, STR_PAD_LEFT);
        }
        if ($task_class=='专项监测'){
            $id['id'] = 'ZXJC'.date('Y').date('m').date('d').date('H').date('m').str_pad(($count+1),4,0,STR_PAD_LEFT);
        }
        return $id;
    }



    public function createSample($unit_test,$unit_production,$unit_sample,$cyd_data)
    {
        //写入抽样单信息，并返回抽样单cyd ID
        $cyd_id = (new Cyd())->createSample($cyd_data);

        //赋值cydID
        $unit_test['cyd_id']=$unit_production['cyd_id']=$unit_sample['cyd_id']=$cyd_id;

        //写入受检单位信息，并返回自增id
        $unit_test_id = (new UnitTest())->insertUnitTestDataGetId($unit_test);
        //写入生产单位信息，并返回自增id
        $unit_production_id = (new UnitProduction())->insertUnitProductionDataGetId($unit_production);
        //写入抽样单位信息，并返回自增id
        $unit_sample_id=(new UnitSample())->insertUnitSampleDataGetId($unit_sample);

        //将各种id写入cyd
        $cyd_id_data = [
            'uid' => $this->uid,
            'test_id' => '',//检测单位id
            'units_id' => $unit_test_id,//受检单位id
            'product_unit' => $unit_production_id,//生产单位id
            'sample_unit' => $unit_sample_id,//抽样单位id
        ];
        $result = (new Cyd())->updateCydDataByID($cyd_id,$cyd_id_data);

        return $result;
    }

}