package com.kcim.dao.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.EmpCostMapMapper;
import com.kcim.dao.model.Drug;
import com.kcim.dao.model.EmpCostMap;
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-24 15:37
 **/
@Repository
public class EmpCostMapRepository extends ServiceImpl<EmpCostMapMapper, EmpCostMap> {
    /**
     * 获取所有对照列表
     * @return 对照列表
     */
    public List<EmpCostMap> getList() {
        LambdaQueryWrapper<EmpCostMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmpCostMap::getHospId, UserContext.getHospId());
        return this.list(queryWrapper);
    }
    public List<EmpCostMap> getList(String costType) {
        LambdaQueryWrapper<EmpCostMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmpCostMap::getHospId, UserContext.getHospId());
        queryWrapper.eq(EmpCostMap::getCostTypeCode, costType);

        return this.list(queryWrapper);
    }
    public Page<EmpCostMap> getPage(Integer current, Integer pageSize, String costTypeCode, String filter) {
        Page<EmpCostMap> page = new Page<>(current,pageSize);
        LambdaQueryWrapper<EmpCostMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmpCostMap::getHospId, UserContext.getHospId());
        queryWrapper.eq(EmpCostMap::getCostTypeCode,costTypeCode);
        if(!StringUtils.isEmpty(filter)){
            queryWrapper.and(q->q.like(EmpCostMap::getAccount,filter).or().like(EmpCostMap::getName,filter));
        }
        return this.page(page,queryWrapper);
    }

    public void deleteEmpCostMap(List<Integer> id) {
        LambdaQueryWrapper<EmpCostMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmpCostMap::getHospId, UserContext.getHospId());
        queryWrapper.in(EmpCostMap::getId,id);
        List<EmpCostMap> list = this.list(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            for (EmpCostMap costMap : list) {
                costMap.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
                costMap.setDeleteTime(new Date());
            }
            this.updateBatchById(list);
            List<Integer> collect = list.stream().map(EmpCostMap::getId).collect(Collectors.toList());
            this.removeBatchByIds(collect);

        }


    }

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

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