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.ImportEmpCostMapper;
import com.kcim.dao.model.ComputeClinicalPathwayCostDetail;
import com.kcim.dao.model.ImportEmpCost;
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-18 19:27
 **/
@Repository
public class ImportEmpCostRepository extends ServiceImpl<ImportEmpCostMapper, ImportEmpCost> {
    /**
     * 按分页获取人事成本导入数据
     * @param current 当前页
     * @param pageSize 页容量
     * @param computeDate 核算年月
     * @param filter 过滤条件
     * @return 导入列表
     */
    public Page<ImportEmpCost> getPage(Integer current, Integer pageSize, String computeDate, String filter) {
        Page<ImportEmpCost> page = new Page<>(current,pageSize);
        LambdaQueryWrapper<ImportEmpCost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImportEmpCost::getHospId, UserContext.getHospId());
        queryWrapper.eq(ImportEmpCost::getComputeDate, computeDate);
        if(!StringUtils.isEmpty(filter)){
            queryWrapper.like(ImportEmpCost::getEmpCode,filter).or().like(ImportEmpCost::getEmpName,filter);
        }
        return this.page(page,queryWrapper);

    }

    /**
     * 通过核算单元获取导入数据
     * @param computeDate 核算年月
     * @return 导入数据
     */
    public List<ImportEmpCost> getByComputeDate(String computeDate) {
        LambdaQueryWrapper<ImportEmpCost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImportEmpCost::getHospId, UserContext.getHospId());
        queryWrapper.eq(ImportEmpCost::getComputeDate, computeDate);
        return this.list(queryWrapper);
    }

    /**
     * 通过核算年月与工号列表获取导入数据
     * @param computeDate 核算年月
     * @param empCodes 工号列表
     * @return 导入数据
     */
    public List<ImportEmpCost> getByComputeDate(String computeDate,List<String> empCodes) {
        LambdaQueryWrapper<ImportEmpCost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImportEmpCost::getHospId, UserContext.getHospId());
        queryWrapper.eq(ImportEmpCost::getComputeDate, computeDate);
        queryWrapper.in(ImportEmpCost::getEmpCode, empCodes);

        return this.list(queryWrapper);
    }

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

    public void reducingEmpCostByFileId(String computeDate, Long fileId) {

        this.baseMapper.reducingEmpCostByFileId(computeDate,UserContext.getHospId(),fileId,new Date(),String.valueOf(UserContext.getCurrentUser().getId()));
    }
}
