package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.base;


import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesEnumExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.util.EnumUtil;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.MesEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesEnum;
import cn.estsh.i3plus.pojo.mes.bean.MesEnumDetail;
import cn.estsh.i3plus.pojo.mes.dbinterface.MesInterfaceEnumUtil;
import cn.estsh.i3plus.pojo.mes.repository.MesEnumDetailRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesEnumRepository;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Wynne.Lu
 * @CreateDate: 2019/8/30 1:39 PM
 * @Description:
 **/
@Slf4j
@Service
@ApiOperation(value = "获取MesEnumUtil")
public class MesEnumExtService implements IMesEnumExtService {

    @Autowired
    private MesEnumRepository enumRepository;

    @Autowired
    private MesEnumDetailRepository enumDetailRepository;

    @Override
    public Map<String, Object> doGetMesEnumByEnumName(String organizeCode, String enumName) {
        Map<String, Object> enumMap = getEnumMap(organizeCode, enumName);
        if (CollectionUtils.isEmpty(enumMap)) enumMap = getEnumByClazzEnum(enumName, MesExtEnumUtil.class);
        if (CollectionUtils.isEmpty(enumMap)) enumMap = getEnumByClazzEnum(enumName, MesEnumUtil.class);
        if (CollectionUtils.isEmpty(enumMap)) enumMap = getEnumByClazzEnum(enumName, MesInterfaceEnumUtil.class);
        return enumMap;
    }

    @Override
    public Map<String, Object> getEnumByClazzEnum(String enumName, Class clz) {
        return EnumUtil.getEnumByName(clz, enumName);
    }

    private Map<String, Object> getEnumMap(String organizeCode, String enumName) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(enumName, MesPcnExtConstWords.ENUM_CODE, packBean);
        MesEnum enumDb = enumRepository.getByProperty(packBean);
        if (null == enumDb) return null;
        List<MesEnumDetail> enumDetailList = enumDetailRepository.findByHqlWhere(packBean);
        if (CollectionUtils.isEmpty(enumDetailList)) return null;
        return getEnumMap(enumDetailList, enumName);
    }

    private Map<String, Object> getEnumMap(List<MesEnumDetail> enumDetailList, String enumName) {
        Map<String, Object> enumMap = new HashMap<>();
        List<Map<String, Object>> values = new ArrayList<>();
        enumDetailList.forEach(e -> {
            Map<String, Object> value = new HashMap<>();
            value.put("value", e.getDetailValue());
            if (!StringUtils.isEmpty(e.getEnumCode())) {
                value.put("code", e.getDetailCode());
            }
            value.put("description", e.getDetailName());
            values.add(value);
        });
        enumMap.put("enumName", enumName);
        enumMap.put("valuesList", values);
        return enumMap;
    }




}
