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

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

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

    /**
     * Notes: 添加新药物
     * User: llbjj
     * DateTime: 2023/6/9 13:54
     *
     * @param array $params
     */
    public function addMedicalData(array $params) {
        $item_id = isset($params['item_id']) && !empty($params['item_id']) ? $params['item_id'] : 0;
        $itemsig_id = isset($params['itemsig_id']) && !empty($params['itemsig_id']) ? $params['itemsig_id'] : 0;
        $patient_id = isset($params['patient_id']) && !empty($params['patient_id']) ? $params['patient_id'] : 0;
        $form_id = isset($params['form_id']) && !empty($params['form_id']) ? $params['form_id'] : 0;
        $checktime_id = isset($params['checktime_id']) && !empty($params['checktime_id']) ? $params['checktime_id'] : 0;
        $annex_id = isset($params['annex_id']) && !empty($params['annex_id']) ? $params['annex_id'] : 0;
        $table_id = isset($params['table_id']) && !empty($params['table_id']) ? $params['table_id'] : 0;
        $info_type = isset($params['info_type']) && !empty($params['info_type']) ? $params['info_type'] : 0;
        $save_id_arr = isset($params['save_id_arr']) && !empty($params['save_id_arr']) ? $params['save_id_arr'] : [];
        $lock_user = isset($params['lock_user']) && !empty($params['lock_user']) ? $params['lock_user'] : 0;
        $lock_time = time();

        if(!empty($annex_id)){
            //删除已删除的数据
            $delete_where = [
                'is_del' => 0,
                'annex_id' => $annex_id,
            ];
            if(!empty($save_id_arr)){
                $delete_where[] = new NotIn('info_Id', $save_id_arr);
            }
            $this->update(['is_del'=>1],$delete_where);
            unset($delete_where);

            //更新保存数据信息
            if(!empty($save_id_arr)){
                //获取已保存的数据
                $old_save_arr = $this->fetchCol('info_id',[
                    'is_del' => 0,
                    'annex_id' => $annex_id,
                ]);
                foreach($save_id_arr as $save_id){
                    if(!in_array($save_id,$old_save_arr)){
                        $saveData = [
                            'table_id'=>$table_id,
                            'info_type'=>$info_type,
                            'info_id'=>$save_id,
                            'item_id'=>$item_id,
                            'itemsig_id'=>$itemsig_id,
                            'patient_id'=>$patient_id,
                            'form_id'=>$form_id,
                            'checktime_id'=>$checktime_id,
                            'annex_id'=>$annex_id,
                            'is_lock'=>1,
                            'create_user_id'=>$lock_user,
                            'create_time'=>$lock_time
                        ];
                        $this->isSetInfo(false)->save($saveData);
                        unset($saveData);
                    }
                }
            }
        }

        //释放空间
        unset($item_id);
        unset($itemsig_id);
        unset($patient_id);
        unset($form_id);
        unset($checktime_id);
        unset($annex_id);
        unset($info_type);
        unset($save_id_arr);
        unset($lock_user);
        unset($lock_time);
    }

}