package cn.freemud.management.controller;

import cn.freemud.base.entity.BaseResponse;
import cn.freemud.management.entities.dto.request.order.McCafeOrderManagerRequest;
import cn.freemud.management.entities.dto.request.order.OrderManagerRequest;
import cn.freemud.management.entities.dto.request.order.RefundSuccessCallbackVo;
import cn.freemud.management.enums.McCafeOperateType;
import cn.freemud.management.enums.OperateType;
import cn.freemud.management.enums.ResponseResult;
import cn.freemud.management.service.adapter.OrderManagerAdapter;
import cn.freemud.management.util.ResponseUtil;
import com.freemud.application.sdk.api.log.ApiAnnotation;
import com.freemud.application.sdk.api.log.LogParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * All rights Reserved, Designed By www.freemud.cn
 *
 * @version V1.0
 * @Title: OrderManagerController
 * @Package cn.freemud.management.controller
 * @Description:
 * @author: shuhu.hou
 * @date: 2020/3/26 16:09
 * @Copyright: 2020 www.freemud.cn Inc. All rights reserved.
 * 注意：本内容仅限于上海非码科技内部传阅，禁止外泄以及用于其他的商业目
 */
@RequestMapping("/order")
@RestController
@Validated
public class OrderManagerController {

    @Autowired
    private OrderManagerAdapter orderManagerAdapter;


    @ApiAnnotation(logMessage = "statusOperate", printInfoLog = true)
    @ApiOperation(value = "oms订单履单", notes = "订单履单", produces = "application/json")
    @PostMapping("/statusOperate")
    public BaseResponse statusOperate(@Validated @LogParams @RequestBody OrderManagerRequest request) {
        OperateType byOpType = OperateType.getByOpType(request.getOperateType());
        if (byOpType==null){
            return ResponseUtil.error(ResponseResult.ORDER_OPERATE_TYPE_NOT_VALIDATE);
        }
        switch (byOpType) {
            case ORDER_CONFIRM:
                return orderManagerAdapter.orderConfirmTake(request);
            case ORDER_DONE:
                return orderManagerAdapter.orderDone(request);
            case ORDER_SEND:
                return orderManagerAdapter.orderDelivery(request);
            case ORDER_AFFIRM:
                return orderManagerAdapter.orderAffirm(request,null);
            case ORDER_REJECT:
                return orderManagerAdapter.orderReject(request);
            case ORDER_AGREE_REFUND:
                return orderManagerAdapter.orderAgreeRefund(request);
            case ORDER_REJECT_REFUND:
                return orderManagerAdapter.orderRejectRefund(request);
            case ORDER_CANCEL:
                return orderManagerAdapter.orderCancel(request);
            case ORDER_CREATE_DELIVERY:
                return orderManagerAdapter.orderCreateDelivery(request);
            case ORDER_CANCEL_DELIVERY:
                return orderManagerAdapter.orderCancelDelivery(request);
            default:
                return ResponseUtil.error(ResponseResult.ORDER_OPERATE_TYPE_NOT_VALIDATE);
        }
    }

    /**
     * 同意退款, 支付服务退款失败的补偿接口
     *
     * 退款成功会回调该接口进行后续流程补偿
     */
    @ApiAnnotation(logMessage = "/order/refund/callback")
    @PostMapping("/refund/callback")
    public BaseResponse refundSuccessCallback(@Validated @LogParams @RequestBody RefundSuccessCallbackVo req) {
        return orderManagerAdapter.refundSuccessCallback(req);
    }

    @ApiAnnotation(logMessage = "mcCafeDeliveryCreate")
    @ApiOperation(value = "麦咖啡订单入机", notes = "麦咖啡订单入机", produces = "application/json")
    @PostMapping("/mcCafe/statusOperate")
    public BaseResponse mcCafeDeliveryCreate(@Validated @LogParams @RequestBody McCafeOrderManagerRequest request) {
        OrderManagerRequest orderManagerRequest = new OrderManagerRequest();
        BeanUtils.copyProperties(request, orderManagerRequest);
        String reason = request.getReason();
        if(StringUtils.isNotEmpty(reason)) {
            if(reason.length() > 50) {
                orderManagerRequest.setReason(reason.substring(reason.length()-50));
            }
            if(reason.length() > 200) {
                orderManagerRequest.setRemark(reason.substring(reason.length()-200));
            } else {
                orderManagerRequest.setRemark(reason);
            }
        }
        McCafeOperateType byOpType = McCafeOperateType.getByOpType(request.getOperateType());
        if (byOpType==null){
            return ResponseUtil.error(ResponseResult.ORDER_OPERATE_TYPE_NOT_VALIDATE);
        }
        switch (byOpType) {
            case ORDER_SUCCESS:
                return orderManagerAdapter.orderConfirmTake(orderManagerRequest);
            case ORDER_REFUND:
                return orderManagerAdapter.orderReject(orderManagerRequest);
            default:
                return ResponseUtil.error(ResponseResult.ORDER_OPERATE_TYPE_NOT_VALIDATE);
        }
    }
}
