<?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 OcrCasePatientSpecialdetail extends \Application\Service\DB\AbstractDb {
    public function __construct($dbAdapter, ContainerInterface $container) {
        $this->tableName = 'yikeen_ocr_case_patient_specialdetail';
        parent::__construct($dbAdapter, $container);
    }

    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中心所有值都加
     *  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('ItemOcrCasePatientSpecialdetail');
        $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])) {
                    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;
    }


}