<?php
namespace Application\Service\DB\Item;
use Application\Service\DB\AbstractDb;
use Interop\Container\ContainerInterface;
use Laminas\Db\Sql\Predicate\Operator;

class Randgroup extends AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        $this->tableName = 'yikeen_item_randgroup';
        parent::__construct($dbAdapter, $container);
    }

    public function itemRandgroupInsert($oldItemId,$newItemId){
        $whereOld['where'] = [
            'is_del' => 0,
            'item_id' => $oldItemId,
        ];
        $whereNew['where'] = [
            'is_del' => 0,
            'item_id' => $newItemId,
        ];
        $maxOrder = $this->LocalService()->itemRandgroup->getMaxOrder('randgroup_order', $whereNew['where']);
        $data = $this->LocalService()->itemRandgroup->fetchAll($whereOld);
        if (!empty($data)){
            foreach ($data as $k => $v) {
                unset($v['id']);
                $v['item_id'] = $newItemId;
                $v['randgroup_order'] = $maxOrder;
                $maxOrder += 10;
                $result = $this->LocalService()->itemRandgroup->save($v);
            }
        }
    }

    /*
     * 研究分层
     * */
    function oldItemRandgroupInsert($item_id = 1){
        $iteminfoData = $this->LocalService()->itemInfo->fetchCol('old_id','is_del=0');
        $itemIds = implode(',',$iteminfoData);
        $randgroupData = $this->LocalService()->patientattrs->dbData('oa_item_randgroup',[
            'is_del' => 0,
            'item_id' => $itemInfoData
        ]);

        foreach ($randgroupData as $k=>&$v){
            foreach ($iteminfoData as $iteminfoDataKey=>$iteminfoDataValue) {
                if ($v['item_id'] == $iteminfoDataValue){
                    $v['item_id'] = $iteminfoDataKey;
                    $v['item_id_old'] = $iteminfoDataValue;
                    $v['id_old'] = $v['id'];
                    unset($v['id']);
                    $result=$this->LocalService()->itemRandgroup->insert($v);
                    if(!empty($result)){
                        echo '<span style="color：green">'.$v['name'].'-'.'同步成功'.'<span>'.'<br>';
                    }else{
                        echo '<span style="color：red">'.$v['name'].'-'.'同步失败'.'<span>'.'<br>';
                    }
                }
            }
        }
        die;
        echo '<pre>';print_r($iteminfoData);die;

    }
    /*
  *随机号
  * */
    public function oldItemRandnumberInsert($item_id = 83){
        $iteminfoId= $this->LocalService()->itemInfo->getOneFieldVal('id',[
            'is_del' => 0,
            'old_id' => $item_id,
        ]);
        $iteminfoBigId= $this->LocalService()->itemInfo->getOneFieldVal('fid',[
            'is_del' => 0,
            'id' => $iteminfoId,
        ]);

        //中心
        $whereItemSignatory['where'] = [
            'is_del' => 0,
            'item_id' => $iteminfoBigId,
        ];
        $whereItemSignatory['columns'] = ['id','old_id'];
        $itemSignatoryIdData = $this->LocalService()->itemSignatory->fetchAll($whereItemSignatory);

        $signtoryDatas = [];
        if (!empty($itemSignatoryIdData)){
            foreach($itemSignatoryIdData as $k=>$v)
            {
                $signtoryDatas[$v['old_id']] = $v;
            }
        }

        //区号
        $blockgroupData = $this->LocalService()->patientattrs->dbData('oa_item_blockgroup',[
            'is_del' => 0,
            'item_id' => explode(',', $item_id),
        ]);// and programme_id !=0
        $blockgroupDatas = [];
        if (!empty($blockgroupData)){
            foreach($blockgroupData as $blockgroupDataKey=>$blockgroupDataValue)
            {
                $blockgroupDatas[$blockgroupDataValue['id']] = $blockgroupDataValue;
            }
        }

        //方案分组
        $whereItemRandom['where'] = [
            'is_del' => 0,
            'old_item_id' => $item_id,
        ];
        $whereItemRandom['columns'] = ['id','old_random_id'];
        $itemRandomIdData = $this->LocalService()->itemRandom->fetchAll($whereItemRandom);
        $itemRandomDatas = [];
        if (!empty($itemRandomIdData)){
            foreach($itemRandomIdData as $itemRandomIdDataKey=>$itemRandomIdDataDataValue)
            {
                $itemRandomDatas[$itemRandomIdDataDataValue['old_random_id']] = $itemRandomIdDataDataValue;
            }
            //block====
            $randblockData = $this->LocalService()->patientattrs->dbData('oa_item_randblock',[
                'is_del' => 0,
                'item_id' => explode(',', $item_id),
            ]);// and programme_id !=0
            foreach ($randblockData as $randblockDataKey=>&$randblockDataValue) {
                $randblockDataValue['item_id'] = $iteminfoId;
                $randblockDataValue['item_id_old'] = $item_id;
                foreach($itemRandomIdData as $itemRandomIdDataKey1=>$itemRandomIdDataDataValue1)
                {
                    $randblockDataValue['programme_id'] =  $itemRandomIdDataDataValue1['id'];
                    unset($randblockDataValue['id']);
                    //block
                    $blockId = $this->LocalService()->itemRandblock->save($randblockDataValue);
                }
            }
        }

        //研究分层
        $whereItemRandgroup['where'] = [
            'is_del' => 0,
            'item_id_old' => $item_id,
        ];
        $whereItemRandgroup['columns'] = ['id','id_old'];
        $itemRandgroupIdData = $this->LocalService()->itemRandgroup->fetchAll($whereItemRandgroup);
        $itemRandgroupDatas = [];
        if (!empty($itemRandgroupIdData)){
            foreach($itemRandgroupIdData as $itemRandgroupIdDataKey=>$itemRandgroupIdDataValue)
            {
                $itemRandgroupDatas[$itemRandgroupIdDataValue['id_old']] = $itemRandgroupIdDataValue;
            }
        }

        if(empty($iteminfoId)) return false;
        $randnumberData = $this->LocalService()->patientattrs->dbData('oa_item_randnumber',[
            'is_del' => 0,
            'item_id' => explode(',', $item_id),
        ]);// and programme_id !=0
        if(!empty($randnumberData)){
            $randnumberIdArr = array_column($randnumberData,'id');
            $randnumberIds = implode(',',$randnumberIdArr);

            $randomdetailsData = $this->LocalService()->patientattrs->dbData('oa_item_randomdetails',[
                'item_id' => explode(',', $item_id),
                'randnumber_id' => $randnumberIdArr,
                new Operator('programme_id', Operator::OP_NE, 0),
            ]);

            $whereNewPatient['where'] = [
                'is_del' => 0,
                'item_id_old' => $item_id,
            ];
            $whereNewPatient['columns'] = ['id','random_number_id_old','random_id_old','randgroup_id_old'];
            $patientData = $this->LocalService()->patient->fetchAll($whereNewPatient);

            $randnumberIdArr = [];
            foreach ($randnumberData as $randnumberDataKey=>&$randnumberDataValue){
                $randnumberDataValue['id_old'] = $old_id = $randnumberDataValue['id'];
                $randnumberDataValue['randgroup_id_old'] = $randnumberDataValue['randgroup_id'];
                $randnumberDataValue['item_id_old'] = $item_id;
                $randnumberDataValue['item_id'] = $iteminfoId;
                $randnumberDataValue['item_sign_id_old'] = $randnumberDataValue['item_sign_id'];
                $randnumberDataValue['item_sign_id'] = isset($signtoryDatas[$randnumberDataValue['item_sign_id']]['id'])?$signtoryDatas[$randnumberDataValue['item_sign_id']]['id']:0;
//                $randnumberDataValue['programme_id'] = isset($itemRandomDatas[$randnumberDataValue['programme_id']])?$itemRandomDatas[$randnumberDataValue['programme_id']]:0;
                $randnumberDataValue['randgroup_id'] = isset($itemRandgroupDatas[$randnumberDataValue['randgroup_id']]['id'])?$itemRandgroupDatas[$randnumberDataValue['randgroup_id']]['id']:0;
                unset($randnumberDataValue['id']);
//                unset($randnumberDataValue['item_id']);
                $randnumberId = $this->LocalService()->itemRandnumber->save($randnumberDataValue);
                $randnumberIdArr[$old_id] = $randnumberId;

            }
            $randnumberProgrammeId = [];
            $patientRandnumberProgrammeArr = [];
            foreach ($randomdetailsData as $randomdetailsDataKey=>$randomdetailsDataValue) {

                $randomdetailsDataValue['id_old'] = $randomdetailsDataValue['id'];
                $randomdetailsDataValue['item_id_old'] = $item_id;
                $randomdetailsDataValue['item_id'] = $iteminfoId;

                $randomdetailsDataValue['randgroup_id_old'] = $randomdetailsDataValue['randgroup_id'];
                //研究
                $randomdetailsDataValue['randgroup_id'] = isset($itemRandgroupDatas[$randomdetailsDataValue['randgroup_id']]['id'])?$itemRandgroupDatas[$randomdetailsDataValue['randgroup_id']]['id']:0;


                $randomdetailsDataValue['randnumber_id_old'] = $randomdetailsDataValue['randnumber_id'];
                //分组
                $randomdetailsDataValue['randnumber_id'] = isset($randnumberIdArr[$randomdetailsDataValue['randnumber_id']])?$randnumberIdArr[$randomdetailsDataValue['randnumber_id']]:0;

                $randomdetailsDataValue['programme_id_old'] = $randomdetailsDataValue['programme_id'];
                //方案
                $randomdetailsDataValue['programme_id'] = isset($itemRandomDatas[$randomdetailsDataValue['programme_id']]['id'])?$itemRandomDatas[$randomdetailsDataValue['programme_id']]['id']:0;

                $randomdetailsDataValue['item_sign_id_old'] = $randomdetailsDataValue['item_sign_id'];
                //中心
                $randomdetailsDataValue['item_sign_id'] = isset($signtoryDatas[$randomdetailsDataValue['item_sign_id']]['id'])?$signtoryDatas[$randomdetailsDataValue['item_sign_id']]['id']:0;
                //区号
                $randomdetailsDataValue['area_code'] = isset($blockgroupDatas[$randomdetailsDataValue['blockgroup_id']]['block_num'])?$blockgroupDatas[$randomdetailsDataValue['blockgroup_id']]['block_num']:0;
                $randomdetailsDataValue['blockgroup_id_old'] = $randomdetailsDataValue['blockgroup_id'];
                if (!empty($randomdetailsDataValue['randnumber_id']) && !empty($randomdetailsDataValue['programme_id'])){
                    $randnumberProgrammeId[$randomdetailsDataValue['randnumber_id']] = $randomdetailsDataValue['programme_id'];
                }

                unset($randomdetailsDataValue['id']);
                $patientattrResult = $this->LocalService()->itemRandomdetails->save($randomdetailsDataValue);
            }

            if (!empty($randnumberProgrammeId)){
                foreach ($randnumberProgrammeId as $randnumberProgrammeIdKey=>$randnumberProgrammeIdValue) {
                    $up['id'] = $randnumberProgrammeIdKey;
                    $up['programme_id'] = $randnumberProgrammeIdValue;
                    $randnumberId = $this->LocalService()->itemRandnumber->save($up);
                }
            }

            if (!empty($patientData)){
                foreach ($patientData as $patientDataKey=>&$patientDataValue) {
                    //分组
                    $patientDataValue['random_number_id'] = isset($randnumberIdArr[$patientDataValue['random_number_id_old']])?$randnumberIdArr[$patientDataValue['random_number_id_old']]:$patientDataValue['random_number_id_old'];

                    //方案
                    $patientDataValue['random_id_old'] = isset($itemRandomDatas[$patientDataValue['random_id_old']]['id'])?$itemRandomDatas[$patientDataValue['random_id_old']]['id']:$patientDataValue['random_id_old'];

                    //分组
                    if (isset($itemRandgroupDatas[$patientDataValue['randgroup_id_old']]['id'])){
                        $patientDataValue['randgroup_id'] = $itemRandgroupDatas[$patientDataValue['randgroup_id_old']]['id'];

                    }
                    $patientId = $this->LocalService()->patient->save($patientDataValue);
                }
            }
        }

        if(!empty($patientattrResult)){
            echo '<span style="color：green">'.$randnumberDataValue['id'].'-'.'同步成功'.'<span>'.'<br>';
        }else{
            echo '<span style="color：red">'.$randnumberDataValue['id'].'-'.'同步失败'.'<span>'.'<br>';
        }
        die;
    }
}

