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

import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.api.iservice.busi.IGenSerialNoStrategy;
import cn.estsh.i3plus.mes.pcn.serviceimpl.busi.GenSerialNoService;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.mes.bean.MesNumberRule;
import cn.estsh.i3plus.pojo.mes.model.GenSerialNoModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * @Author: wangjie
 * @CreateDate: 2019/8/23 1:16 PM
 * @Description: 松下日本向座便器条码流水号
 * 1位：代表年，取年份最后一位数。
 * 2位：代表月份，10月使用大写字母O，11月使用大写字母N，12月使用大写字母D。
 * 3-4位：2位，代表日期01-31日。
 * 5位：物料识别码，和物料相关，和配置在物料信息中。
 * 6-8位：3位，流水码。
 **/

@Service
public class SxJapanGenSerialNoStrategyService implements IGenSerialNoStrategy {

    @Autowired
    private GenSerialNoService genSerialNoService;

    private static final String MES_NUMBER_RULE = "mesNumberRule";
    private static final String ORG = "org";
    private static final String USER_NAME = "userName";
    private static final String GEN_SERIAL_NO_MODEL = "genSerialNoModel";
    private static final String O = "O";
    private static final String N = "N";
    private static final String D = "D";
    private static final String TEN = "10";
    private static final String ELEVEN = "11";
    private static final String TWELVE = "12";
    private static final String ZERO = "0";

    @Override
    public String exec(Map<String, Object> map) {
        MesNumberRule mesNumberRule = (MesNumberRule) map.get(MES_NUMBER_RULE);
        String org = (String) map.get(ORG);
        String userName = (String) map.get(USER_NAME);
        GenSerialNoModel genSerialNoModel = (GenSerialNoModel) map.get(GEN_SERIAL_NO_MODEL);

        String currentYear = TimeTool.getYear();
        if (!currentYear.substring(3,4).equals(genSerialNoModel.getYear())) {
            currentYear = TimeTool.timeCalc(TimeTool.stringParseToDate(currentYear, MesPcnExtConstWords.YEAR_FORMAT), Calendar.YEAR, 1, MesPcnExtConstWords.YEAR_FORMAT);
        }
        String currentDate = currentYear + getMouth(genSerialNoModel.getMonth(), false) + genSerialNoModel.getDay();
        String nextDate = TimeTool.timeCalc(TimeTool.stringParseToDate(currentDate), Calendar.DAY_OF_MONTH, 1, MesPcnExtConstWords.DATE_SERIAL_FORMAT);

        genSerialNoModel.setYear(nextDate.substring(3,4));
        genSerialNoModel.setMonth(getMouth(nextDate.substring(4,6), true));
        genSerialNoModel.setDay(nextDate.substring(6));

        List<String> arr = genSerialNoService.splitRuleCode(mesNumberRule.getNumberRule());
        return genSerialNoService.assembleStrWizoutSerialNo(arr, genSerialNoModel, mesNumberRule, org, userName);
    }

    public String getMouth(String mouth, Boolean flag) {
        if (flag) {
            if (mouth.equals(TEN)) {
                return O;
            } else if (mouth.equals(ELEVEN)) {
                return N;
            } else if (mouth.equals(TWELVE)) {
                return D;
            } else {
                return Integer.valueOf(mouth).toString();
            }
        } else {
            if (mouth.equals(O)) {
                return TEN;
            } else if (mouth.equals(N)) {
                return ELEVEN;
            } else if (mouth.equals(D)) {
                return TWELVE;
            } else {
                return ZERO + mouth;
            }
        }
    }

}
