package com.yida.data.mall.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.mall.dto.*;
import com.yida.data.mall.service.EduBillService;
import com.yida.data.mall.service.EduBillStudentService;
import com.yida.data.mall.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 缴费账单表 Controller
 *
 * @author ZYJ
 * @date 2021-12-24 09:47:02
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("eduBill")
@Api(tags = "缴费账单后台api")
public class EduBillController {

    private final EduBillService eduBillService;
    private final EduBillStudentService eduBillStudentService;

    @PostMapping(value = "listBillPage")
    @ApiOperation(value = "查询缴费账单列表数据")
    public ResultBean<IPage<BillSelectPageVO>> listBillPage(@Valid BillSelectPageDTO billSelectPageDTO) {
        IPage<BillSelectPageVO> page = eduBillService.listBillPage(billSelectPageDTO);
        return ResultBean.buildSuccess(page);
    }

    @GetMapping(value = "getBillInfo")
    @ApiOperation(value = "获取账单详细信息")
    public ResultBean<BillInfoVO> getBillInfo(@ApiParam(value = "账单id", required = true) @RequestParam Long billId) {
        return ResultBean.buildSuccess(eduBillService.getBillInfo(billId));
    }

    @PostMapping(value = "/saveBill", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "保存缴费单数据", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean<String> saveBill(@Valid @RequestBody BillSaveDTO billSaveDTO) {
        eduBillService.saveBill(billSaveDTO);
        return ResultBean.buildSuccess();
    }

    @GetMapping("updateBillEnableStatus")
    @ApiOperation("修改缴费单状态")
    public ResultBean<String> updateBillEnableStatus(
            @ApiParam(value = "账单id", required = true) @RequestParam Long billId,
            @ApiParam(value = "状态", required = true) @RequestParam Integer enableStatus) {
        eduBillService.updateBillEnableStatus(billId, enableStatus);
        return ResultBean.buildSuccess();
    }

    @GetMapping(value = "deleteBill")
    @ApiOperation(value = "删除账单")
    public ResultBean<String> deleteBill(@ApiParam(value = "账单id", required = true) @RequestParam Long billId) {
        eduBillService.deleteBill(billId);
        return ResultBean.buildSuccess();
    }

    @PostMapping("/listBillUserPage")
    @ApiOperation("查询账单人员分页列表")
    public ResultBean<IPage<BillStudentInfoVO>> listBillUserPage(BillStudentSelectPageDTO billStudentSelectPageDTO) {
        IPage<BillStudentInfoVO> page = eduBillStudentService.listBillUserPage(billStudentSelectPageDTO);
        return ResultBean.buildSuccess(page);
    }

    @PostMapping("/listBillUser")
    @ApiOperation("查询账单人员列表")
    public ResultBean<List<BillStudentInfoVO>> listBillUser(
            @ApiParam(value = "账单id", required = true) @RequestParam Long billId) {
        return ResultBean.buildSuccess(eduBillStudentService.listBillUser(billId));
    }

    @PostMapping(value = "/saveBillUser", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "添加缴费单人员", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean<String> saveBillUser(@Valid @RequestBody BillUserSaveDTO billUserSaveDTO) {
        eduBillStudentService.saveBillUser(billUserSaveDTO);
        return ResultBean.buildSuccess();
    }

    @GetMapping(value = "deleteBillUser")
    @ApiOperation(value = "删除账单人员")
    public ResultBean<String> deleteBillUser(
            @ApiParam(value = "账单人员id", required = true) @RequestParam Long billStudentId,
            @ApiParam(value = "学生id", required = true) @RequestParam Long studentId,
            @ApiParam(value = "账单id", required = true) @RequestParam Long billId) {
        eduBillStudentService.deleteBillUser(billStudentId, studentId, billId);
        return ResultBean.buildSuccess();
    }

    @GetMapping(value = "listBillPayDetailTableCol")
    @ApiOperation(value = "查询账单明细缴费项动态表头")
    public ResultBean<List<BillItemInfoVO>> listBillPayDetailTableCol(
            @ApiParam(value = "账单id", required = true) @RequestParam Long billId) {
        return ResultBean.buildSuccess(eduBillService.listBillPayDetailTableCol(billId));
    }

    @PostMapping("/listBillPayDetailPage")
    @ApiOperation("查询账单人员缴费明细")
    public ResultBean<IPage<Map<String, String>>> listBillPayDetailPage(BillStudentSelectPageDTO billStudentSelectPageDTO) {
        IPage<Map<String, String>> page = eduBillStudentService.listBillPayDetailPage(billStudentSelectPageDTO);
        return ResultBean.buildSuccess(page);
    }

    @PostMapping("/exportBillPayData")
    @ApiOperation("导出缴费明细数据")
    public ResultBean<String> exportBillPayData(@RequestBody BillStudentSelectPageDTO billStudentSelectPageDTO,
                                                HttpServletResponse response) {
        return ResultBean.buildSuccess(eduBillStudentService.exportBillPayData(billStudentSelectPageDTO, response));
    }

    @PostMapping(value = "listBillOrderPage")
    @ApiOperation(value = "查询账单订单列表数据")
    public ResultBean<IPage<BillOrderSelectPageVO>> listBillOrderPage(BillOrderSelectPageDTO dto) {
        IPage<BillOrderSelectPageVO> page = eduBillService.listBillOrderPage(dto);
        return ResultBean.buildSuccess(page);
    }

    @GetMapping(value = "refundOrder")
    @ApiOperation(value = "缴费账单退款")
    public ResultBean<String> refundOrder(
            @ApiParam(value = "订单号", required = true) @RequestParam String orderCode) {
        eduBillService.refundOrder(orderCode);
        return ResultBean.buildSuccess();
    }
}
