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

namespace Application\Service\DB\Worklist;

use Interop\Container\ContainerInterface;

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

    /**
     * Notes:获取指定条件下的数据，category_info 为键值，信息为值
     * User: lltyy
     * DateTime: 2022/10/19 15:58
     *
     * @param $where
     */
    public function fetchDataByCategoryAndInfo($where){
        $data = [];
        //获取项目工作清单信息
        $where_cond = [
            'where'=>$where,
            'columns'=>['id','category_id','info_id','workset_value'],
            'order'=>['id']
        ];
        $work_datas = $this->fetchAll($where_cond);
        if(!empty($work_datas)){
            foreach($work_datas as $work_data){
                $work_key = $work_data['category_id'].'_'.$work_data['info_id'];
                $data[$work_key] = $work_data;
            }
        }
        return $data;
    }

    /**
     * Notes:同步设置信息【目前只适用work_type=1的数据】
     * User: lltyy
     * DateTime: 2022/10/19 15:47
     *
     * @param $arr
     */
    public function updateSameSet($arr){
        $parent_item = $arr['parent_item'] ? $arr['parent_item'] : 0;
        $item_id = $arr['item_id'] ? $arr['item_id'] : 0;
        $work_type = $arr['work_type'] ? $arr['work_type'] : 0;
        //获取已有数据信息
        $worklistsetDatas = $this->fetchDataByCategoryAndInfo([
            'is_del' => 0,
            'work_type' => $work_type,
            'item_id' => $item_id,
        ]);
        $worklistDatas = $this->LocalService()->worklistSigworklist->getDataByWork([
            'is_del' => 0,
            'work_type' => $work_type,
        ],1);

        foreach ($arr['update_same'] as $sameCategoryInfo=>$sameDatas){
            $update_worklistset = [];
            $sameCategoryInfo_arr = explode('_',$sameCategoryInfo);
            $update_same = [
                'parent_item' => $parent_item,
                'item_id' => $item_id,
                'work_type' => $work_type,
                'category_id' => empty($sameCategoryInfo_arr[0]) ? 0 : $sameCategoryInfo_arr[0],
                'info_id' => empty($sameCategoryInfo_arr[1]) ? 0 : $sameCategoryInfo_arr[1]
            ];
            $new_workset_value_arr = [];
            if(isset($worklistsetDatas[$sameCategoryInfo])){
                $update_same['id'] = $worklistsetDatas[$sameCategoryInfo]['id'];
                $workset_value_arr = json_decode($worklistsetDatas[$sameCategoryInfo]['workset_value'],true);
                if(!empty($workset_value_arr)){
                    foreach($workset_value_arr as $workset_value_code=>$workset_value){
                        if(isset($sameDatas[$workset_value_code])){
                            $sameData_workset_id = $sameDatas[$workset_value_code]['workset_id'];
                            unset($sameDatas[$workset_value_code]['workset_id']);
                            $new_workset_value_arr[$workset_value_code] = empty($sameDatas[$workset_value_code]['work_value']) ? "" : $sameDatas[$workset_value_code]['work_value'];
                            $update_worklistset[$sameData_workset_id] = empty($sameDatas[$workset_value_code]['work_value']) ? "" : $sameDatas[$workset_value_code]['work_value'];
                            unset($sameDatas[$workset_value_code]);
                        }else{
                            $new_workset_value_arr[$workset_value_code] = $workset_value;
                        }
                    }
                }
            }
            if(!empty($sameDatas)){
                foreach($sameDatas as $sameDate_worksetCode=>$sameData){
                    $sameData_worksetid = $sameData['workset_id'];
                    unset($sameData['workset_id']);
                    $new_workset_value_arr[$sameDate_worksetCode] = empty($sameData['work_value']) ? "" : $sameData['work_value'];
                    $update_worklistset[$sameData_worksetid] = empty($sameData['work_value']) ? "" : $sameData['work_value'];
                    unset($sameDatas[$sameDate_worksetCode]);
                }
            }
            $new_workset_value_arr = array_merge($update_same,$new_workset_value_arr);
            $update_same['workset_value'] = json_encode($new_workset_value_arr);
            $worklist_result = $this->save($update_same);

            if($worklist_result && !empty($update_worklistset)){
                foreach($update_worklistset as $workset_id=>$update_worklist_workset_value){
                    $update_worklist = [
                        'parent_item'=>$parent_item,
                        'item_id'=>$item_id,
                        'work_type' => $work_type,
                        'category_id' => empty($sameCategoryInfo_arr[0]) ? 0 : $sameCategoryInfo_arr[0],
                        'info_id' => empty($sameCategoryInfo_arr[1]) ? 0 : $sameCategoryInfo_arr[1],
                        'worklistset_id' => empty($update_same['id']) ? $worklist_result : $update_same['id'],
                        'workset_id'=>$workset_id,
                        'workset_value'=>empty($update_worklist_workset_value) ? '' : $update_worklist_workset_value
                    ];
                    if(isset($worklistDatas[$sameCategoryInfo]) && isset($worklistDatas[$sameCategoryInfo][$workset_id])){
                        $update_worklist['id'] = $worklistDatas[$sameCategoryInfo][$workset_id]['id'];
                    }
                    $this->LocalService()->worklistItemworklist->save($update_worklist);
                }
            }
        }

        return 200;
    }

    /**
     * Notes:获取项目工作进度人员
     * User: lltyy
     * DateTime: 2022/11/14 8:59
     *
     * @param $item_id //项目ID
     * @param int $return_type //返回类型【0：下拉数据； 1：人员ID为键值，名称为值的数组】
     * @return array
     */
    public function fetchUserData($item_id,$return_type=0){
        $arr = [];
        //获取项目工作进度人员
        $item_workuser_cond = [
            'where'=> [
                'is_del' => 0,
                'work_type' => 3,
                'info_id' => 0,
                'parent_item' => $item_id,
            ],
            'columns'=>['id','workset_value']
        ];
        $item_workusers = $this->fetchAll($item_workuser_cond);
        if(!empty($item_workusers)){
            foreach($item_workusers as $item_workuser){
                $item_workuser_value = json_decode($item_workuser['workset_value'],true);
                $item_workuser_id = '0_'.$item_workuser['id'];
                $item_workuser_name = '【项目人员】'.$item_workuser_value['userName'];
                if(isset($item_workuser_value['userJob']) && $item_workuser_value['userJob'] != ''){
                    $item_workuser_name .= '('.$item_workuser_value['userJob'].')';
                }
                if($return_type == 1){
                    $arr[$item_workuser_id] = $item_workuser_name;
                }else{
                    $arr[] = [
                        'value'=>$item_workuser_id,
                        'label'=>$item_workuser_name
                    ];
                }
            }
        }
        return $arr;
    }
}