package com.gic.haoban.manage.service.service.out.impl.content;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.business.order.dto.ordermanage.OrderOutInfoDTO;
import com.gic.business.order.enums.OrderEnum;
import com.gic.business.order.qdto.ordermanage.OrderInfoQDTO;
import com.gic.business.order.service.ordermanage.OrderInfoOutApiService;
import com.gic.haoban.manage.api.enums.content.MaterialInteractRecordEventType;
import com.gic.haoban.manage.api.service.content.MallOrderStatusChangeApiService;
import com.gic.haoban.manage.service.pojo.bo.content.OrderEventMqBO;
import com.gic.haoban.manage.service.pojo.bo.content.message.InteractRecordMessageBO;
import com.gic.haoban.manage.service.service.content.message.InteractRecordMessageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author MUSI
 * @Date 2023/4/6 5:28 PM
 * @Description
 * @Version
 **/
@Slf4j
@Component("mallOrderStatusChangeApiService")
public class MallOrderStatusChangeApiServiceImpl implements MallOrderStatusChangeApiService {


    @Autowired
    OrderInfoOutApiService orderInfoOutApiService;
    @Autowired
    InteractRecordMessageService interactRecordMessageService;

    /***
     * 订单状态变更事件
     * @param params
     * @return
     */
    @Override
    public ServiceResponse<Void> mallOrderStatusChange(String params) {

        OrderEventMqBO orderEventMqBO = JSON.parseObject(params, OrderEventMqBO.class);
        if (orderEventMqBO == null) {
            log.info("订单状态变更事件参数异常 {}", params);
            return ServiceResponse.success();
        }

        Integer toStatus = orderEventMqBO.getToStatus();
        if (OrderEnum.OrderNodeEnum.PAY_ORDER.getOrderStep().equals(toStatus)) {
            log.info("订单事件不是支付事件 {}", toStatus);
            return ServiceResponse.success();
        }

        OrderInfoQDTO orderInfoQDTO = new OrderInfoQDTO();
        orderInfoQDTO.setEnterpriseId(orderEventMqBO.getEnterpriseId());
        orderInfoQDTO.setOrderNumber(orderEventMqBO.getOrderNumber());
        ServiceResponse<OrderOutInfoDTO> serviceResponse = orderInfoOutApiService.queryOrderInfo(orderInfoQDTO);
        if (!serviceResponse.isSuccess()) {
            log.info("查询订单信息异常 {}", JSON.toJSONString(serviceResponse));
            return ServiceResponse.success();
        }
        OrderOutInfoDTO orderInfo = serviceResponse.getResult();
        String extendJson = orderInfo.getExtendJson();
        if (StringUtils.isBlank(extendJson)) {
            log.info("订单扩展字段为空,不处理 params: {}", params);
            return ServiceResponse.success();
        }

        JSONObject jsonObject = JSON.parseObject(extendJson, JSONObject.class);
        String clerkId = jsonObject.getString("clerkId");
        String contentMaterialId = jsonObject.getString("contentMaterialId");
        String memberId = orderEventMqBO.getMemberId();

        InteractRecordMessageBO interactRecordMessageBO = new InteractRecordMessageBO();
        interactRecordMessageBO.setEnterpriseId(orderEventMqBO.getEnterpriseId());
        interactRecordMessageBO.setMemberId(memberId);
        interactRecordMessageBO.setClerkId(clerkId);
        interactRecordMessageBO.setStoreId("");
        interactRecordMessageBO.setMaterialId(contentMaterialId);
        interactRecordMessageBO.setEventType(MaterialInteractRecordEventType.ORDER.getCode());
        interactRecordMessageBO.setDurationTime(0);
        interactRecordMessageBO.setLastAccessTime(orderInfo.getCreateTime().getTime());
        interactRecordMessageBO.setOrderNumber(orderEventMqBO.getOrderNumber());
        interactRecordMessageService.dealRecord(interactRecordMessageBO);
        return ServiceResponse.success();
    }
}
