package com.liquidnet.service.executor.main.handler;

import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.feign.kylin.task.FeignPlatformAlipayBackClient;
import com.liquidnet.service.feign.platform.api.FeignPlatformApiClient;
import com.liquidnet.service.feign.platform.kylin.FeignPlatformFreightClient;
import com.liquidnet.service.feign.platform.task.FeignPlatformCandyTaskClient;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Platform
 */
@Component
public class PlatformTaskHandler {
    private static final Logger log = LoggerFactory.getLogger(PlatformTaskHandler.class);

    @Autowired
    private FeignPlatformAlipayBackClient feignPlatformAlipayBackClient;
    @Autowired
    private FeignPlatformCandyTaskClient feignPlatformCandyTaskClient;
    @Autowired
    private FeignPlatformFreightClient feignPlatformFreightClient;

    @Autowired
    FeignPlatformApiClient feignPlatformApiClient;

    @XxlJob(value = "sev-platform:overtimeRefund")
    public ReturnT<String> overtimeRefund() {
        try {
            String result = feignPlatformApiClient.overtimeRefund().getData();
            log.info("overtimeRefund：结果："+result);
            ReturnT<String> success = ReturnT.SUCCESS;
            success.setMsg(result);
            return success;
        } catch (Exception e) {
            log.error("exception of handler:{}", e.getMessage(), e);
            ReturnT<String> fail = ReturnT.FAIL;
            fail.setMsg(e.getLocalizedMessage());
            return fail;
        }
    }

    @XxlJob(value = "sev-platform:alipayActiveCallbackHandler")
    public ReturnT<String> alipayActiveCallbackHandler() {
        try {
            String result = feignPlatformAlipayBackClient.alipayActiveCallback().getData();
            log.info("alipayActiveCallback：结果："+result);
            ReturnT<String> success = ReturnT.SUCCESS;
            success.setMsg(result);
            return success;
        } catch (Exception e) {
            log.error("exception of handler:{}", e.getMessage(), e);
            ReturnT<String> fail = ReturnT.FAIL;
            fail.setMsg(e.getLocalizedMessage());
            return fail;
        }
    }

    // 券中心：发放券
    @XxlJob(value = "sev-platform:candyCouponMgtIssueCoupons")
    public ReturnT<String> candyCouponMgtIssueCoupons() {
        try {
            ResponseDto<String> dto = feignPlatformCandyTaskClient.mgtIssueCoupons();
            String dtoStr = JsonUtils.toJson(dto);
            log.info("result of handler:{}", dtoStr);
            ReturnT<String> success = ReturnT.SUCCESS;
            success.setMsg(dtoStr);
            return success;
        } catch (Exception e) {
            log.error("exception of handler:{}", e.getMessage(), e);
            ReturnT<String> fail = ReturnT.FAIL;
            fail.setMsg(e.getLocalizedMessage());
            return fail;
        }
    }

    // 券中心：码到期处理
    @XxlJob(value = "sev-platform:candyCouponDueProcessForRedeem")
    public ReturnT<String> candyCouponDueProcessForRedeem() {
        try {
            ResponseDto<String> dto = feignPlatformCandyTaskClient.dueProcessForRedeem();
            String dtoStr = JsonUtils.toJson(dto);
            log.info("result of handler:{}", dtoStr);
            ReturnT<String> success = ReturnT.SUCCESS;
            success.setMsg(dtoStr);
            return success;
        } catch (Exception e) {
            log.error("exception of handler:{}", e.getMessage(), e);
            ReturnT<String> fail = ReturnT.FAIL;
            fail.setMsg(e.getLocalizedMessage());
            return fail;
        }
    }

    // 券中心：券到期处理（公有券）
    @XxlJob(value = "sev-platform:candyCouponDueProcessForCommon")
    public ReturnT<String> candyCouponDueProcessForCommon() {
        try {
            ResponseDto<String> dto = feignPlatformCandyTaskClient.dueProcessForCommon();
            String dtoStr = JsonUtils.toJson(dto);
            log.info("result of handler:{}", dtoStr);
            ReturnT<String> success = ReturnT.SUCCESS;
            success.setMsg(dtoStr);
            return success;
        } catch (Exception e) {
            log.error("exception of handler:{}", e.getMessage(), e);
            ReturnT<String> fail = ReturnT.FAIL;
            fail.setMsg(e.getLocalizedMessage());
            return fail;
        }
    }

    // 券中心：券到期处理（私有券）
    @XxlJob(value = "sev-platform:candyCouponDueProcessForUser")
    public ReturnT<String> candyCouponDueProcessForUser() {
        try {
            ResponseDto<String> dto = feignPlatformCandyTaskClient.dueProcessForUser();
            String dtoStr = JsonUtils.toJson(dto);
            log.info("result of handler:{}", dtoStr);
            ReturnT<String> success = ReturnT.SUCCESS;
            success.setMsg(dtoStr);
            return success;
        } catch (Exception e) {
            log.error("exception of handler:{}", e.getMessage(), e);
            ReturnT<String> fail = ReturnT.FAIL;
            fail.setMsg(e.getLocalizedMessage());
            return fail;
        }
    }
    //运费
    @XxlJob(value = "sev-platform:getFreightChargeHandler")
    public ReturnT<String> getFreightChargeHandler() {
        try {
            ResponseDto<Object> dto = feignPlatformFreightClient.getFreightCharge();
            String dtoStr = JsonUtils.toJson(dto);
            log.info("result of handler:{}", dtoStr);
            ReturnT<String> success = ReturnT.SUCCESS;
            success.setMsg(dtoStr);
            return success;
        } catch (Exception e) {
            log.error("exception of handler:{}", e.getMessage(), e);
            ReturnT<String> fail = ReturnT.FAIL;
            fail.setMsg(e.getLocalizedMessage());
            return fail;
        }
    }


    // 关注任务加积分脚本
    @XxlJob(value = "sev-platform:followDoTask")
    public ReturnT<String> followDoTask() {
        try {
            ResponseDto<String> dto = feignPlatformCandyTaskClient.doTask();
            String dtoStr = JsonUtils.toJson(dto);
            log.info("result of handler:{}", dtoStr);
            ReturnT<String> success = ReturnT.SUCCESS;
            success.setMsg(dtoStr);
            return success;
        } catch (Exception e) {
            log.error("exception of handler:{}", e.getMessage(), e);
            ReturnT<String> fail = ReturnT.FAIL;
            fail.setMsg(e.getLocalizedMessage());
            return fail;
        }
    }
}
