<?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\TestModel;
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 selectSampleList($task_id,$pageNum,$pageSize)
    {
        $uid = $this->uid;
        $where=[
            ['task_id', '=', $task_id],
            ['sample_unit', '=', $uid]
        ];
        $result = (new Cyd())->selectSampleListByTaskIdAndUid($where, $pageNum, $pageSize);
        $count = (new Cyd())->countSampleListByTaskIdAndUid($where);
        return ['rows'=>$result,'total'=>$count];
    }

    public function getSampleObj($task_id,$under_task_id)
    {
        $model_id = (new Ccjc())->getTaskInfoByTaskID($task_id)['test_model_id'];
        $data = (new TestModel())->getProductInfo($model_id);
        $result=(new Arr())->Arr_columns($data, 'product_name,product_id');
        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 = [
            '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;
    }

}