package com.sw.mes.yb.seed.utils;

import com.sw.mes.core.api.system.YbFeignClients;
import com.sw.mes.core.dto.DictionaryItemDto;
import com.sw.mes.core.exception.BusinessException;
import com.sw.mes.core.response.Result;
import com.sw.mes.utils.StringUtils;
import com.sw.mes.yb.seed.entity.SeedData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Service
public class SeedBatchNoGenerator {
    @Autowired
    private YbFeignClients ybFeignClients;

    private static final String[] CHARACTER = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
            "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

    /**
     * 籽晶当天批次生成规则:
     * 1.定向/非定向   D：定向；F：非定向
     * 2.定向/非定向   瓦克定向:E;OCI/OCIM 定向:F;新定向:C永祥新能源和永祥多晶硅用于供欧定向:H内蒙新特用于供欧定向:1(不区分厂家)用于供欧定向:J
     * 3.类型    N型:N;P型:P
     * 4.年月日  23A13(入库日期)
     * 5.流水码  基地/厂家代码(四川:1;青海:2;新疆:3)山西:9 (目前写死9)
     * 6.车间   产出车间:用阿拉伯数字表示(1:5-8硅加工车间，硅加工一车间从数字5开始)
     * 7.流水码  籽晶长度:A:170;B:200;C:230(依次类推用26个大写字母表示)
     *
     * @param seed
     * @return
     * @throws BusinessException
     */
    public String generator(@NotNull SeedData seed) throws BusinessException {
        StringBuilder seedDayBatchNo = new StringBuilder();
        if (StringUtils.isEmpty(seed.getDirectionalSign())) {
            seedDayBatchNo.append("F");
            seedDayBatchNo.append("F");
        } else {
            seedDayBatchNo.append("D");
            Result<DictionaryItemDto> themeCodeAndItemName = ybFeignClients.findByThemeCodeAndItemName("creatCrystalNo", seed.getDirectionalSign());
            if (themeCodeAndItemName.getCode().equals(200) && null != themeCodeAndItemName.getData()) {
                seedDayBatchNo.append(themeCodeAndItemName.getData().getValue());
            } else {
                throw new BusinessException("未查询到相关定向数据【" + seed.getDirectionalSign() + "】,请检查【creatCrystalNo】字典数据!");
            }
        }
        if (null != seed.getMaterialModel()) {
            seedDayBatchNo.append(seed.getMaterialModel());
        } else {
            throw new BusinessException("未获取到物料【" + seed.getMaterialCode() + "】的类型,请检查数据!");
        }
        seedDayBatchNo.append(buildYear());
        seedDayBatchNo.append(buildMonth());
        seedDayBatchNo.append(buildDay());
        Result<DictionaryItemDto> workShopCode = ybFeignClients.findByThemeCodeAndItemName("createSeedBatchNoWorkShop", seed.getWorkShopCode());
        if (workShopCode.getCode().equals(200) && null != workShopCode.getData()) {
            String value = workShopCode.getData().getValue();
            String workShop = "山西" + value;
            Result<DictionaryItemDto> workShopItem = ybFeignClients.findByThemeCodeAndItemName("createSeedBatchNoWorkShop", workShop);
            if (workShopItem.getCode().equals(200) && null != workShopItem.getData()) {
                seedDayBatchNo.append(workShopItem.getData().getValue());
            }
        } else {
            throw new BusinessException("未获取到车间【" + seed.getWorkShopCode() + "】的数据,请检查【createSeedBatchNoWorkShop】字典数据!");
        }
        Result<DictionaryItemDto> processLength = ybFeignClients.findByThemeCodeAndItemName("createSeedBatchNoLength", String.valueOf(seed.getProcessLength()));
        if(processLength.getCode().equals(200) && null != processLength.getData()){
            seedDayBatchNo.append(processLength.getData().getValue());
        }else {
            throw new BusinessException("未获取到籽晶长度【" + seed.getProcessLength() + "】的数据,请检查【createSeedBatchNoLength】字典数据!");
        }
        return seedDayBatchNo.toString();
    }

    /**
     * 构建年
     * @return
     */
    public String buildYear() {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
            Date date = new Date();
            String years = simpleDateFormat.format(date);
            String year = years.substring(years.length() - 2, years.length());
            return year;
        } catch (Exception ex) {
            throw new BusinessException("获取年时间失败,请重新操作!");
        }
    }

    /**
     * 构建月
     * 规则：
     * 1、1-9月返回数字
     * 2、10月A 11月B 12月C
     *
     * @return
     */
    public String buildMonth() {
        try {
            Calendar calendar = Calendar.getInstance();
            int month = calendar.get(Calendar.MONTH) + 1;//jdk里面取到的值会-1，所以外面需要加1
            if (month <= 9) {
                return String.valueOf(month);
            }
            if (month == 10) {
                return "A";
            } else if (month == 11) {
                return "B";
            } else if (month == 12) {
                return "C";
            }
            return "";
        } catch (Exception ex) {
            throw new BusinessException("获取月时间失败,请重新操作!");
        }
    }

    /**
     * 构建天 两位
     * @return
     */
    public String buildDay() {
        try {
            Calendar calendar = Calendar.getInstance();
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            if (day <= 9) {
                return "0" + day;
            }
            return String.valueOf(day);
        } catch (Exception ex) {
            throw new BusinessException("获取天时间失败,请重新操作!");
        }
    }
}
