package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.pay.util.SignatureUtil;
import com.hfkj.common.security.VerifyCode;
import com.hfkj.common.security.VerifyCodeStorage;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsAgentApiLogWithBLOBs;
import com.hfkj.entity.BsAgentApiParam;
import com.hfkj.model.ResponseData;
import com.hfkj.model.order.OrderModel;
import com.hfkj.openapi.model.request.RequestOrderRefundModel;
import com.hfkj.openapi.service.ApiGasOrderService;
import com.hfkj.service.agent.BsAgentApiAccountService;
import com.hfkj.service.agent.BsAgentApiLogService;
import com.hfkj.service.agent.BsAgentApiParamService;
import com.hfkj.service.order.BsOrderRefundService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Random;

@RestController
@RequestMapping(value="/oaApprovalNotify")
@Api(value="OA审批通知")
public class OaApprovalNotifyController {
	Logger log = LoggerFactory.getLogger(OaApprovalNotifyController.class);
	@Resource
	private BsAgentApiParamService agentApiParamService;
	@Resource
	private BsOrderRefundService orderRefundService;
	@Resource
	private BsAgentApiLogService agentApiLogService;

	@RequestMapping(value="/refund",method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "退款申请")
	public ResponseData refund(@Validated @RequestBody RequestOrderRefundModel body) {
		log.info("========= Start 退款申请 Start ===========");
		log.info("请求参数：" + JSONObject.toJSONString(body));
		BsAgentApiLogWithBLOBs apiLog = new BsAgentApiLogWithBLOBs();
		try {
			// 验证签名
			BsAgentApiParam apiParam = agentApiParamService.getParamByAppId(body.getAppId());
			if (!SignatureUtil.checkSign(body.getSign(), body, apiParam.getAppSecret())) {
				throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.OPEN_API_SIGN_ERR, "");
			}
			apiLog.setAppId(body.getAppId());
			apiLog.setRequestUrl("oaApprovalNotify/refund");
			apiLog.setRequestParam(JSONObject.toJSONString(body));

			// 申请退款
			orderRefundService.tradeOrderRefund(body.getOrderNo(), body.getRefundReason());
			log.info("返回参数：" + "退款申请操作成功");
			apiLog.setResponseParam("退款申请操作成功");
			return ResponseMsgUtil.success("退款申请操作成功");

		} catch (Exception e) {
			log.info("出现异常：", e);
			// 异常内容
			ResponseData exception = ResponseMsgUtil.exception(e);
			apiLog.setErrorContent(JSONObject.toJSONString(exception));
			return exception;
		} finally {
			// 记录日志
			if (apiLog.getAppId() != null) {
				agentApiLogService.edit(apiLog);
			}
			log.info("========= END 退款申请 END ===========");
		}
	}
}
