package com.galaxis.manatee.entity.chuanyun.bo;

import com.dingtalk.api.response.OapiProcessinstanceGetResponse;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.galaxis.manatee.constant.ChuanYunConstant;
import com.galaxis.manatee.constant.StringConstant;
import com.galaxis.manatee.entity.chuanyun.data.object.BasicDO;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author zcj
 * @version 0.1
 * @date 2020/2/6 2:34 下午
 */
@Slf4j
@EqualsAndHashCode(callSuper = true)
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class MaterialResendBO extends BasicDO {

    /**
     * 表单编码
     */
    @JsonIgnore
    public static final String SCHEMA_CODE = "D001789materialResend";

    /**
     * 归属项目
     */
    private String project;
    /**
     * 请求日期
     */
    private LocalDate requestDate;
    /**
     * 请求类型:
     * 1.临时采购
     * 2.损坏补发
     * 3.售后服务
     * 4.研发缺料
     * 5.稳定性改善
     * 6.其他
     */
    private String requestType;
    /**
     * 请求原因
     */
    private String requestReason;
    /**
     * 交付日期
     */
    private LocalDate deadline;
    /**
     * 紧急程度
     */
    private String level;
    /**
     * 收货信息
     */
    private String receiverInformation;

    /**
     * 备注
     */
    private String content;
    /**
     * 物料明细列表
     */
    List<MaterialResendDetail> detailList;
    /**
     * 创建人
     */
    @JsonProperty(value = "CreatedByObjectObject")
    RelationObject createdByObjectObject;
    /**
     * 拥有者
     */
    @JsonProperty(value = "OwnerIdObjectObject")
    RelationObject ownerIdObjectObject;
    /**
     * 拥有部门
     */
    @JsonProperty(value = "OwnerDeptIdObjectObject")
    RelationObject ownerDeptIdObjectObject;

    /**
     * 申请日期编码
     */
    @Getter
    private enum FormComponent {
        /**
         * 申请日期
         */
        REQUEST_DATE("申请日期", "DDDateField-IV7F9M1Z"),
        /**
         * 申请类型
         */
        REQUEST_TYPE("申请类型", "DDSelectField-JUF43OUB"),
        REQUEST_REASON("申请原因", "TextField-IH4T9JQ9"),
        RESEARCH_PROJECT("研发项目号", "DDSelectField-JLLO6AZ2"),
        INTEGRATION_PROJECT("实施项目号", "DDSelectField-K4XUUCJA"),
        OLD_INTEGRATION_PROJECT_1("老项目号1","DDSelectField-IV7F9M20"),
        OLD_INTEGRATION_PROJECT_2("老项目号2","DDSelectField-JPHQ1P7F"),
        AFTER_SALE_PROJECT("售后项目号", "DDMultiSelectField-K3TG0TSA"),
        DEAD_LINE("交付日期", "DDDateField-IH4T9JQ8"),
        LEVEL("紧急程度", "DDSelectField-IV7F9M21"),
        RECEIVE_INFORMATION("收货信息", "TextareaField-IV7F9M22"),
        MATERIAL_DETAIL("物料明细", "TableField-IH4T9JQB"),
        CONTENT("备注", "TextareaField-IH4T9JQK");
        /**
         * 名称
         */
        private String name;
        /**
         * ID
         */
        private String id;

        FormComponent(String name, String id) {
            this.name = name;
            this.id = id;
        }

        /**
         * 根据Id获取对象
         *
         * @param id id
         * @return 枚举值
         * @throws Exception ID无法找到的异常
         */
        public static FormComponent fromId(String id) throws Exception {
            for (FormComponent formComponent : FormComponent.values()) {
                if (formComponent.getId().equals(id)) {
                    return formComponent;
                }
            }
            throw new Exception("can't find by id");
        }
    }

    /**
     * 根据钉钉中的数据获取
     *
     * @return 可以被氚云保存的对象
     */
    @SuppressWarnings("AlibabaSwitchStatement")
    public static MaterialResendBO fromDingTak(OapiProcessinstanceGetResponse.ProcessInstanceTopVo dingTalkProcessInstance) {
        MaterialResendBO materialResendBO = new MaterialResendBO();
        //由于物料补发功能在2020年一月份发生了变更，需要根据
        if(dingTalkProcessInstance.getFormComponentValues()==null){
            log.info("test");
        }
        dingTalkProcessInstance.getFormComponentValues().forEach(formComponentValueVo -> {
            FormComponent formComponent;
            try {
                String nullString="null";
                formComponent = FormComponent.fromId(formComponentValueVo.getId());
                switch (formComponent) {
                    case REQUEST_DATE -> materialResendBO.setRequestDate(LocalDate.parse(formComponentValueVo.getValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                    case DEAD_LINE -> materialResendBO.setDeadline(LocalDate.parse(formComponentValueVo.getValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                    case REQUEST_TYPE -> materialResendBO.setRequestType(formComponentValueVo.getValue());
                    case REQUEST_REASON -> materialResendBO.setRequestReason(formComponentValueVo.getValue());
                    case LEVEL -> materialResendBO.setLevel(formComponentValueVo.getValue());
                    case RECEIVE_INFORMATION -> materialResendBO.setReceiverInformation(formComponentValueVo.getValue());
                    case RESEARCH_PROJECT, INTEGRATION_PROJECT, AFTER_SALE_PROJECT,OLD_INTEGRATION_PROJECT_1,OLD_INTEGRATION_PROJECT_2 -> {
                        if (!formComponentValueVo.getValue().equals(nullString)) {
                            if(formComponentValueVo.getValue().contains(StringConstant.TAB_STRING)){
                                String projectId = formComponentValueVo.getValue().split(StringConstant.TAB_STRING)[0];
                                materialResendBO.setProject(ChuanYunConstant.PROJECT_CODE.get(projectId));
                            }else if(formComponentValueVo.getValue().contains(StringConstant.SPACE_STRING)){
                                String projectId = formComponentValueVo.getValue().split(StringConstant.SPACE_STRING)[0];
                                materialResendBO.setProject(ChuanYunConstant.PROJECT_CODE.get(projectId));
                            }
                        }
                    }
                    case CONTENT -> materialResendBO.setContent(formComponentValueVo.getValue());
                    case MATERIAL_DETAIL -> log.info(formComponentValueVo.getValue());
                    default -> log.warn(formComponentValueVo + "异常");
                }
            } catch (Exception e) {
                log.error(formComponentValueVo + "");
                log.error("根据formComponent内容解析异常");
            }
        });
        return materialResendBO;
    }

    /**
     * 物料明细对象
     */
    @Data
    static class MaterialResendDetail {
        /**
         * 主键
         */
        @JsonProperty(value = "ObjectId")
        private String objectId;
        /**
         * 物料名称
         */
        @JsonProperty(value = "Name")
        private String name = null;
        /**
         * 父ID，即物料补发明细Id
         */
        @JsonProperty(value = "ParentObjectId")
        private String parentObjectId;
        /**
         * 物料明细类型
         */
        private String detailType;
        /**
         * 物料明细名称
         */
        private String detailName;
        /**
         * 物料编码
         */
        private String code;
        /**
         * 物料数量
         */
        private Integer quantity;
        /**
         * 用途
         */
        private String usage;
    }
}