package com.xhpc.payment.controller;

import com.xhpc.common.core.constant.HttpStatus;
import com.xhpc.common.core.constant.StatusConstants;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.payment.service.IXhpcRefundOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/refund/order")
@Api(value = "退款订单接口", tags = "退款订单接口")
public class XhpcRefundOrderController extends BaseController {

    @Autowired
    private IXhpcRefundOrderService iXhpcRefundOrderService;

    /**
     * 申请退款
     *
     * @return
     */
    @PostMapping("/checkOut")
    @ApiOperation(value = "申请退款")
    public AjaxResult enterpriseCheckOut(@RequestBody Map<String, Object> map) {
        String amount = StringUtils.valueOf(map.get("amount"));
        if (StringUtils.isEmpty(amount)) {
            return AjaxResult.error(HttpStatus.NOT_NULL, "退款金额不能为空");
        }else{
            if(new BigDecimal(1).compareTo(new BigDecimal(amount))==1){
                return AjaxResult.error(HttpStatus.NOT_NULL, "退款金额不能少于1元");
            }
        }
        //用户信息id
        String userId = StringUtils.valueOf(map.get("userId"));
        if (StringUtils.isEmpty(userId)) {
            return AjaxResult.error(HttpStatus.NOT_NULL, "用户信息不能为空");
        }
        //openid
        String openid = StringUtils.valueOf(map.get("openid"));
        if (StringUtils.isEmpty(userId)) {
            return AjaxResult.error(HttpStatus.NOT_NULL, "用户信息不能为空");
        }
        String type = StringUtils.valueOf(map.get("type"));
        if (StringUtils.isEmpty(type)) {
            return AjaxResult.error(HttpStatus.NOT_NULL, "退款渠道不能为空");
        }

        //是否有实时数据
        int i =iXhpcRefundOrderService.countXhpcRealTimeOrder(Long.valueOf(userId));
        if (i > 0) {
            return AjaxResult.error(1103, "车辆正在充电,不能退款");
        }
        // 是否有异常订单
        int j =iXhpcRefundOrderService.countXhpcChargeOrder(Long.valueOf(userId));
        if (j > 0) {
            return AjaxResult.error(1103, "你有异常订单未解决，请拨打客服电话进行解决");
        }

        //生成退款订单
        String orderOutNumber = StringUtils.numFormat(Long.parseLong(userId), Integer.parseInt(type), StatusConstants.FLOWING_WATER_REFUND_TYPE);
        String remark = StringUtils.valueOf(map.get("remark"));
        return iXhpcRefundOrderService.addRefundOrder(userId, BigDecimal.valueOf(Double.parseDouble(amount)), type, orderOutNumber, openid, remark);
    }

    /**
     * 退款订单详情
     *
     * @param refundOrderId
     * @return
     */
    @GetMapping("/info")
    @ApiOperation(value = "退款订单详情")
    public AjaxResult info(@RequestParam Long refundOrderId) {
        return AjaxResult.success(iXhpcRefundOrderService.info(refundOrderId));
    }

    /**
     * 通过用户id查询未完成退款订单
     *
     * @param userId
     * @return
     */
    @GetMapping("/getNotRefundOrder")
    @ApiOperation(value = "用户id查询未完成退款订单")
    public AjaxResult getNotRefundOrder(@RequestParam Long userId) {
        return AjaxResult.success(iXhpcRefundOrderService.getNotRefundOrder(userId));
    }

    /**
     * 退款订单页列表
     */
    //@PreAuthorize(hasPermi = "refund:order:page")
    @GetMapping("/page")
    public TableDataInfo page(String phone, String refundOrderNumber, String status, String createTimeStart, String createTimeEnd,Integer type) {
        startPage();
        List<Map<String, Object>> list = iXhpcRefundOrderService.page(phone, refundOrderNumber, status, createTimeStart, createTimeEnd,type);
        return getDataTable(list);
    }

    /**
     * 统计
     */
    @GetMapping("/sumMoney")
    @ApiOperation(value = "退款统计订单")
    public AjaxResult sumMoney(String phone, String refundOrderNumber, String status, String createTimeStart, String createTimeEnd,Integer type) {
        return AjaxResult.success(iXhpcRefundOrderService.sumMoney(phone, refundOrderNumber, status, createTimeStart, createTimeEnd,type));
    }
}
