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

namespace Application\Service\DB\Item;

use Interop\Container\ContainerInterface;

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

    /**
     * Notes: 研究阶段复制yikeen_item_researchstage
     * @param string old_item_id 要复制的项目ID
     * @param string new_item_id 新项目ID
     * @return string
     */
    public function CopyResearchstage($old_item_id,$new_item_id){
        if(empty($old_item_id)){
            return 'old_item_id 为空';
        }else{
            if(!is_numeric($old_item_id)){
                return 'old_item_id 参数不正确';
            }
        }
        if(empty($new_item_id)){
            return 'new_item_id 为空';
        }else{
            if(!is_numeric($new_item_id)){
                return 'new_item_id 参数不正确';
            }
        }
        $where['where'] = [
            'is_del' => 0,
            'item_id' => $old_item_id
        ];
        $ResearchstageDatas=$this->fetchAll($where);
        foreach ($ResearchstageDatas as $key => $val) {
            $insert_arr = [
                'item_id' => $new_item_id, 'is_skip' => $val['is_skip'], 'stage_name' => $val['stage_name'],
                'stage_id' => $val['stage_id'], 'time_unit' => $val['time_unit'], 'stage_remark' => $val['stage_remark'],
                'stage_order' => $val['stage_order'], 'is_del' => $val['is_del'], 'is_booked' => $val['is_booked'],
                'is_same' => $val['is_same'], 'remind_time' => $val['remind_time'], 'is_show' => $val['is_show'],
                'stage_time' => $val['stage_time'],'check_time' => $val['check_time']
            ];
            $result = $this->LocalService()->itemResearchstage->insert($insert_arr);
            $this->LocalService()->itemChecktime->update(['stage_id'=>$result],[
                'item_id' => $new_item_id,
                'stage_id' => $val['id'],
            ]);
        }
    }

    /**
     * Notes:获取指定条件下的数据
     * @param string $where 条件
     * @param string[] $columns 字段
     * @param string[] $order 排序
     * @param string $key 键值
     * @param int $is_more 是否有多笔【1 是 0 否】
     * @return array
     * @return
     * @author haojinhua
     * @date 2024-05-28
     */
    public function fetchDataByKey($where='is_del = 0',$columns=['id'],$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 $infoData){
                $data_key = $infoData[$key];
                if($is_more == 1){
                    $datas[$data_key][] = $infoData;
                }else{
                    $datas[$data_key] = $infoData;
                }
            }
        }
        unset($cond);
        unset($infoDatas);
        return $datas;
    }

}