package com.kcim.dao.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcim.common.util.UserContext;
import com.kcim.dao.mapper.ImportPatientInfoMapper;
import com.kcim.dao.model.ImportEmpCost;
import com.kcim.dao.model.ImportPatientInfo;
import com.kcim.vo.CommonDepartVo;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: CostAccount
 * @description: 患者信息导入数据库交互
 * @author: Wang.YS
 * @create: 2023-10-19 18:05
 **/
@Repository
public class ImportPatientInfoRepository extends ServiceImpl<ImportPatientInfoMapper, ImportPatientInfo> {
    public Page<ImportPatientInfo> getPage(Integer current, Integer pageSize, String computeDate, String departmentName, String patientName, String filter, String departmentCode) {

        Page<ImportPatientInfo> page = new Page<>(current,pageSize);
        LambdaQueryWrapper<ImportPatientInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImportPatientInfo::getHospId, UserContext.getHospId());
        queryWrapper.eq(ImportPatientInfo::getComputeDate, computeDate);
        if(!StringUtils.isEmpty(departmentName)){
            queryWrapper.like(ImportPatientInfo::getDepartmentName,departmentName);
        }
        if(!StringUtils.isEmpty(patientName)){
            queryWrapper.like(ImportPatientInfo::getName,patientName);
        }
        if(!StringUtils.isEmpty(filter)){
            queryWrapper.and(q->q.like(ImportPatientInfo::getName,filter).or().like(ImportPatientInfo::getPatientNo,filter));
        }
        if(!StringUtils.isEmpty(departmentCode)){
            queryWrapper.eq(ImportPatientInfo::getDepartmentCode, departmentCode);

        }
        return this.page(page,queryWrapper);

    }

    public List<ImportPatientInfo> getByComputeDate(String computeDate) {
        LambdaQueryWrapper<ImportPatientInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImportPatientInfo::getHospId, UserContext.getHospId());
        queryWrapper.eq(ImportPatientInfo::getComputeDate, computeDate);
        return this.list(queryWrapper);
    }

    public List<CommonDepartVo> getPatientInfoDepartment(Integer type, String filter, String computeDate) {
       return this.baseMapper.getPatientInfoDepartment(type,filter,computeDate,UserContext.getHospId());
    }

    public void removePatientInfoByFileId(String computeDate, Long fileId) {
        LambdaQueryWrapper<ImportPatientInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImportPatientInfo::getHospId, UserContext.getHospId());
        queryWrapper.eq(ImportPatientInfo::getComputeDate, computeDate);
        queryWrapper.eq(ImportPatientInfo::getFileId,fileId);
        List<ImportPatientInfo> list = this.list(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            for (ImportPatientInfo patientInfo : list) {
                patientInfo.setDeleteTime(new Date());
                patientInfo.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
            }
            this.updateBatchById(list);
            List<Integer> collect = list.stream().map(ImportPatientInfo::getId).collect(Collectors.toList());
            this.removeBatchByIds(collect);
        }
    }

    public void reducingPatientInfoByFileId(String computeDate, Long fileId) {
        this.baseMapper.reducingPatientInfoByFileId(computeDate,UserContext.getHospId(),fileId,new Date(),String.valueOf(UserContext.getCurrentUser().getId()));

    }
}
