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.constants.NumberConstant;
import com.kcim.common.util.UserContext;
import com.kcim.dao.mapper.SpaceMapper;
import com.kcim.dao.model.Space;
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:32
 **/
@Repository
public class SpaceRepository extends ServiceImpl<SpaceMapper, Space> {

    /**
     * 获取列表
     * @return 列表
     */
    public List<Space> getList(){
        LambdaQueryWrapper<Space> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Space::getHospId, UserContext.getHospId());
        return this.list(queryWrapper);
    }

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

    private List<Space> getChildList(SessionUserVO currentUser) {
        LambdaQueryWrapper<Space> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Space::getHospId, currentUser.getHospId());
        queryWrapper.eq(Space::getType, NumberConstant.ONE);

        return this.list(queryWrapper);
    }

    public List<Space> getChildList(){
        LambdaQueryWrapper<Space> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Space::getHospId, UserContext.getHospId());
        queryWrapper.eq(Space::getType, NumberConstant.ONE);

        return this.list(queryWrapper);
    }

    public List<Space> getList(String name, Integer type) {
        LambdaQueryWrapper<Space> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Space::getHospId, UserContext.getHospId());
        if(!type.equals(NumberConstant.ONE)){
            queryWrapper.eq(Space::getStatus,type);
        }
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like(Space::getName,name);
        }
        return this.list(queryWrapper);
    }

    public List<Space> getParentList() {
        LambdaQueryWrapper<Space> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Space::getHospId, UserContext.getCurrentLoginHospId());
        queryWrapper.eq(Space::getParentCode, NumberConstant.ZERO_S);
        return this.list(queryWrapper);
    }

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

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

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

        return this.list(queryWrapper);
    }

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

        return this.getOne(queryWrapper);
    }
}
