<?php

namespace app\api\business;

use app\api\exception\ApiException;
use app\api\model\Ccjc;
use app\api\model\Cyd;
use app\api\model\SamplePesticides;
use app\api\model\TestPesticides;

class JcdBus
{

    public function getJcdList($data)
    {
        $jcd_info = (new Cyd())->getCydListByUndertakeId($data['undertake_id'], $data['pageNum'], $data['pageSize']);
        $jcd_info_count = (new Cyd())->countCydListByUndertakeId($data['undertake_id']);

        return ['rows'=>$jcd_info,'total'=>$jcd_info_count];
    }

    public function getJcdObjectItem($sampel_id)
    {
        //根据模型，需要先获取到task_id才能获取到model_id，最后获取检测项
        $task_id = (new Cyd())->getCydInfoBySampleId($sampel_id)['task_id'];
        $test_model_id = (new Ccjc())->getTaskInfoByTaskID($task_id)['test_model_id'];
        $model_item = (new TestPesticides())->getInfoByTestModelId($test_model_id)->toArray();

        //检索数据库中是否保存检测项信息，如果为空进行创建操作，如果存在进行读取操作
        $data = [];
        $data_num = 0;
        $create_jcd_item=[];
        foreach ($model_item as $k=>$v) {
            //检索 是否存在,不存在则$item_is_set==null
            $item_is_set = (new SamplePesticides())->getItemByTestIdAndSampleId($v['test_id'],$sampel_id);
            if ($item_is_set==null){
                $data[$k]['test_id'] = $v['test_id'];
                $data[$k]['test_name'] = $v['test_name'];
                $data[$k]['sample_id'] = $sampel_id;
            }
        }
        //创建不存在的检测项
        $create_jcd_item = (new SamplePesticides())->saveItem($data);
        //获取检测单的信息
        $item_info = (new SamplePesticides())->getItemBySampleId($sampel_id);

        return $item_info;
    }

    //修改检测单状态，就是修改cyd表中的，test_status
    public function upJcdItem($sample_id)
    {
        $jcd_status = ['test_status' => '1'];
        $result = (new Cyd())->updateCydDataBySampleID($sample_id, $jcd_status);
        return $result;
    }

    //保存检测单
    public function saveJcdItem($sample_id, $pesticides){
        //格式化数据
        $data = [];//需要保存的数据
        $data_num =0;//一共有多少个需要保存的数据
        foreach ($pesticides as $k0 =>$v0) {
            if ($pesticides[$k0]['test_value'] != '') {
                $data [$k0]['test_id'] = $pesticides[$k0]['test_id'];
                $data [$k0]['test_value'] = $pesticides[$k0]['test_value'];
                $data [$k0]['state_value'] = $pesticides[$k0]['state_value'];
                $data [$k0]['unit'] = $pesticides[$k0]['unit'];
                $data [$k0]['creattime'] = $pesticides[$k0]['creattime'];
                $data [$k0]['sample_id'] = $sample_id;
                $data [$k0]['LOD'] = $pesticides[$k0]['LOD'];
                $data [$k0]['LOQ'] = $pesticides[$k0]['LOQ'];
                $data [$k0]['test_result'] = $pesticides[$k0]['test_result'];
                $data_num = $k0;
            }
        }



        $disable_save_data=$enable_save_data=[];
        //查找数据，如果enable_save==0,则无法保存，为null则可以保存

        for ($i = 0; $i < ($data_num+1); $i++) {
            $save_data = (new SamplePesticides())->getItemByTestIdAndSampleId($data[$i]['test_id'], $sample_id);

            //如果enable_save为1不可以保存
            if ($save_data['enable_save']==1) {
                $disable_save_data[] = ['test_id' => $data[$i]['test_id'], 'sample_id' => $sample_id];
            }
            //如果enable_save为null可以保存
            else if ($save_data['enable_save'] == null && $save_data['test_value']=='') {
                //添加数据
                (new SamplePesticides())->saveItemByTestIdAndSampleId($data[$i]['test_id'], $sample_id, $data[$i]);
                //根据item_id将对应的enable_save字段置1，禁止二次保存
                (new SamplePesticides())->saveItemByTestIdAndSampleId($data[$i]['test_id'], $sample_id,['enable_save' => 1]);

                $enable_save_data[] = ['test_id' => $data[$i]['test_id'], 'sample_id' => $sample_id];
            }
            else{
                throw new ApiException(config('status.err_none_auth_upload_data'));
            }
        }

        //将检测单的判定结果（整体合格还是不合格）存到cyd的test_result
        $test_result=(new SamplePesticides())->getItemBySampleId($sample_id)->toArray();
        $test_result_true=$test_result_false=$test_result_null=0;//三个统计项目，分别是检测合格、不合格、为空
        foreach ($test_result as $key => $value) {
            if ($test_result[$key]['test_result'] == '合格') $test_result_true+=1;
            if ($test_result[$key]['test_result'] == '不合格') $test_result_false+=1;
            if ($test_result[$key]['test_result'] == '') $test_result_null+=1;
        }
        //所有项目都有检测值，且没有不合格，则总的检测结果为合格
        if ($test_result_null == 0 && $test_result_false==0) {
            (new Cyd())->updateCydDataBySampleID($sample_id,['test_result'=>1]);
        }
        //所有项目都有检测值，且有不合格，则总的检测结果为不合格
        if ($test_result_null == 0 && $test_result_false!=0) {
            (new Cyd())->updateCydDataBySampleID($sample_id,['test_result'=>0]);
        }


        return ['enable_upload_data'=>$enable_save_data,'disable_upload_data'=>$disable_save_data];

    }

    //生成检测报告单
    public function createTestValue($sample_id)
    {
        $result_detail = (new SamplePesticides())->getItemBySampleId($sample_id);
        $result = (new Cyd())->getCydDetailBySampleID($sample_id);
        return ['result_detail'=>$result_detail,'result'=>$result];
    }


}