package com.xinelu.manage.service.patientinfoimportmain.impl;

import com.xinelu.common.annotation.DataScope;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.domain.entity.SysUser;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.manage.domain.agency.Agency;
import com.xinelu.manage.domain.dialtime.DialTime;
import com.xinelu.manage.mapper.agency.AgencyMapper;
import com.xinelu.manage.mapper.patientinfoimportmain.PatientInfoImportMainMapper;
import com.xinelu.manage.service.patientinfoimportmain.IPatientInfoImportMainService;
import com.xinelu.manage.vo.patientinfo.PatientInfoVo;
import com.xinelu.manage.vo.patientinfoimportmain.PatientInfoImportMainVO;
import com.xinelu.system.mapper.SysUserMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service
public class PatientInfoImportMainServiceImpl implements IPatientInfoImportMainService {

    @Resource
    PatientInfoImportMainMapper patientInfoImportMainMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private AgencyMapper agencyMapper;


    @DataScope(agencyAlias = "p")
    @Override
    public List<PatientInfoImportMainVO> GetPatientInfoImport(PatientInfoImportMainVO patientInfoImport2VO)
    {
         List<PatientInfoImportMainVO>  list = new ArrayList<>();

        list = patientInfoImportMainMapper.GetPatientInfoImport(patientInfoImport2VO);

         return  list;

    }

    /**
     * 删除批次导入信息
     *
     * @param sn 流水号
     * @return AjaxResult
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult updateSn(String sn) {
        List<Long> list = patientInfoImportMainMapper.selectPatientInfoIds(sn);
        if (CollectionUtils.isNotEmpty(list)) {
            //修改患者表删除状态
            patientInfoImportMainMapper.updatePatientInfoByIds(list);
            //修改就诊表删除状态
            patientInfoImportMainMapper.updatePatientVisitRecordByPatientIds(list);
        }
        //删除患者导入信息表
        patientInfoImportMainMapper.deletePatientInfoImportBySn(sn);
        //删除患者导入记录表
        patientInfoImportMainMapper.deletePatientInfoImportMainBySn(sn);
        return AjaxResult.success();
    }

    /**
     * 删除单个导入信息
     *
     * @param patientInfoVo 单个信息
     * @return AjaxResult
     */
    @Override
    public AjaxResult updatePatientInfoImport(PatientInfoVo patientInfoVo) {
        Long patientId = patientInfoImportMainMapper.selectPatientInfoByMainVO(patientInfoVo);
        if (Objects.nonNull(patientId)) {
            //修改患者表删除状态
            patientInfoImportMainMapper.updatePatientInfoById(patientId);
            //修改就诊表删除状态
            patientInfoImportMainMapper.updatePatientVisitRecordByPatientId(patientId);
        }
        //删除患者导入信息表
        patientInfoImportMainMapper.deletePatientInfoImportByName(patientInfoVo);
        int i = patientInfoImportMainMapper.selectPatientInfoImportCount(patientInfoVo.getSn());
        if (i == 0) {
            patientInfoImportMainMapper.deletePatientInfoImportMainBySn(patientInfoVo.getSn());
        }
        return AjaxResult.success();
    }

    /**
     * 获取医院外呼时间起止详细信息
     *
     * @return AjaxResult
     */
    @Override
    public AjaxResult selectDialTimeById() {
        SysUser sysUser = sysUserMapper.selectUserById(SecurityUtils.getUserId());
        if (Objects.isNull(sysUser.getHospitalAgencyId())) {
            return AjaxResult.error("该账号未绑定医院，请绑定医院后查看！");
        }
        Agency agency = agencyMapper.selectAgencyById(sysUser.getHospitalAgencyId());
        if (Objects.isNull(agency)) {
            return AjaxResult.error("未查询该账号绑定的医院信息，请联系管理员！");
        }
        return AjaxResult.success(patientInfoImportMainMapper.selectDialTimeById(agency.getId()));
    }

    /**
     * 新增医院外呼时间起止
     *
     * @param dialTime 医院外呼时间起止
     * @return 结果
     */
    @Override
    public AjaxResult insertDialTime(DialTime dialTime) {
        SysUser sysUser = sysUserMapper.selectUserById(SecurityUtils.getUserId());
        if (Objects.isNull(sysUser.getHospitalAgencyId())) {
            return AjaxResult.error("该账号未绑定医院，请绑定医院后添加！");
        }
        Agency agency = agencyMapper.selectAgencyById(sysUser.getHospitalAgencyId());
        if (Objects.isNull(agency)) {
            return AjaxResult.error("未查询该账号绑定的医院信息，请联系管理员！");
        }
        DialTime dataDialTime = patientInfoImportMainMapper.selectDialTimeById(agency.getId());
        dialTime.setHospitalAgencyId(agency.getId());
        dialTime.setHospitalAgencyName(agency.getAgencyName());
        if (Objects.nonNull(dataDialTime)) {
            dialTime.setUpdateTime(LocalDateTime.now());
            dialTime.setUpdateBy(SecurityUtils.getUsername());
            return AjaxResult.success(patientInfoImportMainMapper.updateDialTime(dialTime));
        }
        dialTime.setCreateTime(LocalDateTime.now());
        dialTime.setCreateBy(SecurityUtils.getUsername());
        return AjaxResult.success(patientInfoImportMainMapper.insertDialTime(dialTime));
    }
}
