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

namespace Application\Service\DB\Item;

use Interop\Container\ContainerInterface;
use Application\Service\DB\Db;

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


    function copyData(array $data){
        $copy_item_id = $data['copy_item_id'];
        $item_id = $data['item_id'];
        $type = $data['type'];
        $fid = $data['fid'];

        if(empty($copy_item_id)){
            return 'copy_item_id 为空';
        }else{
            if(!is_numeric($copy_item_id)){
                return 'copy_item_id 参数不正确';
            }
        }
        if(empty($item_id)){
            return 'new_item_id 为空';
        }else{
            if(!is_numeric($item_id)){
                return 'new_item_id 参数不正确';
            }
        }
        switch($type){
            case 1:
                //独立数据
                Db::beginTransaction();
                //复制研究分层设置
                $this->LocalService()->itemRandgroup->itemRandgroupInsert($copy_item_id,$item_id);
                //药物分组设置
                $this->LocalService()->itemMedication->itemMedicationInsert($copy_item_id,$item_id);
                //受试者信息设置
                $this->LocalService()->patientattrs->patientattrInsert($copy_item_id,$item_id);
                Db::commit();
                break;
            case 2:
                //复制检查项
                $this->LocalService()->itemCheckname->ViceinfoAdd($copy_item_id,$item_id);
                break;
            case 3:
                //关联数据
                Db::beginTransaction();
                //随机设置
                $this->LocalService()->itemRandom->CopyRandom($copy_item_id,$item_id);
                //研究阶段设置
                $this->LocalService()->itemResearchstage->CopyResearchstage($copy_item_id,$item_id);
                //检查点设置
                $checktimeMap = $this->LocalService()->itemChecktime->CopyChecktime($copy_item_id,$item_id);
                //表单设置
                $this->LocalService()->itemForm->cloneForm($copy_item_id,$item_id,$checktimeMap,$fid);

                Db::commit();
                    $infodata['is_mainitem '] = 0;
                    $upinfo = $this->LocalService()->itemInfo->update($infodata,'id = '.$item_id);
                    //保存日志  编辑：1
                    if (!empty($upinfo)) $this->LocalService()->log->saveFileLog($item_id,'itemInfo.Form',$infodata,1,'',0,'viceForm');
                break;
        }
        return true;

    }


}