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.StandItemEquipmentMapMapper;
import com.kcim.dao.model.StandItemEquipmentMap;
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:43
 **/
@Repository
public class StandItemEquipmentMapRepository extends ServiceImpl<StandItemEquipmentMapMapper, StandItemEquipmentMap> {
    /**
     * 获取设备与标准项目对照列表
     * @return  对照列表
     */
    public List<StandItemEquipmentMap> getList() {
        LambdaQueryWrapper<StandItemEquipmentMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StandItemEquipmentMap::getHospId, UserContext.getHospId());
        return this.list(queryWrapper);
    }

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

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

        return this.list(queryWrapper);
    }

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

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