<?php

namespace Application\Service\DB\Project;

use Interop\Container\ContainerInterface;

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

    //编辑，添加
    public function change($data){
        if (empty($data)) return false;
            $newData = $this->backupschangeField($data);

            $id = (isset($data['id']) && !empty($data['id']))?$data['id']:0;
            $change = $newData['change'];
            $backupschange = $newData['backupschange'];
            if(empty($id)){
                unset($change['id']);
                $id = $this->LocalService()->itemIdentificationresultchange->save($change);
            }else{
                $this->LocalService()->itemIdentificationresultchange->save($change);
                $id = $data['id'];
            }
            if (!empty($id)){
                if (!empty($backupschange)){
                    $backupschange['patient_change_id'] = $id;
                    $whereItemBackupschange['where'] = [
                        'is_del' => 0,
                        'patient_change_id' => $id
                    ];
                    $itemBackupschangeInfo = $this->LocalService()->itemBackupschange->fetchOne($whereItemBackupschange);
                    if(empty($itemBackupschangeInfo)){
                        $backupschangeId = $this->LocalService()->itemBackupschange->save($backupschange);
                    }else{
                        $backupschangeId = $this->LocalService()->itemBackupschange->update($backupschange,'id='.$itemBackupschangeInfo['id']);
                    }

                }
            }
        return true;
    }
    public function backupschangeField($dataField){

        $changeArr = [];
        $backupschangeArr = [];
        $backupschange = ['item_min_val','item_max_val','item_unit','item_result','user_name','remarks','item_min_range','item_max_range'];//,'patient_change_id'
        foreach ($dataField as $dataFieldKey=>$dataFieldValue){
            if (in_array($dataFieldKey,$backupschange)){
                $backupschangeArr[$dataFieldKey] = $dataFieldValue;
            }else{
                $changeArr[$dataFieldKey] = $dataFieldValue;
            }
        }
        $data['change'] = $changeArr;
        $data['backupschange'] = $backupschangeArr;
        return $data;
    }

    //合并数据一条数据
    public function mergeChangeInfo($whereChange=0,$columns = []){
        if(empty($whereChange)) return [];
        $whereitemIdentificationresultchange['where'] =  $whereChange;
        if(!empty($columns)) $whereitemIdentificationresultchange['columns'] =  $columns;
        $itemIdentificationresultchangeInfo = $this->LocalService()->itemIdentificationresultchange->fetchOne($whereitemIdentificationresultchange);
        if(empty($itemIdentificationresultchangeInfo)) return [];
        $whereitemBackupschange['where'] = ['is_del'=>0,'patient_change_id'=>$itemIdentificationresultchangeInfo['id']];
        $whereitemBackupschange['columns'] = ['item_min_val','item_max_val','item_unit','item_result','user_name','remarks','item_min_range','item_max_range'];
        $itemBackupschangeInfo = $this->LocalService()->itemBackupschange->fetchAll($whereitemBackupschange);
        if (!empty($itemBackupschangeInfo)){
            $itemIdentificationresultchangeInfo = array_merge($itemIdentificationresultchangeInfo,$itemBackupschangeInfo);
        }
        return $itemIdentificationresultchangeInfo;
    }


    //合并数据多条数据
    public function mergeChangeData($whereChange=0,$columns = []){
        if(empty($whereChange)) return [];
        $whereitemIdentificationresultchange['where'] =  $whereChange;
        if(!empty($columns)) $whereitemIdentificationresultchange['columns'] =  $columns;
        $itemIdentificationresultchangeData = $this->LocalService()->itemIdentificationresultchange->fetchAll($whereitemIdentificationresultchange);
        if(empty($itemIdentificationresultchangeData)) return [];
        //备份表
        $chageIdArr = array_column($itemIdentificationresultchangeData,'id');
        $whereitemBackupschange['where'] = ['is_del'=>0,'patient_change_id'=>$chageIdArr];
        $whereitemBackupschange['columns'] = ['patient_change_id','item_min_val','item_max_val','item_unit','item_result','user_name','remarks','item_min_range','item_max_range'];
        $itemBackupschangeData = $this->LocalService()->itemBackupschange->fetchAll($whereitemBackupschange);
        if (!empty($itemBackupschangeData)){
            $itemBackupschangeDatas = [];
            foreach ($itemBackupschangeData as $itemBackupschangeDataKey=>$itemBackupschangeDataValue) {
                $patientChangeId = $itemBackupschangeDataValue['patient_change_id'];
                unset($itemBackupschangeDataValue['patient_change_id']);
                $itemBackupschangeDatas[$patientChangeId] = $itemBackupschangeDataValue;
            }
            foreach ($itemIdentificationresultchangeData as $itemIdentificationresultchangeDataK=>&$itemIdentificationresultchangeDataV){
                if (isset($itemBackupschangeDatas[$itemIdentificationresultchangeDataV['id']])){
                    $itemIdentificationresultchangeDataV = array_merge($itemIdentificationresultchangeDataV,$itemBackupschangeDatas[$itemIdentificationresultchangeDataV['id']]);
                }else{
                    $itemIdentificationresultchangeDataV = array_merge($itemIdentificationresultchangeDataV,$this->getBackupschangeField());
                }
            }
        }
        return $itemIdentificationresultchangeData;
    }
    public function getBackupschangeField(){
        return ['item_min_val'=>'','item_max_val'=>'','item_unit'=>'','item_result'=>'','user_name'=>'','remarks'=>'','item_min_range'=>'','item_max_range'=>''];
    }

}