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.StandItemEmpMapMapper;
import com.kcim.dao.model.StandItemEmpMap;
import com.kcim.vo.SessionUserVO;
import org.springframework.stereotype.Repository;

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-26 10:42
 **/
@Repository
public class StandItemEmpMapRepository extends ServiceImpl<StandItemEmpMapMapper, StandItemEmpMap> {
    /**
     * 获取人员与标准项目对照列表
     * @return  对照列表
     */
    public List<StandItemEmpMap> getList() {
        LambdaQueryWrapper<StandItemEmpMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StandItemEmpMap::getHospId, UserContext.getHospId());
        return this.list(queryWrapper);
    }

    public void removeByStandCodes(List<String> standCodes) {
        LambdaQueryWrapper<StandItemEmpMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StandItemEmpMap::getHospId, UserContext.getHospId());
        queryWrapper.in(StandItemEmpMap::getStandItemCode, standCodes);
        List<StandItemEmpMap> list = this.list(queryWrapper);
        for (StandItemEmpMap standItemEmpMap : list) {
            standItemEmpMap.setDeleteTime(new Date());
            standItemEmpMap.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
        }
        this.updateBatchById(list,100);
        List<Integer> collect = list.stream().map(StandItemEmpMap::getId).collect(Collectors.toList());
        this.removeBatchByIds(collect,100);
    }

    public List<StandItemEmpMap> getByStandCode(String standCode) {
        LambdaQueryWrapper<StandItemEmpMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StandItemEmpMap::getHospId, UserContext.getHospId());
        queryWrapper.eq(StandItemEmpMap::getStandItemCode, standCode);

        return this.list(queryWrapper);

    }

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

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