package com.fizz.business.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fizz.business.domain.ProMatmap;
import com.fizz.business.dto.MatmapDTO;
import com.fizz.business.mapper.ProMatmapMapper;
import com.fizz.business.service.client.RedisCacheManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * matmap数据操作封装
 *
 * @author chenhao
 * @date 2023/05/25
 */
@Slf4j
@Component
public class MatmapUtil {

    private static ProMatmapMapper romtbMatmapMapper;
    private static RedisCacheManager redisCacheManager;

    @Autowired
    public void setRomtbMatmapMapper(ProMatmapMapper romtbMatmapMapper) {
        MatmapUtil.romtbMatmapMapper = romtbMatmapMapper;
    }

    @Autowired
    public void setRedisCacheManage(RedisCacheManager redisCacheManager) {
        MatmapUtil.redisCacheManager = redisCacheManager;
    }

    @PostConstruct
    public static void init() {
        redisCacheManager.setMatmapList(getRomtbMatmap());
        log.info("#################load matmap from db#################");
    }

    private static List<MatmapDTO> getRomtbMatmap() {
        LambdaQueryWrapper<ProMatmap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(ProMatmap::getPosIdx);
        List<ProMatmap> list = romtbMatmapMapper.selectList(queryWrapper);
        return list.stream().map(m -> BeanUtil.toBean(m, MatmapDTO.class)).collect(Collectors.toList());
    }

    public static boolean notReady(MatmapDTO matmap) {
        return Objects.isNull(matmap) || StrUtil.isBlank(matmap.getMatId());
    }

    public static boolean already(MatmapDTO matmap) {
        return Objects.nonNull(matmap) && Objects.nonNull(matmap.getMatId());
    }

    public static void setMatId(int index, String matId) {
        MatmapDTO matmap = getMatmap(index);
        if (ObjectUtil.isNull(matmap)) {
            return;
        }
        matmap.setPosIdx(index);
        matmap.setMatId(matId);
        redisCacheManager.setMatmap(index, matmap);
    }

    public static String getMatId(int index) {
        MatmapDTO matmap = getMatmap(index);
        if (MatmapUtil.notReady(matmap)) return null;
        return matmap.getMatId();
    }

    public static void clearMatmap(int index) {
        MatmapDTO matmap = getMatmap(index);
        if (ObjectUtil.isNull(matmap)) {
            return;
        }
        matmap.setMatId(null);
        matmap.setPlanId(null);
        matmap.setPlanNo(null);
        redisCacheManager.setMatmap(index, matmap);
    }

    public static void setMatmap(int index, String matId, String planId, String planNo) {
        MatmapDTO matmap = getMatmap(index);
        if (ObjectUtil.isNull(matmap)) {
            return;
        }
        matmap.setPosIdx(index);
        matmap.setMatId(matId);
        matmap.setPlanId(planId);
        matmap.setPlanNo(planNo);
        redisCacheManager.setMatmap(index, matmap);
    }

    public static MatmapDTO getMatmap(int index) {
        MatmapDTO matmap = redisCacheManager.getMatmap(index);
        if (Objects.isNull(matmap)) {
            init();
        }
        return redisCacheManager.getMatmap(index);
    }

    public static List<MatmapDTO> getMatmapList() {
        List<MatmapDTO> matmapList = redisCacheManager.getMatmapList();
        if (CollUtil.isEmpty(matmapList)) {
            init();
        }
        return redisCacheManager.getMatmapList();
    }

    public static void clearMatmapByMatId(String matId) {
        List<MatmapDTO> matmapList = getMatmapList();
        for (MatmapDTO matmap : matmapList) {
            if (Objects.nonNull(matmap) && Objects.equals(matmap.getMatId(), matId)) {
                matmap.setMatId(null);
                matmap.setPlanId(null);
                matmap.setPlanNo(null);
            }
        }
        redisCacheManager.setMatmapList(matmapList);
    }
}
