package com.xhpc.payment.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
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.IXhpcRechargeOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@EnableScheduling
@RestController
@RequestMapping("/recharge/order")
@Api(value = "充值订单接口", tags = "充值订单接口")
public class XhpcRechargeOrderController extends BaseController {

    @Autowired
    private IXhpcRechargeOrderService iXhpcRechargeOrderService;

    /**
     * 充值订单详情
     *
     * @param rechargeOrderId
     * @return
     */
    @GetMapping("/info")
    @ApiOperation(value = "充值订单详情")
    public AjaxResult info(@RequestParam Long rechargeOrderId) {
        return AjaxResult.success(iXhpcRechargeOrderService.info(rechargeOrderId));
    }

    /**
     * 充值订单分页列表
     */
    //@PreAuthorize(hasPermi = "refund:order:page")
    @GetMapping("/page")
    public TableDataInfo page(String phone, String rechargeOrderNumber, String status, String createTimeStart, String createTimeEnd,Integer type,Integer source,Integer userId) {
        startPage();
        List<Map<String, Object>> list = iXhpcRechargeOrderService.page(phone, rechargeOrderNumber, status, createTimeStart, createTimeEnd,type,source,userId);
        return getDataTable(list);
    }



    /**
     * 充值订单页表导出
     */
    //@PreAuthorize(hasPermi = "refund:order:page")
    @PostMapping("/export")
    public void page(HttpServletResponse response,
                     String phone,
                     String rechargeOrderNumber,
                     String status,
                     String createTimeStart,
                     String createTimeEnd,
                     Integer type,
                     Integer source,
                     Integer userId) throws IOException {


        iXhpcRechargeOrderService.export(response, phone, rechargeOrderNumber, status, createTimeStart, createTimeEnd,type,source,userId);

    }

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

    @GetMapping("/sumUserMoney")
    @ApiOperation(value = "充值金额")
    public AjaxResult sumUserMoney(String phone, String refundOrderNumber, String status, String createTimeStart, String createTimeEnd,Integer type,Integer source,Integer userId) {
        return AjaxResult.success(iXhpcRechargeOrderService.sumUserMoney(phone, refundOrderNumber, status, createTimeStart, createTimeEnd,type,source,userId));
    }
    /**
     * 每隔30分钟，清理一次未支付的订单
     */
    //@Scheduled(cron = "0 0/30 * * * ?")
    @GetMapping("/test1")
    public void test1(){
        iXhpcRechargeOrderService.updateRechargeOrderStatus();
    }

}
