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.constants.NumberConstant;
import com.kcim.common.util.UserContext;
import com.kcim.dao.mapper.EquipmentMapper;
import com.kcim.dao.model.Equipment;
import com.kcim.vo.SessionUserVO;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: CostAccount
 * @description: 设备字典数据库交互
 * @author: Wang.YS
 * @create: 2023-10-26 15:21
 **/
@Repository
public class EquipmentRepository extends ServiceImpl<EquipmentMapper, Equipment> {
    /**
     * 获取列表
     * @return 列表
     */
    public List<Equipment> getList(){
        LambdaQueryWrapper<Equipment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Equipment::getHospId, UserContext.getHospId());
        return this.list(queryWrapper);
    }

    public Map<String, BigDecimal> getEquipmentMinuteMap() {
        List<Equipment> list = getList();
        if(!CollectionUtils.isEmpty(list)){
            return list.stream().collect(Collectors.toMap(Equipment::getCode, Equipment::getCostPerMinute, (a, b) -> b));
        }
        return new HashMap<>();
    }

    public Page<Equipment> getPage(Integer current, Integer pageSize, String name, Integer stop) {
        Page<Equipment> page = new Page<>(current,pageSize);
        LambdaQueryWrapper<Equipment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Equipment::getHospId, UserContext.getHospId());
        if(!stop.equals(NumberConstant.ONE)){
            queryWrapper.eq(Equipment::getStatus,stop);
        }
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like(Equipment::getName,name);
        }

        return this.page(page,queryWrapper);
    }

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

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

    public List<Equipment> getByCode(String code) {
        LambdaQueryWrapper<Equipment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Equipment::getHospId, UserContext.getHospId());
        queryWrapper.eq(Equipment::getCode, code);

        return this.list(queryWrapper);
    }

    public Equipment getOneByCode(String code) {
        LambdaQueryWrapper<Equipment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Equipment::getHospId, UserContext.getHospId());
        queryWrapper.eq(Equipment::getCode, code);

        return this.getOne(queryWrapper);
    }

    public Map<String, BigDecimal> getEquipmentMinuteMap(SessionUserVO currentUser) {
        List<Equipment> list = getList(currentUser);
        if(!CollectionUtils.isEmpty(list)){
            return list.stream().collect(Collectors.toMap(Equipment::getCode, Equipment::getCostPerMinute, (a, b) -> b));
        }
        return new HashMap<>();
    }

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