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.ItemEquipmentMapMapper;
import com.kcim.dao.model.*;
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:14
 **/
@Repository
public class ItemEquipmentMapRepository extends ServiceImpl<ItemEquipmentMapMapper, ItemEquipmentMap> {
    /**
     * 按医疗服务项目代码删除
     * @param collect 医疗服务项目代码
     */
    public void removeByItemCode(List<String> collect) {
        LambdaUpdateWrapper<ItemEquipmentMap> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ItemEquipmentMap::getDelFlag,0)
                .eq(ItemEquipmentMap::getHospId, UserContext.getHospId())
                .in(ItemEquipmentMap::getItemCode,collect)
                .set(ItemEquipmentMap::getDelFlag,1)
                .set(ItemEquipmentMap::getDeleteUser,String.valueOf(UserContext.getCurrentUser().getId()))
                .set(ItemEquipmentMap::getDeleteTime,new Date());
        this.update(updateWrapper);
    }

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

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

        return this.list(queryWrapper);
    }

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

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