package com.ygtx.system.strategy;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ygtx.common.core.domain.entity.SysAutoCodePart;
import com.ygtx.common.core.domain.entity.SysDept;
import com.ygtx.common.utils.StringUtils;
import com.ygtx.system.service.ISysDeptService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(4)
public class PartTypeDefineHandler implements PartTypeTemplate {

    @Autowired
    private ISysDeptService sysDeptService;

    @Override
    public String partHandle(SysAutoCodePart sysAutoCodePart) {
        String val = sysAutoCodePart.getBizValue();
        String rs = "";
        switch (val) {
            case "1":   //原始数据
                rs = parserValue(sysAutoCodePart);
                break;
            case "2":   //分类码
                break;
            case "3":   //特征码
                break;
            case "4":   //字典值
                break;
            case "5":   //中转英首字母
                break;
            case "6":   //中转英全称
                break;
            case "7":   //场站编号
                rs = parseStationValue(sysAutoCodePart);
                break;
        }
        return rs;
    }

    private String parserValue(SysAutoCodePart sysAutoCodePart){
        String bizItem = sysAutoCodePart.getBizItem();
        JSONObject dataJson = parserData(sysAutoCodePart);
        String val = dataJson.get(bizItem).toString();
        int length = sysAutoCodePart.getPartLength();
        if(StringUtils.isNumeric(val) && length > 0){
            val = String.format("%0"+length+"d", Integer.parseInt(val));
        }
        return val;
    }

    private String parserWorkOrder(SysAutoCodePart sysAutoCodePart){
        String bizItem = sysAutoCodePart.getBizItem();
        String bizValue = sysAutoCodePart.getBizValue();
        //字典值
        if("4".equals(bizValue)){

        }
        return "";
    }

    private JSONObject parserData(SysAutoCodePart sysAutoCodePart){
        return JSONUtil.parseObj(sysAutoCodePart.getBizData());
    }

    private String parseStationValue(SysAutoCodePart sysAutoCodePart){
        String bizItem = sysAutoCodePart.getBizItem();
        JSONObject dataJson = parserData(sysAutoCodePart);
        String val = dataJson.get(bizItem).toString();
        if(StringUtils.isEmpty(val)){
            return "-";
        }
        SysDept dept = sysDeptService.selectDeptById(Long.parseLong(val));
        String depCode = dept.getDeptCode();
        if(StringUtils.isEmpty(depCode)){
            return "-";
        }
        return depCode;
    }
}
