package com.cool.store.dto.log;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;
import com.cool.store.entity.HyPartnerTaskInfoLogDO;
import com.cool.store.enums.OperateLogFieldValueEnum;
import com.cool.store.enums.OperateTypeEnum;
import com.cool.store.enums.WorkflowStageEnum;
import com.cool.store.enums.WorkflowStatusEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

/**
 * @author zhangchenbiao
 * @FileName: LineLogInfo
 * @Description:
 * @date 2023-06-27 13:44
 */
@Slf4j
@Data
public class LineLogInfo<T> {

    @ApiModelProperty("hy_partner_user_info.partner_id")
    private String partnerId;

    @ApiModelProperty("hy_partner_line_info.id")
    private Long partnerLineId;


    @ApiModelProperty("操作人id")
    private String operateUserId;

    @ApiModelProperty("操作人姓名")
    private String operateUsername;

    @ApiModelProperty("操作类型")
    private OperateTypeEnum operateType;

    @ApiModelProperty("流程阶段：意向申请审核、预约面试时间、加盟资格面试、分配选址开发经理、商圈点位评估、上传店铺租赁信息、完善加盟签约信息、支付加盟费用、签订加盟合同")
    private WorkflowStageEnum workflowStage;

    @ApiModelProperty("流程子状态")
    private String workflowStatus;

    @ApiModelProperty("备注")
    private String message;

    @ApiModelProperty("数据处理字段")
    private T data;

    public LineLogInfo(){

    }
    public LineLogInfo(String partnerId, Long partnerLineId, String operateUserId, String operateUsername,
                       OperateTypeEnum operateType, WorkflowStageEnum workflowStage,
                       String workflowStatus, String message) {
        this.partnerId = partnerId;
        this.partnerLineId = partnerLineId;
        this.operateUserId = operateUserId;
        this.operateUsername = operateUsername;
        this.operateType = operateType;
        this.workflowStage = workflowStage;
        this.workflowStatus = workflowStatus;
        this.message = message;
    }

    public static boolean checkParams(LineLogInfo params){
        //部分操作没有操作人，这里不再校验操作人
        if(StringUtils.isAnyBlank(params.getPartnerId())){
            log.info("lineLogInfo string:{}", JSONObject.toJSONString(params));
            return false;
        }
        if(Objects.isNull(params.getOperateType()) || Objects.isNull(params.getWorkflowStage()) || Objects.isNull(params.getWorkflowStatus())){
            log.info("lineLogInfo enum is null:{}", JSONObject.toJSONString(params));
            return false;
        }
        return true;
    }

    public static HyPartnerTaskInfoLogDO convertDO(LineLogInfo params){
        OperateTypeEnum operateType = params.getOperateType();
        HyPartnerTaskInfoLogDO result = new HyPartnerTaskInfoLogDO();
        result.setPartnerId(params.getPartnerId());
        result.setPartnerLineId(params.getPartnerLineId());
        result.setOperateType(params.getOperateType().getCode());
        result.setWorkflowStage(params.getWorkflowStage().getCode());
        result.setWorkflowStatus(params.getWorkflowStatus());
        result.setOperateUserId(params.getOperateUserId());
        result.setOperateUsername(params.getOperateUsername());
        result.setMessage(params.getMessage());
        result.setCreateTime(new Date());
        result.setUpdateTime(new Date());
        Object data = params.getData();
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(data));
        List<OperateLogFieldValueEnum> filedList = operateType.getFiledList();
        List<Map<String, Object>> fieldMapList = new ArrayList<>();
        for (OperateLogFieldValueEnum field : filedList) {
            Map<String, Object> fieldMap = new HashMap<>();
            if(jsonObject.get(field.getCode())==null){
                continue;
            }
            fieldMap.put("code",field.getCode());
            fieldMap.put("name",field.getName());
            fieldMap.put("value",jsonObject.get(field.getCode()));
            fieldMapList.add(fieldMap);
        }
        result.setFieldCopy(JSONObject.toJSONString(fieldMapList));
        return result;
    }

}
