package com.miaxis.pc;

import com.miaxis.common.annotation.Log;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.core.page.ResponsePageInfo;
import com.miaxis.common.enums.BusinessTypeEnum;
import com.miaxis.common.utils.poi.ExcelUtil;
import com.miaxis.order.domain.OrderInfo;
import com.miaxis.order.dto.QuerySchoolOrderListDTO;
import com.miaxis.order.service.IOrderInfoService;
import com.miaxis.order.vo.OrderIncomeVo;
import com.miaxis.order.vo.QuerySchoolOrderDetailedVo;
import com.miaxis.order.vo.QuerySchoolOrderListVo;
import com.miaxis.wx.domain.WxJsOrder;
import com.miaxis.wx.service.IRefundRecordService;
import com.miaxis.wx.service.IWxJsOrderService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.miaxis.common.utils.OrderCodeFactory.getOrderCode;

/**
 * 【订单信息】Controller
 *
 * @author miaxis
 * @date 2022-06-07
 */
@RestController
@RequestMapping("/order/info")
@Api(tags={"【PC-订单信息】"})
public class PcOrderInfoController extends BaseController{
    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    private IWxJsOrderService wxJsOrderService;

    @Autowired
    private IRefundRecordService refundRecordService;

    /**
     * 查询订单信息列表
     */
    @GetMapping("/list")
    @ApiOperation("查询订单信息列表")
        @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码" ,dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize",value = "每页数据量" , dataType = "int", paramType = "query", required = false),
    })
    public ResponsePageInfo<OrderInfo> list(@ModelAttribute OrderInfo orderInfo){
        startPage();
        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
        return toResponsePageInfo(list);
    }


    /**
     * 获取订单信息详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取订单信息详细信息")
    public Response<OrderInfo> getInfo(
            @ApiParam(name = "id", value = "订单信息参数", required = true)
            @PathVariable("id") Long id
    ){
        return Response.success(orderInfoService.getById(id));
    }

    /**
     * 查询驾校订单
     */
    @GetMapping("/school/list")
    @ApiOperation("查询驾校报表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码" ,dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize",value = "每页数据量" , dataType = "int", paramType = "query", required = false),
    })
    public ResponsePageInfo<QuerySchoolOrderListVo> list(@ModelAttribute QuerySchoolOrderListDTO schoolOrderListDTO) {
        startPage();
        List<QuerySchoolOrderListVo> list = orderInfoService.selectSchoolOrderInfoList(schoolOrderListDTO);
        ResponsePageInfo<QuerySchoolOrderListVo> orderInfoResponsePageInfo = toResponsePageInfo(list);
        Integer total = orderInfoService.getTotalMoney(schoolOrderListDTO);
        Integer commission = orderInfoService.getCommissionMoney(schoolOrderListDTO);

        Map<String,Integer> map = new HashMap<String,Integer>();
        map.put("total",total);
        map.put("commission",commission);
        orderInfoResponsePageInfo.setData(map);
        return orderInfoResponsePageInfo;

    }

    /**
     * 查询月份总收益
     */
    @GetMapping("/school/getIncomeList")
    @ApiOperation("查询月份总收益")
    public List<OrderIncomeVo> getIncomeList(@ModelAttribute QuerySchoolOrderListDTO schoolOrderListDTO) {
        startPage();
        List<OrderIncomeVo> list = orderInfoService.getIncomeList(schoolOrderListDTO);
        return list;
    }


    /**
     * 查询驾校订单明细列表
     */
    @GetMapping("/school/detailed")
    @ApiOperation("查询驾校订单明细列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码" ,dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize",value = "每页数据量" , dataType = "int", paramType = "query", required = false),
    })
    public ResponsePageInfo<OrderInfo> detailed(@ModelAttribute QuerySchoolOrderListDTO schoolOrderListDTO) {
        startPage();
        List<OrderInfo> list = orderInfoService.selectSchoolOrderDetailed(schoolOrderListDTO);
        return toResponsePageInfo(list);
    }


    /**
     * 导出订单报表信息
     */
    @Log(title = "导出订单明细", businessType = BusinessTypeEnum.EXPORT)
    @GetMapping("/exportDetailed")
    @ApiOperation("导出驾校订单明细Excel")
    public Response<String> exportDetailed(@ModelAttribute QuerySchoolOrderListDTO schoolOrderListDTO){
        List<QuerySchoolOrderDetailedVo> list = orderInfoService.selectSchoolOrderDetailedExcel(schoolOrderListDTO);
        ExcelUtil<QuerySchoolOrderDetailedVo> util = new ExcelUtil<QuerySchoolOrderDetailedVo>(QuerySchoolOrderDetailedVo.class);
        return util.exportExcel(list, "info");
    }


    /**
     * 发起退款
     */
    @PutMapping(value = "/refund/{outTradeNo}")
    @ApiOperation("发起退款")
    public Response<OrderInfo> refund(
            @ApiParam(name = "outTradeNo", value = "订单信息参数", required = true)
            @PathVariable("outTradeNo") String outTradeNo,
            String reason
    ) throws Exception {
        String refundCode = getOrderCode(null);
        WxJsOrder order = wxJsOrderService.getByOutTradeNo(outTradeNo);
        refundRecordService.refund(order,refundCode,reason);
        //TODO 取消会员
        orderInfoService.refundOrder(order);
        return Response.success();
    }


    /**
     * 导出订单报表信息
     */
    @Log(title = "导出订单报表信息", businessType = BusinessTypeEnum.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出订单报表信息Excel")
    public Response<String> export(@ModelAttribute QuerySchoolOrderListDTO schoolOrderListDTO){
        List<QuerySchoolOrderListVo> list = orderInfoService.selectSchoolOrderInfoExcel(schoolOrderListDTO);
        ExcelUtil<QuerySchoolOrderListVo> util = new ExcelUtil<QuerySchoolOrderListVo>(QuerySchoolOrderListVo.class);
        return util.exportExcel(list, "info");
    }


}
