package com.jwipc.job.handler;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jwipc.job.dto.*;
import com.jwipc.shopping.entity.ShoppingCart;
import com.jwipc.shopping.entity.ShoppingCartInfo;
import com.jwipc.shopping.service.IMiddleWareService;
import com.jwipc.shopping.service.IOnlineMallService;
import com.jwipc.shopping.service.IShoppingCartInfoService;
import com.jwipc.shopping.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author: swq
 * @Date: 2024/4/19 14:40
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
@Component
public class GetMessageDelJob extends QuartzJobBean implements InterruptableJob {
    @Autowired
    private IShoppingCartService shoppingCartService;
    @Autowired
    private IShoppingCartInfoService shoppingCartInfoService;
    @Autowired
    IMiddleWareService middleWareService;
    @Autowired
    IOnlineMallService onlineMallService;
    @Override
    public void interrupt() throws UnableToInterruptJobException {

    }
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        log.info("商城获取推送消息删除消息任务开始");
        //获取ZKH商城token
        String token = onlineMallService.findTokenByCart("ZKH");
        //调用推送消息接口拉取数据
        Integer type = 10;//代表订单取消
        GetMessageDto gmDto = new GetMessageDto();
        gmDto.setType(type);
        gmDto.setToken(token);
        String plDtoJson = JSON.toJSONString(gmDto);
        log.info("商城获取推送消息订单取消的接口调用参数: {}", plDtoJson);
        //调用发货单明细接口
        try {
            String result = middleWareService.requestMiddleGetMessage(plDtoJson);
            log.info("商城获取推送消息订单取消的接口调用返回结果: {}", result);
            if (StringUtils.isNotEmpty(result)) {
                final GetMessageResponse orderResponse = JSON.parseObject(result, GetMessageResponse.class);
                if (orderResponse.getSuccess()) {
                    List<GetResultDto> grDtoList= orderResponse.getResult();
                    if (CollectionUtils.isNotEmpty(grDtoList)) {
                        List<ShoppingCart> shoppingCartList = new ArrayList<>();
                        grDtoList.forEach(t -> {
                            ResultTypeDto resultTypeDto= t.getResult();
                            String orderId = resultTypeDto.getOrderId();
                            Integer cancelType =resultTypeDto.getCancelType()==null?0:resultTypeDto.getCancelType();
                            String cancelReason  =resultTypeDto.getCancelReason();
                            ShoppingCart shoppingCart = shoppingCartService.getOne(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getOrderId, orderId));
                            if(!Objects.isNull(shoppingCart)){
                                if(cancelType.equals(0)){
                                    shoppingCart.setSrmStatus(8);//7天未确认自动取消
                                }else{
                                    shoppingCart.setSrmStatus(9);//电商因为自身原因无法发货而取消
                                }
                                shoppingCart.setCancelReason(cancelReason);//取消原因
                                shoppingCartList.add(shoppingCart);
                            }
                            //调用删除接口
                            String id= t.getId();
                            DelMessageDto delMessageDto = new DelMessageDto();
                            delMessageDto.setId(id);
                            delMessageDto.setToken(token);
                            String delJson = JSON.toJSONString(delMessageDto);
                            log.info("商城删除消息接口调用参数: {}", delJson);
                            try {
                                String delResult = middleWareService.requestMiddleDelMessage(delJson);
                                log.info("商城删除消息接口调用返回结果: {}", delResult);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        });
                        if(CollectionUtils.isNotEmpty(shoppingCartList)){
                            shoppingCartService.updateBatchById(shoppingCartList);//更新订单信息
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        //获取ZKH商城token
       /* String qtoken = onlineMallService.findTokenByCart("ZKH");
        Integer qtype = 105;//代表确认后订单取消申请
        GetMessageDto gmqDto = new GetMessageDto();
        gmqDto.setType(qtype);
        gmqDto.setToken(qtoken);
        String gmqJson = JSON.toJSONString(gmqDto);
        log.info("商城获取推送消息确认后订单取消的接口调用参数: {}", gmqJson);
        //调用发货单明细接口
        try {
            String qresult = middleWareService.requestMiddleGetMessage(gmqJson);
            log.info("商城获取推送消息确认后订单取消的接口调用返回结果: {}", qresult);
            if (StringUtils.isNotEmpty(qresult)) {
                final GetMessageResponse orderqResponse = JSON.parseObject(qresult, GetMessageResponse.class);
                if (orderqResponse.getSuccess()) {
                    List<GetResultDto> grqDtoList= orderqResponse.getResult();
                    if (CollectionUtils.isNotEmpty(grqDtoList)) {
                        List<ShoppingCart> qshoppingCartList = new ArrayList<>();
                        grqDtoList.forEach(q -> {
                            ResultTypeDto qresultTypeDto= q.getResult();
                            String orderId = qresultTypeDto.getOrderId();
                            Integer state =qresultTypeDto.getState();
                            ShoppingCart qshoppingCart = shoppingCartService.getOne(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getOrderId, orderId));
                            if(!Objects.isNull(qshoppingCart)){
                                if(state.equals(0)){//已同意
                                    qshoppingCart.setSrmStatus(10);//正式订单取消
                                    qshoppingCartList.add(qshoppingCart);
                                }
                            }
                            //调用删除接口
                            String qid= q.getId();
                            DelMessageDto qdelMessageDto = new DelMessageDto();
                            qdelMessageDto.setId(qid);
                            qdelMessageDto.setToken(qtoken);
                            String qdelJson = JSON.toJSONString(qdelMessageDto);
                            log.info("订单确认后商城删除消息接口调用参数: {}", qdelJson);
                            try {
                                String qdelResult = middleWareService.requestMiddleDelMessage(qdelJson);
                                log.info("订单确认后商城删除消息接口调用返回结果: {}", qdelResult);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        });
                        if(CollectionUtils.isNotEmpty(qshoppingCartList)){
                            shoppingCartService.updateBatchById(qshoppingCartList);//更新订单信息
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }*/

    }
}
