package com.shkj.wms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shkj.common.constant.RedisConstant;
import com.shkj.common.core.domain.Result;
import com.shkj.common.utils.DateUtils;
import com.shkj.wms.domain.SysThirdExceptionRequest;
import com.shkj.wms.enums.ThirdApiStatusEnum;
import com.shkj.wms.enums.ThirdApiTypeEnum;
import com.shkj.wms.mapper.SysThirdExceptionRequestMapper;
import com.shkj.wms.service.ISysThirdExceptionRequestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 第三方请求异常Service业务层处理
 *
 * @author lch
 * @date 2023-02-02
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class SysThirdExceptionRequestServiceImpl extends ServiceImpl<SysThirdExceptionRequestMapper, SysThirdExceptionRequest> implements ISysThirdExceptionRequestService {

    @Override
    public Result executeException(String taskCode) throws InterruptedException {

        SysThirdExceptionRequest request = getOne(new LambdaQueryWrapper<SysThirdExceptionRequest>()
                .eq(SysThirdExceptionRequest::getCode, taskCode)
                .eq(SysThirdExceptionRequest::getStatus, ThirdApiStatusEnum.fail.getValue())
                .last("limit 1").orderByDesc(SysThirdExceptionRequest::getCreateTime));

        log.info("executeException = {}", request);
        if (request == null) {
            return Result.err().msg("不存在异常信息");
        }

        String type = request.getType();
        Result result;
        switch (type) {
            case ThirdApiTypeEnum.wcsOutTask:
            case ThirdApiTypeEnum.wcsCallOutStock:


                break;
            default:
                log.info("type = {}", type);
                return Result.err().msg("类型异常");
        }


        //更新数据
        log.info("定时任务更新数据 = {}", request);
        request.setStatus(ThirdApiStatusEnum.success.getValue());
        request.setUpdateTime(DateUtils.getNowDate());
        this.updateById(request);


        return Result.ok();
    }




    @Override
    public void addException(String code, String type, String requestBody, String responseBody, Boolean isSuccess, Date planTime,Integer layer) {
        SysThirdExceptionRequest request = new SysThirdExceptionRequest();
        request.setCode(code);
        request.setType(type);
        request.setStatus(isSuccess ? ThirdApiStatusEnum.success.getValue() : ThirdApiStatusEnum.fail.getValue());
        request.setRequestBody(requestBody);
        request.setResponseBody(responseBody);
        request.setCreateBy("admin").setCreateTime(new Date());
        request.setPlanTime(planTime);
        request.setUpdateTime(DateUtils.getNowDate());
        request.setLayer(layer);
        this.save(request);
    }

    @Override
    public List<SysThirdExceptionRequest> getListByTaskIdAndOrderNo(Long taskId, String orderNo) {
        LambdaQueryWrapper<SysThirdExceptionRequest> eq = new LambdaQueryWrapper<SysThirdExceptionRequest>().eq(SysThirdExceptionRequest::getTaskId, taskId)
                .eq(SysThirdExceptionRequest::getOrderNo, orderNo);
        return list(eq);
    }

    @Override
    public SysThirdExceptionRequest getOneByOrderNo(String orderNo) {
        SysThirdExceptionRequest oneByOrderNo = this.baseMapper.getOneByOrderNo(orderNo);
        return oneByOrderNo;
    }


    /**
     * 得到第三异常请求列表
     *
     * @return {@code List<SysThirdExceptionRequest>}
     */
    public List<SysThirdExceptionRequest> getThirdExceptionRequestList() {
        LambdaQueryWrapper<SysThirdExceptionRequest> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(SysThirdExceptionRequest::getCreateTime);
        wrapper.last("limit 20");
        return list(wrapper);
    }

    /**
     * 做任务  定时任务
     */
    @Override
    //@Scheduled(fixedDelay = 10000)
    public void doTask() {
        LambdaQueryWrapper<SysThirdExceptionRequest> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysThirdExceptionRequest::getStatus, ThirdApiStatusEnum.fail.getValue());
        wrapper.orderByAsc(SysThirdExceptionRequest::getCreateTime);
        List<SysThirdExceptionRequest> thirdExceptionRequestList = list(wrapper);
        // log.info("定时任务当前执行数量 = {}", thirdExceptionRequestList.size());
        for (SysThirdExceptionRequest request : thirdExceptionRequestList) {
            try {
                Date planTime = request.getPlanTime();
                if (planTime == null) {
                    executeException(request.getCode());
                } else {
                    boolean after = planTime.before(DateUtils.getNowDate());
                    if (after) {
                        executeException(request.getCode());
                    }
                }
            } catch (Exception e) {
                log.error("doTask = {}", e);
            }
        }
    }

}
