<?php
namespace Application\Service\DB\Admin;

use Interop\Container\ContainerInterface;
use Laminas\Db\Sql\Predicate\Operator;

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

    /***
     * Notes:更新本月工作的完成情况
     * User: lltyy
     * DateTime: 2024/12/23 11:04
     *
     * @return int
     */
    public function updateAllBatch(){
        $date_arr = [];

        //获取工作明细中最早的时间
        $firstDate = '';
        $workInfos = $this->LocalService()->adminWorkinfo->fetchAll([
            'where'=>'is_del = 0',
            'columns'=>['create_time'],
            'order'=>['create_time ASC']
        ]);
        $firstDate = !empty($workInfos[0]) ? date('Y-m',$workInfos[0]['create_time']) : '';
        unset($workInfos);
        if(!empty($firstDate)){
            $firstDate_arr = explode('-',$firstDate);
            $firstDate_year = intval($firstDate_arr[0]);
            $firstDate_m = intval($firstDate_arr[1]);
            $first_diff = 12-$firstDate_m;
            //当前年份的日期
            for($i=0;$i<=$first_diff;$i++){
                $date_m = $firstDate_m+$i;
                $date_str = $firstDate_year.'-'.$date_m;
                if($date_m < 10){
                    $date_str = $firstDate_year.'-0'.$date_m;
                }
                $date_arr[] = $date_str;
            }
            unset($first_diff);
            unset($firstDate_m);
            unset($firstDate_arr);

            //获取其他年份数据
            if(!empty($date_arr)){
                $diff_year = 2025-$firstDate_year;
                if($diff_year > 0){
                    for($yeari=1;$yeari<=$diff_year;$yeari++){
                        $year = $firstDate_year+$yeari;
                        $monthMax = $year == 2025 ? intval(date('m')) : 12;
                        for($monthi=1;$monthi<=$monthMax;$monthi++){
                            $date = $monthi < 10 ? $year.'-0'.$monthi : $year.'-'.$monthi;
                            $date_arr[] = $date;
                            unset($date);
                        }
                        unset($year);
                        unset($monthMax);
                    }
                }
                unset($diff_year);
            }
        }
        unset($firstDate);

        //更新每月统计信息
        if(!empty($date_arr)){
            foreach($date_arr as $date_info){
                $this->updateBatch(['now_date'=>$date_info]);
            }
        }
        unset($date_arr);

        return 1;
    }

    /***
     * Notes:更新本月工作的完成情况
     * User: lltyy
     * DateTime: 2024/12/23 11:04
     *
     * @return int
     */
    public function updateBatch(array $info=[]){
        $now_date = isset($info['now_date']) && !empty($info['now_date']) ? $info['now_date'] : date('Y-m');
        //if($now_date == date('Y-m')){
            //获取所有本月的批次ID
            $batch_arr = $this->fetchCol('id',['is_del'=>0,'now_date'=>$now_date]);
            if(!empty($batch_arr)){
                $this->LocalService()->adminWorkinfo->isSetInfo(false)->update(['batch_id'=>0],[
                    'is_del' => 0,
                    'batch_id' => array_values($batch_arr),
                ]);
                $this->update(['is_del'=>1],[
                    'is_del' => 0,
                    'id' => array_values($batch_arr),
                ]);
            }
            unset($batch_arr);
        //}
        $now_date_arr = explode('-',$now_date);
        $next_date = '';
        if(intval($now_date_arr[1]) == 12){
            $next_date = (intval($now_date_arr[0])+1).'-01';
        }else{
            $next_date_day = intval($now_date_arr[1])+1;
            $next_date = $now_date_arr[0].'-0'.$next_date_day;
            if($next_date_day > 9){
                $next_date = $now_date_arr[0].'-'.$next_date_day;
            }
            unset($next_date_day);
        }
        unset($now_date_arr);
        $start_date_time = strtotime($now_date.'-01 00:00:00');
        $end_date_time = strtotime($next_date.'-01 00:00:00');
        //获取截止时间未统计【无batch】的工作明细信息
        $workBatchArr = [];
        $workInfos = $this->LocalService()->adminWorkinfo->fetchAll([
            'where'=> [
                'is_del' => 0,
                'batch_id' => 0,
                new Operator('create_time', Operator::OP_LT, $end_date_time)
            ],
            'columns'=>['id','item_id','itemsig_id','patient_id','checktime_id','info_type','is_lock','update_time','update_user_id'],
            'order'=>['patient_id','checktime_id','info_type','create_time','update_time']
        ]);
        if(!empty($workInfos)){
            foreach($workInfos as $workInfoKey=>$workInfo){
                $workInfo_key = $workInfo['patient_id'].'_'.$workInfo['checktime_id'].'_'.$workInfo['info_type'];
                $workBatchArr[$workInfo_key] = isset($workBatchArr[$workInfo_key]) && !empty($workBatchArr[$workInfo_key]) ? $workBatchArr[$workInfo_key] : [];
                $workBatchArr[$workInfo_key]['item_id'] = $workInfo['item_id'];
                $workBatchArr[$workInfo_key]['itemsig_id'] = $workInfo['itemsig_id'];
                $workBatchArr[$workInfo_key]['patient_id'] = $workInfo['patient_id'];
                $workBatchArr[$workInfo_key]['checktime_id'] = $workInfo['checktime_id'];
                $workBatchArr[$workInfo_key]['info_type'] = $workInfo['info_type'];
                if($workInfo['is_lock'] == 1){
                    //判断锁定时间是否为截止日期之前的时间
                    if($workInfo['update_time'] < $end_date_time){
                        $workBatchArr[$workInfo_key]['lock'][] = [
                            'id'=>$workInfo['id'],
                            'update_time'=>$workInfo['update_time'],
                            'update_user_id'=>$workInfo['update_user_id']
                        ];
                        $workBatchArr[$workInfo_key]['lock_user'] = $workInfo['update_user_id'];
                        $workBatchArr[$workInfo_key]['lock_time'] = $workInfo['update_time'];
                    }else{//锁定时间为截止之后时间的话则截止月份算未锁定数据
                        $workBatchArr[$workInfo_key]['nolock'][] = $workInfo['id'];
                    }
                }else{
                    $workBatchArr[$workInfo_key]['nolock'][] = $workInfo['id'];
                }
                unset($workInfos[$workInfoKey]);
            }
        }
        unset($workInfos);

        //更新各类工作的完成情况
        if(!empty($workBatchArr)){
            foreach($workBatchArr as $workBatchKey=>$workBatch){
                $noLock = isset($workBatch['nolock']) && !empty($workBatch['nolock']) ? $workBatch['nolock'] : [];
                if(isset($workBatch['lock']) && !empty($workBatch['lock']) && empty($noLock)){
                    $save_batch = [
                        'now_date'=>$now_date,
                        'item_id'=>$workBatch['item_id'],
                        'itemsig_id'=>$workBatch['itemsig_id'],
                        'patient_id'=>$workBatch['patient_id'],
                        'checktime_id'=>$workBatch['checktime_id'],
                        'info_type'=>$workBatch['info_type'],
                        'create_user_id'=>$workBatch['lock_user'],
                        'create_time'=>$workBatch['lock_time'],
                        'is_over'=>1
                    ];
                    $batchid = $this->isSetInfo(false)->save($save_batch);
                    foreach($workBatch['lock'] as $workBatchLock){
                        $this->LocalService()->adminWorkinfo->isSetInfo(false)->update([
                            'batch_id'=>$batchid,
                            'update_user_id'=>$workBatchLock['update_user_id'],
                            'update_time'=>$workBatchLock['update_time']
                        ],[
                            'id' => $workBatchLock['id']
                        ]);
                    }
                    unset($batchid);
                }
                unset($workBatchArr[$workBatchKey]);
            }
        }
        unset($workBatchArr);

        return 1;
    }

}