<?php

namespace Application\Service\DB\Project;

use Interop\Container\ContainerInterface;
use Application\Service\Extension\MiniAppWs\App;
use Application\Service\Extension\Laminas;
use Laminas\Db\Sql\Expression;

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

    //字段n1为开始时间（入院时间），字段n5为结束时间（出院日期），
    public function hospitaltime($start_time,$end_time)
    {

        $endtimestamp = strtotime($end_time);
        $starttimestamp = strtotime($start_time);
        if($endtimestamp == false || $starttimestamp == false) {
            return 0;
        }

        $formattedDate = date('Y-m-d', strtotime($start_time));
        if(($formattedDate === $start_time) == false){
            return 0;
        }

        $formattedDate = date('Y-m-d', strtotime($end_time));
        if(($formattedDate === $end_time) == false){
            return 0;
        }

        $days = ($end_time - $start_time) / 86400;
        return ceil($days);
    }

    //itemOcrCsaePaitentDetailNew明细表的id====yikeen_ocr_case_patient_detail_new
    public function moneyCount($id){
        $fieldIdData =  $this->LocalService()->itemOcrTitleKeys->fetchCol('id','is_del=0 and sig_id=276  and is_use=1 and word_type =1 and id not in(9,11) and pid !=0');//,26,24,27,34
        $fieldIdDatas = [];
        foreach ($fieldIdData as $fieldIdDataKey=>$fieldIdDataValue){
            $fieldIdDatas['n'.$fieldIdDataValue] = 'n'.$fieldIdDataValue;
        }
        $where['where'] = [
            'is_del' => 0,
            'id' => $id,
        ];
        $itemOcrCsaePaitentDetailInfo = $this->LocalService()->itemOcrCsaePaitentDetailNew->fetchOne($where);
        $sumData = [];
        $sumDataY = [];
        if (!empty($itemOcrCsaePaitentDetailInfo)){
            foreach ($itemOcrCsaePaitentDetailInfo as $itemOcrCsaePaitentDetailInfoKey=>$itemOcrCsaePaitentDetailInfoValue){
                if(in_array($itemOcrCsaePaitentDetailInfoKey,$fieldIdDatas)){
                    if ( ($itemOcrCsaePaitentDetailInfoKey == 'n23') && empty($itemOcrCsaePaitentDetailInfo['n23']) && !empty($itemOcrCsaePaitentDetailInfo['n24'])){
                        $sumNumberO = $itemOcrCsaePaitentDetailInfoValue = (float)($itemOcrCsaePaitentDetailInfo['n24']);
                    }

                    if ( ($itemOcrCsaePaitentDetailInfoKey == 'n25') && empty($itemOcrCsaePaitentDetailInfo['n25'])){
                        $sumNumberT = $itemOcrCsaePaitentDetailInfoValue = (float)($itemOcrCsaePaitentDetailInfo['n26']) + (float)($itemOcrCsaePaitentDetailInfo['n27']);
                    }


                    if ( ($itemOcrCsaePaitentDetailInfoKey == 'n33') && empty($itemOcrCsaePaitentDetailInfo['n33'])){
                        $sumNumberS = $itemOcrCsaePaitentDetailInfoValue = (float)($itemOcrCsaePaitentDetailInfo['n34']);
                    }



                    $sumDataY[] = intval($itemOcrCsaePaitentDetailInfoValue);
                    if(($itemOcrCsaePaitentDetailInfoKey == 'n33') && preg_match("/^[1-9][0-9]*$/" ,$itemOcrCsaePaitentDetailInfoValue) && !empty($itemOcrCsaePaitentDetailInfoValue) && (intval($itemOcrCsaePaitentDetailInfoValue) > 10000)){
                        $sumData[] = $itemOcrCsaePaitentDetailInfoValue/100;
                    }else if(($itemOcrCsaePaitentDetailInfoKey == 'n14') && preg_match("/^[1-9][0-9]*$/" ,$itemOcrCsaePaitentDetailInfoValue) && !empty($itemOcrCsaePaitentDetailInfoValue) && (intval($itemOcrCsaePaitentDetailInfoValue) > 10000)){
                        $sumData[] = $itemOcrCsaePaitentDetailInfoValue/100;
                    }else if(($itemOcrCsaePaitentDetailInfoKey == 'n15') && preg_match("/^[1-9][0-9]*$/" ,$itemOcrCsaePaitentDetailInfoValue) && !empty($itemOcrCsaePaitentDetailInfoValue) && (intval($itemOcrCsaePaitentDetailInfoValue) > 10000)){
                        $sumData[] = $itemOcrCsaePaitentDetailInfoValue/100;
                    }else if(($itemOcrCsaePaitentDetailInfoKey == 'n19') && preg_match("/^[1-9][0-9]*$/" ,$itemOcrCsaePaitentDetailInfoValue) && !empty($itemOcrCsaePaitentDetailInfoValue) && (intval($itemOcrCsaePaitentDetailInfoValue) > 10000)){
                        $sumData[] = $itemOcrCsaePaitentDetailInfoValue/100;
                    }else if(($itemOcrCsaePaitentDetailInfoKey == 'n46') && preg_match("/^[1-9][0-9]*$/" ,$itemOcrCsaePaitentDetailInfoValue) && !empty($itemOcrCsaePaitentDetailInfoValue) && (intval($itemOcrCsaePaitentDetailInfoValue) > 1000)){
                        $sumData[] = $itemOcrCsaePaitentDetailInfoValue/100;
                    }else if(($itemOcrCsaePaitentDetailInfoKey == 'n36') && preg_match("/^[1-9][0-9]*$/" ,$itemOcrCsaePaitentDetailInfoValue) && !empty($itemOcrCsaePaitentDetailInfoValue) && (intval($itemOcrCsaePaitentDetailInfoValue) > 100000)){
                        $sumData[] = $itemOcrCsaePaitentDetailInfoValue/100;
                    }else if(($itemOcrCsaePaitentDetailInfoKey == 'n49') && preg_match("/^[1-9][0-9]*$/" ,$itemOcrCsaePaitentDetailInfoValue) && !empty($itemOcrCsaePaitentDetailInfoValue) && (intval($itemOcrCsaePaitentDetailInfoValue) > 10000)){
                        $sumData[] = $itemOcrCsaePaitentDetailInfoValue/100;
                    }else if(($itemOcrCsaePaitentDetailInfoKey == 'n21') && preg_match("/^[1-9][0-9]*$/" ,$itemOcrCsaePaitentDetailInfoValue) && !empty($itemOcrCsaePaitentDetailInfoValue) && (intval($itemOcrCsaePaitentDetailInfoValue) > 1000)){
                        $sumData[] = $itemOcrCsaePaitentDetailInfoValue/100;
                    }else if(($itemOcrCsaePaitentDetailInfoKey == 'n13') && preg_match("/^[1-9][0-9]*$/" ,$itemOcrCsaePaitentDetailInfoValue) && !empty($itemOcrCsaePaitentDetailInfoValue) && (intval($itemOcrCsaePaitentDetailInfoValue) > 10000)){
                        $sumData[] = $itemOcrCsaePaitentDetailInfoValue/100;
                    }else{
                        $sumData[] = intval($itemOcrCsaePaitentDetailInfoValue);
                    }

                }
            }
        }

        $moneyCount = array_sum($sumData);
        $moneyCountY = array_sum($sumDataY);
        $data['moneyCount'] = $moneyCount;
        $data['moneyCountY'] = $moneyCountY;
//        var_dump($data);die;
        return $data;

    }
    public function compareCount($itemOcrCsaePaitentDetailInfo,$moneyCountY){
        $n9 = $itemOcrCsaePaitentDetailInfo['n9'];
        $n9 = (float)($n9);
//        $n9_1 = $n9*0.15;
        $moneyYAndN9 = $moneyCountY-$n9;
        $flags = 0;
        if  ($moneyYAndN9 < 0 && ($moneyCountY > $n9)){
            $flags = 1;
        }
        $fieldIdDataJ = ['n23','n24','n25','n26','n27','n33','n34'];
        $fieldIdDataKey = '';
        $diffMoneys = '';
        if (!empty($itemOcrCsaePaitentDetailInfo['n9']) && $itemOcrCsaePaitentDetailInfo['n9'] > 100){
            foreach ($itemOcrCsaePaitentDetailInfo as $itemOcrCsaePaitentDetailInfoKey=>$itemOcrCsaePaitentDetailInfoValue){
                $diffMoneyYAndN9 = abs($moneyYAndN9);
                if  (in_array($itemOcrCsaePaitentDetailInfoKey,$fieldIdDataJ) && !empty($flags) && !empty($itemOcrCsaePaitentDetailInfoValue)){
                    $newDiffMoneyYAndN9  = (float)($itemOcrCsaePaitentDetailInfoValue) - $diffMoneyYAndN9;
                    if ($newDiffMoneyYAndN9 >0 && $newDiffMoneyYAndN9 < 200){
                        $fieldIdDataKey = $itemOcrCsaePaitentDetailInfoKey;
                        $diffMoneys = $itemOcrCsaePaitentDetailInfoValue;
                        break;
                    }

                }
            }
        }
        return ['fieldIdDataKey' =>$fieldIdDataKey,'diffMoneys'=>$diffMoneys];
    }


    private function Introduce($formValidator, array $params = []){
        $path=APP_PATH.'/formData/';
        $fromMap=include $path.'formMap.php';
        $fromData=include $path.ucfirst($formValidator).'Data.php';
        if (!empty($fromData)){
            if(is_callable($fromData)) return $fromData($params);
            return $fromData;
        }else{
            return [];
        }
    }
    /**
     * Notes: 依据条件 重新计算推算金额 与 金额差
     * User: lltyy
     * DateTime: 2023/8/27 21:11
     * $data ：计算数据
     * $id :信息ID
     *推算金额获取规则
     *  1》2、51中心所有值都加
     *  2》其他中心计算规则如下
     *      1》如果 n24 与 n23 差值 小于 1 ，则只加 n24 否则两个都加
     *      2》如果 n25 与【n26、n27之和】差值小于 1，则只加 n25 ，否则 三个都加
     *      3》如果 n33 与 n34 差值小于 1，则只加 n33 ，否则 两个都加
     * @param $where
     */
    public function newMoneySum(array $data = [],int $id = 0){
        $formFiledData = $this->Introduce('ItemOcrCsaePaitentDetailNew');
        $parentSumFiledArr = $childSumFiledArr = $allSumFiledArr = [];
        if(!empty($formFiledData)){
            foreach($formFiledData as $formFiledKey=>$formFiled){
                if(isset($formFiled['isHide']) &&  $formFiled['isHide'] == 1){
                    $parentSumFiledArr[] = $formFiledKey;
                    $allSumFiledArr[] = $formFiledKey;
                }
                if(isset($formFiled['isHide']) &&  $formFiled['isHide'] == 2){
                    $childSumFiledArr[$formFiledKey] = $formFiled['parentProp'];
                    $allSumFiledArr[] = $formFiledKey;
                }
            }
        }

        $id = isset($id) && !empty($id) ? $id : 0;
        if(!empty($id)){
            $data = $this->fetchOne(['where'=>'id = '.$id]);
        }

        $arr = [];
        if(!empty($data)){
            $sig_id = isset($data['sig_id']) && !empty($data['sig_id']) ? $data['sig_id'] : 0;
            //获取推算金额
            $n51 = floatval(0);

            $childMoneyArr = [];
            foreach($data as $key=>$value){
                if(in_array($key,$allSumFiledArr)){
                    $value = !is_null($value) && !empty($value) ? $value : 0;
                    $value = floatval($value);
                    if(in_array($key,$parentSumFiledArr)){
                        $n51 += $value;
                    }else{
                        $childParentKey = isset($childSumFiledArr[$key]) && !empty($childSumFiledArr[$key]) ? $childSumFiledArr[$key] : '';
                        if($childParentKey != ''){
                            $childMoneyArr[$childParentKey] = isset($childMoneyArr[$childParentKey]) && !empty($childMoneyArr[$childParentKey]) ? $childMoneyArr[$childParentKey] : 0;
                            $childMoneyArr[$childParentKey] = floatval($childMoneyArr[$childParentKey]);
                            $childMoneyArr[$childParentKey] += $value;
                        }
                    }
                }
            }

            if (!empty($childMoneyArr)) {
                //2中心 所有值都加
                if (in_array($sig_id, [276,12075])) {
                    foreach ($childMoneyArr as $childMoney) {
                        $n51 += $childMoney;
                    }
                } else {
                    //其他中心子级增加判断
                    foreach ($childMoneyArr as $parentKey => $childSum) {
                        $parentKey_money = isset($data[$parentKey]) && !is_null($data[$parentKey]) && !empty($data[$parentKey]) ? $data[$parentKey] : 0;
                        $parentKey_money = floatval($parentKey_money);
                        $diff_money = $childSum - $parentKey_money;
                        if ($diff_money > 1) {
                            $n51 += $childSum;
                        }
                    }
                }
            }
            unset($childMoneyArr);

            $arr['n51'] = $n51;

            //获取金额差
            if(isset($data['n9'])){
                $n9 = !is_null($data['n9']) && !empty($data['n9']) ? $data['n9'] : 0;
                $n9 = floatval($n9);
                $differenceMoney = $n9 != 0 || $n51 != 0 ? abs($n9 - $n51) : 0;
                $differenceMoney_arr = explode('.',$differenceMoney);
                if(isset($differenceMoney_arr[1])){
                    $differenceMoney = $differenceMoney_arr[0].".".$differenceMoney_arr[1];
                    if(strlen($differenceMoney_arr[1]) > 2){
                        $differenceMoney = $differenceMoney_arr[0].".".substr($differenceMoney_arr[1],0,2);
                    }
                }
                $arr['differenceMoney'] = $differenceMoney;
            }
        }
        return $arr;
    }


}