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

use Interop\Container\ContainerInterface;

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

    /**
     *
     *
     */
    public function changeUpdateLogs($data= [],$type = 0){
        if (empty($data)) return false;
        $changeIdData = [];
        $fieldArr = ['id','unit','is_normal_type','result','reference_range_max','reference_range_min','min_symbol','max_symbol','collect_date'];
        if (in_array($type,[0,1,2])){
            $tableName = 'itemIdentificationresultchange';
            $changeIdData = array_filter(array_unique(array_column($data,'id')));
            $changeWhere = [
                'where'=>[
//                    'lock_state'=>1,
                    'is_del'=>0,
                    'id'=>$changeIdData,
                ]
            ];
        }elseif($type == 3){//ae
            $tableName = 'itemIdentificationresultchange';
            $changeIdData = array_filter(array_unique(array_column($data,'change_id')));
            $changeWhere = [
                'where'=>[
//                    'lock_state'=>1,
                    'is_del'=>0,
                    'id'=>$changeIdData,
                ]
            ];

        }
        if (empty($changeIdData)) return false;
        $resultchangeData = $this->LocalService()->$tableName->fetchAll($changeWhere);
        if(empty($resultchangeData)) return false;
        foreach ($resultchangeData as $resultchangeDataKey=>$resultchangeDataValue ){
            foreach ($resultchangeDataValue as $fieldK=>$value ){
                foreach ($data as $newDataK=>$newDataV){
                    if (($newDataV['id'] != $resultchangeDataValue['id']) || (!in_array($fieldK,$fieldArr))){
                        continue;
                    }
                    if (isset($newDataV[$fieldK]) && ($value != $newDataV[$fieldK])){
                        $arr = [
                            'change_id'=>$newDataV['id'],
                            'patient_form_id'=>$newDataV['patient_form_id'],
                            'category_id'=>$newDataV['category_id'],
                            'form_id'=>$newDataV['form_id'],
                            'researchstage_id'=>$newDataV['researchstage_id'],
                            'checktime_id'=>$newDataV['checktime_id'],
                            'patient_id'=>$newDataV['patient_id'],
                            'itemsig_id'=>$newDataV['itemsig_id'],
                            'field'=>$fieldK,

                        ];
                        $resultLog = $this->LocalService()->changeUpdateLog->save($arr);
                    }
                }
            }
        }
        return true;
    }


}