package com.kcim.dao.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcim.common.util.UserContext;
import com.kcim.dao.mapper.ItemSpaceMapMapper;
import com.kcim.dao.model.ItemEmpMap;
import com.kcim.dao.model.ItemEquipmentMap;
import com.kcim.dao.model.ItemSpaceMap;
import com.kcim.dao.model.StandItemSpaceMap;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @program: CostAccount
 * @description: 医疗服务项目空间成本对照数据库交互
 * @author: Wang.YS
 * @create: 2024-01-11 17:17
 **/
@Repository
public class ItemSpaceMapRepository extends ServiceImpl<ItemSpaceMapMapper, ItemSpaceMap> {
    public void removeByItemCode(List<String> collect) {
        LambdaUpdateWrapper<ItemSpaceMap> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ItemSpaceMap::getDelFlag,0)
                .eq(ItemSpaceMap::getHospId, UserContext.getHospId())
                .in(ItemSpaceMap::getItemCode,collect)
                .set(ItemSpaceMap::getDelFlag,1)
                .set(ItemSpaceMap::getDeleteUser,String.valueOf(UserContext.getCurrentUser().getId()))
                .set(ItemSpaceMap::getDeleteTime,new Date());
        this.update(updateWrapper);
    }

    public List<ItemSpaceMap> getList() {
        LambdaQueryWrapper<ItemSpaceMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ItemSpaceMap::getHospId, UserContext.getHospId());
        return this.list(queryWrapper);

    }

    public List<ItemSpaceMap> getList(String itemCode) {
        LambdaQueryWrapper<ItemSpaceMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ItemSpaceMap::getHospId, UserContext.getHospId());
        queryWrapper.eq(ItemSpaceMap::getItemCode, itemCode);

        return this.list(queryWrapper);
    }

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

    public List<ItemSpaceMap> getList(String itemCode, String departmentCode) {
        LambdaQueryWrapper<ItemSpaceMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ItemSpaceMap::getHospId, UserContext.getHospId());
        queryWrapper.eq(ItemSpaceMap::getItemCode, itemCode);
        queryWrapper.eq(ItemSpaceMap::getDepartmentCode, departmentCode);
        return this.list(queryWrapper);
    }
}
