package com.weaver.seconddev.interfaces.workflow.controller;

import com.alibaba.fastjson.JSONObject;
import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.seconddev.interfaces.workflow.action.ZOA_MM_MA_VALIDITY;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Author: calyrex
 * @CreateTime: 2025-02-17
 * @Description: ZOA_MM_MA_VALIDITY的API调用
 */

@Slf4j
@RestController
@RequestMapping({"/papi/secondev/workflow/controller"})
public class ZOA_MM_MA_VALIDITY_CONTROLLER {
    @GetMapping("/ZOA_MM_MA_VALIDITY")
    public WeaResult<String> run(@RequestParam Map<String,Object> params) {
        log.error("ZOA_MM_MA_VALIDITY_CONTROLLER execute start");
        log.error("ZOA_MM_MA_VALIDITY_CONTROLLER execute params : " + params);
        try {
            String index = full2HalfWidth(null2String(params.get("index")));
            String wlzbh = full2HalfWidth(null2String(params.get("wlzbh")));
            String wlzms = null2String(params.get("wlzms"));
            String wlbm = full2HalfWidth(null2String(params.get("wlbm")));
            String wlms = full2HalfWidth(null2String(params.get("wlms")));
            String t1 = full2HalfWidth(null2String(params.get("t1")));
            log.error("ZOA_MM_MA_VALIDITY_CONTROLLER t1--" + t1);
            String t2 = full2HalfWidth(null2String(params.get("t2")));
            String t3 = full2HalfWidth(null2String(params.get("t3")));
            String t4 = full2HalfWidth(null2String(params.get("t4")));
            String t5 = full2HalfWidth(null2String(params.get("t5")));
            String t6 = full2HalfWidth(null2String(params.get("t6")));
            String t7 = full2HalfWidth(null2String(params.get("t7")));
            String t8 = full2HalfWidth(null2String(params.get("t8")));
            String t9 = full2HalfWidth(null2String(params.get("t9")));
            String t10 = full2HalfWidth(null2String(params.get("t10")));
            String t11 = full2HalfWidth(null2String(params.get("t11")));
            String t12 = full2HalfWidth(null2String(params.get("t12")));
            String t13 = full2HalfWidth(null2String(params.get("t13")));
            String t14 = full2HalfWidth(null2String(params.get("t14")));
            String t15 = full2HalfWidth(null2String(params.get("t15")));
            String estatus = "";
            String emessage = "";
            String[][] prdetail = null;
            String aa = "@~&#";
            int i = wlzbh.split(aa).length;
            prdetail = new String[i + 1][22];
            int x = 0;
            for (int y = 0; y < i; y++) {
                x = x + 1;
                if (y < index.split(aa).length) {
                    prdetail[x][0] = index.split(aa)[y];
                } else {
                    prdetail[x][0] = "";
                }
                if (y < wlzbh.split(aa).length) {
                    prdetail[x][1] = wlzbh.split(aa)[y];
                } else {
                    prdetail[x][1] = "";
                }
                if (y < wlzms.split(aa).length) {
                    prdetail[x][2] = wlzms.split(aa)[y];
                } else {
                    prdetail[x][2] = "";
                }
                if (y < wlbm.split(aa).length) {
                    prdetail[x][3] = wlbm.split(aa)[y];
                } else {
                    prdetail[x][3] = "";
                }
                if (y < wlms.split(aa).length) {
                    prdetail[x][4] = wlms.split(aa)[y];
                } else {
                    prdetail[x][4] = "";
                }
                if (y < t1.split(aa).length) {
                    prdetail[x][5] = t1.split(aa)[y];
                } else {
                    prdetail[x][5] = "";
                }
                if (y < t2.split(aa).length) {
                    prdetail[x][6] = t2.split(aa)[y];
                } else {
                    prdetail[x][6] = "";
                }
                if (y < t3.split(aa).length) {
                    prdetail[x][7] = t3.split(aa)[y];
                } else {
                    prdetail[x][7] = "";
                }
                if (y < t4.split(aa).length) {
                    prdetail[x][8] = t4.split(aa)[y];
                } else {
                    prdetail[x][8] = "";
                }
                if (y < t5.split(aa).length) {
                    prdetail[x][9] = t5.split(aa)[y];
                } else {
                    prdetail[x][9] = "";
                }
                if (y < t6.split(aa).length) {
                    prdetail[x][10] = t6.split(aa)[y];
                } else {
                    prdetail[x][10] = "";
                }
                if (y < t7.split(aa).length) {
                    prdetail[x][11] = t7.split(aa)[y];
                } else {
                    prdetail[x][11] = "";
                }
                if (y < t8.split(aa).length) {
                    prdetail[x][12] = t8.split(aa)[y];
                } else {
                    prdetail[x][12] = "";
                }
                if (y < t9.split(aa).length) {
                    prdetail[x][13] = t9.split(aa)[y];
                } else {
                    prdetail[x][13] = "";
                }
                if (y < t10.split(aa).length) {
                    prdetail[x][14] = t10.split(aa)[y];
                } else {
                    prdetail[x][14] = "";
                }
                if (y < t11.split(aa).length) {
                    prdetail[x][15] = t11.split(aa)[y];
                } else {
                    prdetail[x][15] = "";
                }
                if (y < t12.split(aa).length) {
                    prdetail[x][16] = t12.split(aa)[y];
                } else {
                    prdetail[x][16] = "";
                }
                if (y < t13.split(aa).length) {
                    prdetail[x][17] = t13.split(aa)[y];
                } else {
                    prdetail[x][17] = "";
                }
                if (y < t14.split(aa).length) {
                    prdetail[x][18] = t14.split(aa)[y];
                } else {
                    prdetail[x][18] = "";
                }
                if (y < t15.split(aa).length) {
                    prdetail[x][19] = t15.split(aa)[y];
                } else {
                    prdetail[x][19] = "";
                }
                prdetail[x][20] = "";
                prdetail[x][21] = "";

            }
            ZOA_MM_MA_VALIDITY po = new ZOA_MM_MA_VALIDITY();
            String[][] fhz = po.Getexecute(prdetail);

            estatus = fhz[0][0];
            emessage = fhz[0][1];


            log.error("ZOA_MM_MA_VALIDITY_CONTROLLER i--"+ i);
            log.error("ZOA_MM_MA_VALIDITY_CONTROLLER x--"+ x);

            JSONObject result = new JSONObject();
            result.put("estatus", estatus);
            result.put("emessage", emessage);

            log.error("ZOA_MM_MA_VALIDITY_CONTROLLER estatus--"+estatus);
            log.error("ZOA_MM_MA_VALIDITY_CONTROLLER emessage--"+emessage);

            List<Map<String, Object>> item = new ArrayList<>();
            x = 0;
            for (int y = 0; y < i; y++) {
                Map<String, Object> itemMap = new HashMap<>();
                log.error("ZOA_MM_MA_DUPLICATE_CONTROLLER fhz[x][0]--"+ fhz[x][0]);
                log.error("ZOA_MM_MA_DUPLICATE_CONTROLLER fhz[x][3]--"+ fhz[x][3]);
                x = x + 1;
                itemMap.put("index", fhz[x][0]);
                itemMap.put("matnr", fhz[x][3]);
                itemMap.put("maktx", fhz[x][4].replaceAll("&", "and"));
                itemMap.put("status", fhz[x][20]);
                itemMap.put("message", fhz[x][21]);
                item.add(itemMap);
            }
            result.put("item", item);
            return WeaResult.success(JSONObject.toJSONString(result));
        }catch (Exception e) {
            log.error("ZOA_MM_MA_VALIDITY_CONTROLLER execute error", e);
            return WeaResult.fail(500, "ZOA_MM_MA_VALIDITY_CONTROLLER execute error " + e, e);
        }
    }

    public String full2HalfWidth(String str) {
        String outStr = "";
        if(str.contains("ＯＲ")){
            String Tstr = "";
            byte[] b = null;
            if (str != null) {
                for (int i = 0; i < str.trim().length(); i++) {
                    try {
                        Tstr = str.substring(i, i + 1);
                        b = Tstr.getBytes("unicode");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (b[2] == -1) {
                        b[3] = (byte) (b[3] + 32);
                        b[2] = 0;
                        try {
                            outStr = outStr + new String(b, "unicode");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        outStr = outStr + Tstr;
                    }
                }
            }
        }else{
            outStr = str;
        }
        return outStr;
    }

    public static String null2String(String s){
        return s == null ? "" : s;
    }

    public static String null2String(Object o){
        return o == null ? "" : o.toString();
    }

    public static String null2String(String s1,String s2){
        return s1 == null ? (s2 == null ? "" : s2) : s1;
    }

}
