package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.DateUtil;
import com.hfkj.common.utils.IDGenerator;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsTradeOrder;
import com.hfkj.entity.BsTradeOrderRefund;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserInfoModel;
import com.hfkj.service.BsTradeOrderRefundService;
import com.hfkj.service.BsTradeOrderService;
import com.hfkj.sysenum.TradeOrderRefundStatusEnum;
import com.hfkj.sysenum.TradeOrderStatusEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
@Api(value = "交易订单退款")
@RequestMapping(value = "/tradeOrderRefund")
public class BsTradeOrderRefundController {

    private static Logger log = LoggerFactory.getLogger(BsTradeOrderRefundController.class);

    @Resource
    private BsTradeOrderService tradeOrderService;

    @Resource
    private BsTradeOrderRefundService tradeOrderRefundService;

    @Resource
    private UserCenter userCenter;

    @RequestMapping(value="/refund",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "订单退款")
    public ResponseData refund(@RequestBody JSONObject body) {
        try {
            UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userInfoModel.getStore() == null) {
                log.error("configAccount error!","权限不足");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMPETENCE_INSUFFICIENT, "");
            }
            if (body == null
                    || body.getString("outTradeNo") == null
                    || body.getBigDecimal("refundAmount") == null) {
                log.error("configStore error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            // 查询交易订单
            BsTradeOrder order = tradeOrderService.getOrderByOutTradeNo(body.getString("outTradeNo"));
            if (order == null) {
                log.error("configMerBasis error!","未找到订单");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到订单");
            }
            if (!order.getStoreId().equals(userInfoModel.getStore().getId())) {
                log.error("configMerBasis error!","权限不足");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "权限不足");
            }
            if (!order.getStatus().equals(TradeOrderStatusEnum.status3.getNumber())) {
                log.error("configMerBasis error!","订单不处于支付成功状态");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "订单不处于支付成功状态");
            }
            if (DateUtil.getMinutesDiff(order.getPayTime(), new Date()) >= 4320L) {
                log.error("configMerBasis error!","无法退款，支付时间大于三天");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法退款，支付时间大于三天");
            }
 /*           // 查询存在退款记录
            if (tradeOrderRefundService.getRefundByOutTradeOrder(body.getString("outTradeNo")) != null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "订单处于退款中");
            }*/
            // 生成退款订单号
            String refundNo = "1170"+ DateUtil.format(new Date(), DateUtil.YMDHMS) + IDGenerator.nextId(10);

            // 退款订单
            BsTradeOrderRefund orderRefund = new BsTradeOrderRefund();
            orderRefund.setTradeOrderId(order.getId());
            orderRefund.setOutTradeNo(order.getOutTradeNo());
            orderRefund.setTradeOrderId(order.getId());
            orderRefund.setAgentId(order.getAgentId());
            orderRefund.setSalesmanId(order.getSalesmanId());
            orderRefund.setMerId(order.getMerId());
            orderRefund.setStoreId(order.getStoreId());
            orderRefund.setPlatformType(order.getPlatformType());
            orderRefund.setRefundNo(refundNo);
            orderRefund.setTotalAmount(order.getTradeAmount());
            orderRefund.setRefundAmount(body.getBigDecimal("refundAmount"));
            orderRefund.setRefundReason(body.getString("refundReason"));
            orderRefund.setStatus(TradeOrderRefundStatusEnum.status1.getNumber());
            orderRefund.setOperatorId(userInfoModel.getSecUser().getId());
            orderRefund.setOperatorName(userInfoModel.getSecUser().getUserName());

            return ResponseMsgUtil.success(tradeOrderRefundService.tradeOrderRefund(orderRefund));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }
}
