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.util.UserContext;
import com.kcim.dao.mapper.EmpCostTypeMapper;
import com.kcim.dao.model.EmpCostMap;
import com.kcim.dao.model.EmpCostType;
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.Objects;
import java.util.stream.Collectors;

/**
 * @program: CostAccount
 * @description: 人事分类字典
 * @author: Wang.YS
 * @create: 2023-10-23 17:48
 **/
@Repository
public class EmpCostTypeRepository extends ServiceImpl<EmpCostTypeMapper, EmpCostType> {
    /**
     * 获取列表
     * @return 列表
     */
    public List<EmpCostType> getList() {
        LambdaQueryWrapper<EmpCostType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmpCostType::getHospId, UserContext.getHospId());
        return this.list(queryWrapper);

    }

    public List<EmpCostType> getList(String empTypeName) {
        LambdaQueryWrapper<EmpCostType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmpCostType::getHospId, UserContext.getHospId());
        if(!StringUtils.isEmpty(empTypeName)){
            queryWrapper.like(EmpCostType::getCostTypeName,empTypeName);
        }
        return this.list(queryWrapper);
    }

    public void deleteEmpCostType(Integer id) {
        EmpCostType costType = this.getById(id);
        if(Objects.nonNull(costType)){
            costType.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
            costType.setDeleteTime(new Date());
            this.updateById(costType);
            this.removeById(id);
        }
    }

    public String getCode() {
        LambdaQueryWrapper<EmpCostType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmpCostType::getHospId, UserContext.getHospId());
        List<EmpCostType> list = this.list(queryWrapper);
        if(CollectionUtils.isEmpty(list)){
            return  "T-1";
        }else {
            int size = list.size();
            int newId = size+ 1;
            return "T-"+newId;

        }
    }

    public List<EmpCostType> getList(SessionUserVO currentUser) {
        LambdaQueryWrapper<EmpCostType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmpCostType::getHospId, currentUser.getHospId());
        return this.list(queryWrapper);
    }

    public void removeList(List<EmpCostType> empCostType) {
        for (EmpCostType empCostMap : empCostType) {
            empCostMap.setDeleteTime(new Date());
            empCostMap.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
        }
        this.updateBatchById(empCostType);
        List<Integer> collect = empCostType.stream().map(EmpCostType::getId).collect(Collectors.toList());
        this.removeBatchByIds(collect);
    }
}
