package com.xxfc.platform.order.rest;

import cn.hutool.core.util.StrUtil;
import com.github.wxiaoqi.security.admin.feign.UserFeign;
import com.github.wxiaoqi.security.admin.feign.rest.UserRestInterface;
import com.github.wxiaoqi.security.auth.client.annotation.IgnoreClientToken;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.rest.BaseController;
import com.github.wxiaoqi.security.common.util.Query;
import com.github.wxiaoqi.security.common.vo.PageParam;
import com.xxfc.platform.order.biz.BaseOrderBiz;
import com.xxfc.platform.order.biz.OrderAccountBiz;
import com.xxfc.platform.order.biz.OrderItemBiz;
import com.xxfc.platform.order.biz.OrderRefundBiz;
import com.xxfc.platform.order.biz.inner.OrderCalculateBiz;
import com.xxfc.platform.order.contant.enumerate.OrderStatusEnum;
import com.xxfc.platform.order.entity.OrderRefund;
import com.xxfc.platform.order.pojo.calculate.OrderRefundPriceVO;
import com.xxfc.platform.order.pojo.order.OrderPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

@Controller
@RequestMapping("orderRefund")
public class OrderRefundController extends BaseController<OrderRefundBiz,OrderRefund> implements UserRestInterface {

    @Autowired
    BaseOrderBiz baseOrderBiz;

    @Autowired
    OrderAccountBiz orderAccountBiz;

    @Autowired
    OrderItemBiz orderItemBiz;

    @Autowired
    OrderCalculateBiz orderCalculateBiz;

    @Autowired
    UserFeign userFeign;

    @Override
    public UserFeign getUserFeign() {
        return userFeign;
    }

    @RequestMapping(value = "/price/calculate/{no}", method = RequestMethod.GET)
    @ResponseBody
    @IgnoreClientToken
    public ObjectRestResponse<BigDecimal> getPriceCalculate(@PathVariable(value = "no") String no) {
        checkAppUser();
        OrderPageVO orderPageVO = baseOrderBiz.pageByParm(new Query(new PageParam(){{
            setLimit(1);
            setPage(1);
        }}){{put("no", no); }}.getSuper()).get(0);
        OrderRefundPriceVO orderRefundPriceVO = orderCalculateBiz.getPriceCalculate(no, orderPageVO);
        if(orderPageVO.getStatus().equals(OrderStatusEnum.ORDER_UNPAY.getCode())
                || orderPageVO.getStatus().equals(OrderStatusEnum.ORDER_CRT.getCode())
                || orderRefundPriceVO.getRefundAmount().compareTo(BigDecimal.ZERO) <= 0) {
            orderRefundPriceVO.setRefundDesc("是否确定取消订单");
        }else {
            orderRefundPriceVO.setRefundDesc(StrUtil.format("本次取消操作需要扣除{}元违约金，实际退款金额为{}元，您确定要取消订单吗？", orderRefundPriceVO.getCutAmount(), orderRefundPriceVO.getRefundAmount()));
        }
        return ObjectRestResponse.succ(orderRefundPriceVO);
    }

    @RequestMapping(value = "/bg-stage/getViolatePrice/{no}", method = RequestMethod.GET)
    @ResponseBody
    @IgnoreClientToken
    public ObjectRestResponse<BigDecimal> getViolatePrice(@PathVariable(value = "no") String no) {
        checkAdminUser();
        OrderPageVO orderPageVO = baseOrderBiz.pageByParm(new Query(new PageParam(){{
            setLimit(1);
            setPage(1);
        }}){{put("no", no); }}.getSuper()).get(0);
        OrderRefundPriceVO orderRefundPriceVO = orderCalculateBiz.getPriceCalculate(no, orderPageVO);
        return ObjectRestResponse.succ(orderRefundPriceVO);
    }


}