package com.mosty.hczx.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.dto.hczx.TbHcSydjQueryDto;
import com.mosty.base.model.dto.hczx.TbHcSydjSaveDto;
import com.mosty.base.model.entity.bkzx.TbBkCl;
import com.mosty.base.model.entity.hczx.TbHcSydj;
import com.mosty.base.model.vo.hczx.TbHcSydjVo;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.hczx.mapper.TbHcSydjMapper;
import com.mosty.hczx.service.TbHcSydjServer;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.reflection.wrapper.BaseWrapper;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@AllArgsConstructor
public class TbHcSydjServiceImpl  extends ServiceImpl<TbHcSydjMapper, TbHcSydj> implements TbHcSydjServer {

    @Override
    public void sydjSaveOrEdit(TbHcSydjSaveDto dto) {
        UserInfo user = UserInfoManager.get();
        if (user == null) {
            throw new BusinessException("请重新登录");
        }
        if (StringUtils.isBlank(dto.getPcid())) {
            throw new BusinessException("盘查Id不存在");
        }
        TbHcSydj sydj = this.baseMapper.selectOne(new LambdaQueryWrapper<TbHcSydj>()
                .eq(TbHcSydj::getSfzh, dto.getSfzh())
                .eq(TbHcSydj::getPcid,dto.getPcid())
                .last(" limit 1"));
        if (null==sydj){
            TbHcSydj saveSydj = new TbHcSydj();
            BeanUtil.copyProperties(dto,saveSydj);
            saveSydj.setTjsj(new Date());
            saveSydj.setXtSjly("2");
            this.baseMapper.insert(saveSydj);
        }else {
            //人员姓名
            sydj.setRyxm(dto.getRyxm());
            //身份证号
            sydj.setSfzh(dto.getSfzh());
            //健康码状态
            sydj.setJkmzt(dto.getJkmzt());
            //疫苗接种情况
            sydj.setYmjzqk(dto.getYmjzqk());
            //通讯行程卡是否带*
            sydj.setTxxck(dto.getTxxck());
            //14天内到达或途径城市
            sydj.setDdhtjcs(dto.getDdhtjcs());
            //来自哪里
            sydj.setLz(dto.getLz());
            //去往哪里
            sydj.setQw(dto.getQw());
            //图片Id
            sydj.setTpid(dto.getTpid());
            sydj.setXtSjly("2");
            this.baseMapper.updateById(sydj);
        }
    }

    @Override
    public void editSydj(TbHcSydjSaveDto dto) {
        UserInfo user = UserInfoManager.get();
        if (user == null) {
            throw new BusinessException("请重新登录");
        }
        if (StringUtils.isBlank(dto.getId())){
            throw new BusinessException("需要修改的数据不存在");
        }
        if (StringUtils.isBlank(dto.getPcid())) {
            throw new BusinessException("盘查Id不存在");
        }

        TbHcSydj sydj =  this.baseMapper.selectById((dto.getId()));
        if (null==sydj){
            throw new BusinessException("需要修改的数据不存在");
        }
        //盘查id
        sydj.setPcid(dto.getPcid());
        //人员姓名
        sydj.setRyxm(dto.getRyxm());
        //身份证号
        sydj.setSfzh(dto.getSfzh());
        //健康码状态
        sydj.setJkmzt(dto.getJkmzt());
        //疫苗接种情况
        sydj.setYmjzqk(dto.getYmjzqk());
        //通讯行程卡是否带*
        sydj.setTxxck(dto.getTxxck());
        //14天内到达或途径城市
        sydj.setDdhtjcs(dto.getDdhtjcs());
        //来自哪里
        sydj.setLz(dto.getLz());
        //去往哪里
        sydj.setQw(dto.getQw());
        //图片Id
        sydj.setTpid(dto.getTpid());
        sydj.setTjsj(new Date());
        sydj.setXtSjly("2");
        this.baseMapper.updateById(sydj);
    }

    @Override
    public TbHcSydjVo getRySydjInfo(TbHcSydjQueryDto dto) {
        TbHcSydj sydj = this.baseMapper.selectOne(new LambdaQueryWrapper<TbHcSydj>()
                .eq(TbHcSydj::getSfzh, dto.getSfzh())
                .eq(TbHcSydj::getPcid,dto.getPcid())
                .last(" limit 1"));
        TbHcSydjVo sydjVo = new TbHcSydjVo();
        BeanUtil.copyProperties(sydj,sydjVo);
        return sydjVo;
    }
}
