package com.kcim.dao.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcim.common.constants.NumberConstant;
import com.kcim.common.util.UserContext;
import com.kcim.dao.mapper.ComputeEmpCostMapper;
import com.kcim.dao.model.ComputeEmpCost;
import com.kcim.vo.SessionUserVO;
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-23 16:15
 **/
@Repository
public class ComputeEmpCostRepository extends ServiceImpl<ComputeEmpCostMapper, ComputeEmpCost> {
    /**
     * 获取当前核算年月计算数据
     * @param computeDate 核算年月
     * @return 列表数据
     */
    public List<ComputeEmpCost> getByComputeDate(String computeDate) {
        LambdaQueryWrapper<ComputeEmpCost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ComputeEmpCost::getHospId, UserContext.getHospId());
        queryWrapper.eq(ComputeEmpCost::getComputeDate,computeDate);
        return this.list(queryWrapper);
    }

    public void removeByComputeDate(String computeDate) {
        List<ComputeEmpCost> byComputeDate = getByComputeDate(computeDate);
        if(!CollectionUtils.isEmpty(byComputeDate)){
            for (ComputeEmpCost computeEmpCost : byComputeDate) {
                computeEmpCost.setDeleteTime(new Date());
                computeEmpCost.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
            }
            this.updateBatchById(byComputeDate);
            List<Integer> collect = byComputeDate.stream().map(ComputeEmpCost::getId).collect(Collectors.toList());
            this.removeBatchByIds(collect);

        }
    }

    public List<ComputeEmpCost> getByComputeDate(String computeDate, String filter) {
        LambdaQueryWrapper<ComputeEmpCost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ComputeEmpCost::getHospId, UserContext.getHospId());
        queryWrapper.eq(ComputeEmpCost::getComputeDate,computeDate);
        if(!StringUtils.isEmpty(filter)){
            queryWrapper.like(ComputeEmpCost::getName,filter);
        }
        return this.list(queryWrapper);
    }

    public List<ComputeEmpCost> getByParentComputeDate(String computeDate) {
        LambdaQueryWrapper<ComputeEmpCost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ComputeEmpCost::getHospId, UserContext.getHospId());
        queryWrapper.eq(ComputeEmpCost::getComputeDate,computeDate);
        queryWrapper.eq(ComputeEmpCost::getParentCode, NumberConstant.ZERO_S);

        return this.list(queryWrapper);
    }

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