<?php
/**
 *
 * @author：llbjj
 * @DateTime：2022/5/6 10:57
 * @Description：
 *
 */

namespace Application\Service\DB\Item;

use Interop\Container\ContainerInterface;

class Checknames extends \Application\Service\DB\AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        $this->tableName = 'yikeen_item_checkname';
        parent::__construct($dbAdapter, $container);
    }

    /*
     * 检查项基本信息数据同步
     * $copy_item_id  需要复制的项目ID
     * $item_id       需要复制到该项目下
     */
    function ViceinfoAdd($copy_item_id,$item_id){
        $datas = $this->LocalService()->itemCheckdate->fetchOne(['where'=>'is_del = 0 and item_id = '.$copy_item_id,'columns'=>
            ['item_id','date_type']]);
        if(!empty($datas)){
            $datas['item_id'] = $item_id;
            $this->LocalService()->itemCheckdate->insert($datas);
        }

        //查询需要复制项目下的检查项信息
        $whereitemCheckname['where'] = [
            'is_del' => 0,
            'item_id' => $copy_item_id,
        ];;
        $whereitemCheckname['columns'] = [];
        $itemChecknameDatas = $this->LocalService()->itemCheckname->fetchAll($whereitemCheckname);
        if(!empty($itemChecknameDatas)){
            foreach($itemChecknameDatas as $key=>$val){
               $insertData['item_id'] = $item_id;
               $insertData['category_id'] = $val['category_id'];
               $insertData['checkname_id'] = $val['checkname_id'];
               $insertData['name'] = $val['name'];
               $insertData['show_name'] = $val['show_name'];
               $insertData['variable'] = $val['variable'];
               $insertData['unit'] = $val['unit'];
               $insertData['max_val'] = $val['max_val'];
               $insertData['max_symbol'] = $val['max_symbol'];
               $insertData['min_val'] = $val['min_val'];
               $insertData['min_symbol'] = $val['min_symbol'];
               $insertData['check_type'] = $val['check_type'];
               $insertData['alias'] = $val['alias'];
               $insertData['min_range'] = $val['min_range'];
               $insertData['max_range'] = $val['max_range'];
               $insertData['checkname_order'] = $val['checkname_order'];
               $insertData['form_id'] = $val['form_id'];
               $insertData['form_field_id'] = $val['form_field_id'];
               $insertData['is_del'] = 0;
               $result = $this->LocalService()->itemCheckname->insert($insertData);
               if (!empty($result)) $this->LocalService()->log->saveFileLog($result,'itemCheckname',$insertData);
            }
        }

    }

    //检查项基本信息数据同步
    function InfoAdd($item_id){
        //查询迁移前对应项目ID
        $item_id = $this->LocalService()->itemInfo->getOneFieldVal('id',"old_id = ".$item_id);
        //查询当前迁移项目数据
        $whereitemInfo['where'] = "fid = ".$item_id;
        $whereitemInfo['columns'] = ['id','old_id'];
        $itemInfoDatasnew = $this->LocalService()->itemInfo->fetchAll($whereitemInfo);
        if(!empty($itemInfoDatasnew)){
            $itemInfoId = [];
            foreach($itemInfoDatasnew as $key=>$val){
                $itemInfoId[$val['old_id']] = $val['id'];
            }
            $item_id_arr = array_unique(array_column($itemInfoDatasnew, 'old_id'));
        }
        if(count($item_id_arr) == 1){
            $where = " where is_del = 0 and item_id = ".$item_id_arr[0];
        }else if(count($item_id_arr) > 1){
            $where = " where is_del = 0 and item_id in (".implode(',', $item_id_arr).")";

        }
        //查询迁移前检查项数据
//        $dictionaryCheckcateDatasnew = array_flip($this->LocalService()->dictionaryCheckcategory->fetchCol('old_id',""));
//        $wheredictionaryCheckname['columns'] = ['id','old_id','old_category_id'];
//        $dictionaryChecknameDatasnew = $this->LocalService()->dictionaryCheckname->fetchAll($wheredictionaryCheckname);
//        if(!empty($dictionaryChecknameDatasnew)){
//            $dictionaryChecknameId = $dictionaryCheckcateId =  [];
//            foreach($dictionaryChecknameDatasnew as $key=>$val){
//                $dictionaryChecknameId[$val['old_id']] = $val['id'];
//                $dictionaryCheckcateId[$val['old_id']] = $val['old_category_id'];
//            }
//        }
        //查询字典项检查项数据
        $study = new \mysqli('localhost','root','root3306',"study");

        $study->select_db('study') or die('选择数据库失败：'.$study->error);

        $study->set_charset('utf8') or die('设置字符集失败：'.$study->error);

        $sql="select * from oa_dictionary_checkname";
        $dresult = $study->query($sql);
        $dictionaryChecknameDatas=[];
        while($rowd = $dresult->fetch_assoc())
        {
            $dictionaryChecknameDatas[]=$rowd;
        }
        if(!empty($dictionaryChecknameDatas)){
            $dictionaryChecknameId = $dictionaryCheckcateId =  [];
            foreach($dictionaryChecknameDatas as $key=>$val){
                $dictionaryChecknameId[$val['id']] = $val['id'];
                $dictionaryCheckcateId[$val['id']] = $val['category_id'];
            }
        }

        $yikeen = new \mysqli('localhost','root','root3306',"e-yda");

        $yikeen->select_db('e-yda') or die('选择数据库失败：'.$yikeen->error);

        $yikeen->set_charset('utf8') or die('设置字符集失败：'.$yikeen->error);
        //项目检查项
        $sql="select * from oa_item_ocr".$where;

        $result = $yikeen->query($sql);

        $ItemChecknameDatas=[];

        while($row = $result->fetch_assoc())
        {
            $ItemChecknameDatas[]=$row;
        }
        $dataKey = [];
        if(!empty($ItemChecknameDatas)){
            foreach ($ItemChecknameDatas as $key=>$val){
                $content = unserialize($val['content']);
                foreach($content as $k=>$v){
                        $checkKey = $val['item_id'].'-'.$v['checkname_id'];
                        if(!empty($dataKey[$checkKey])) continue;
                        $insertData['item_id'] = $itemInfoId[$val['item_id']];
                        $insertData['category_id'] = $dictionaryCheckcateId[$v['checkname_id']];
                        $insertData['checkname_id'] = $v['checkname_id'];
                        $insertData['name'] = $v['ocr_name'];
                        $insertData['variable'] = $v['variable'];
                        $insertData['check_type'] = $v['check_type'];
                        $insertData['unit'] = $v['unit'];
                        $insertData['max_val'] = $v['max_val'];
                        $insertData['min_val'] = $v['min_val'];
                        $insertData['alias'] = $v['alias'];
                        $insertData['is_del'] = $val['is_del'];
                        $insertData['create_time'] = time();
                        $insertData['old_id'] = $val['id'];
                        $insertData['old_item_id'] = $val['item_id'];
                        $insertData['old_item_sign_id'] = $val['center_id'];
                        $insertData['old_ocr_id'] = $val['ocr_id'];
                        $insertData['old_research_id'] = $val['research_id'];
                        $insertData['old_category_id'] = $dictionaryCheckcateId[$v['checkname_id']];
                        $insertData['old_checkname_id'] = $v['checkname_id'];
                        $dataKey[$checkKey] = 1;
                        $result=$this->LocalService()->itemCheckname->insert($insertData);
                }
                if(!empty($result)){
                    echo '<span style="color：green">'.$v['name'].'-'.'同步成功'.'<span>'.'<br>';
                }else{
                    echo '<span style="color：red">'.$v['name'].'-'.'同步失败'.'<span>'.'<br>';
                }
            }
            unset($dataKey);
        }else{
            echo '<span style="color：green">'.'暂无数据同步'.'<span>'.'<br>';
        }
        die;
    }

    /**
     * Notes: 获取对应数组
     * @param  $where 条件
     * @param  $filed  查询字段 $type 1 一对一 2 一对多
     * @return array
     * @author haojinhua
     * @date 2024-05-21
     */
    public function getFiledRelation($where,$key,$value,$filed,$type){
        $res = [];
        $where = empty($where) ? 'is_del = 0' : $where;
        $cond = [
            'where' => $where,
            'columns' => $filed,
            'order' => ['id'],
        ];
        $datas = $this->fetchAll($cond);
        if(!empty($datas)){
            foreach ($datas as $data){
                if($type == 1){
                    $res[$data[$key]] = $data[$value];
                }else{
                    $res[$data[$key]][] = $data[$value];
                }
            }
        }

        return $res;
    }

    /**
     * Notes:获取指定条件下的数据
     * User: lltyy
     * DateTime: 2024/5/15 9:20
     *
     * @param $where 条件
     * @param $columns 字段
     * @param string $key 键值
     * @param int $is_more 是否有多笔数据【0 否 1 是】
     * @return array
     */
    public function fetchDataByKey($where,$columns,$order=['id'],$key='id',$is_more = 0){
        $datas = [];
        $cond = [
            'where'=>$where,
            'columns'=>$columns,
            'order'=>$order
        ];
        $infoDatas = $this->fetchAll($cond);
        if(!empty($infoDatas)){
            foreach($infoDatas as $infoKey=>$infoData){
                $data_key = isset($infoData[$key]) && !empty($infoData[$key]) ? $infoData[$key] : '';
                if($is_more == 1){
                    $datas[$data_key][] = $infoData;
                }else{
                    $datas[$data_key] = $infoData;
                }
                unset($infoDatas[$data_key]);
            }
        }
        unset($cond);
        unset($infoDatas);

        return $datas;
    }
}