package com.app.controller.admin;

import com.app.annotation.platform.OperationLog;
import com.app.constant.SystemConstant;
import com.app.dto.PageResult;
import com.app.dto.Result;
import com.app.dto.order.operation.AuditRefundDto;
import com.app.dto.order.operation.ExportOrderDto;
import com.app.dto.order.operation.RefundDto;
import com.app.dto.order.search.SearchAdminOpenCourseRefundDto;
import com.app.entity.order.OpenCourseRefund;
import com.app.service.order.OpenCourseOrderExportService;
import com.app.service.order.OpenCourseRefundService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Api(description = "开课 - 退课")
@Slf4j
@Validated
@RestController("AdminOpenCourseRefundController")
@RequestMapping("/admin/order/refund")
public class OpenCourseRefundController {

    @Autowired
    private OpenCourseRefundService openCourseRefundService;

    @Autowired
    private OpenCourseOrderExportService openCourseOrderExportService;

    /**
     * 功能描述：查询所有[退课申请]
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "查询所有[订单]")
    @PostMapping("/page")
    public ResponseEntity<Result<PageResult<OpenCourseRefund>>> page(@Valid @RequestBody SearchAdminOpenCourseRefundDto dto) {
        return new ResponseEntity(new Result().ok(openCourseRefundService.page(
                dto.getChildName(), dto.getPhoneNumber(), dto.getOpenCourseName(),
                dto.getAccountId(), dto.getTransactionId(), dto.getState(),
                dto.getSchoolYear(), dto.getSemester(),
                dto.getSchoolId(), dto.getGradeName(), dto.getClassName(),dto.getOrganIds(),
                dto.getPage(), dto.getSize()
        )), HttpStatus.OK);
    }

    /**
     * 功能描述：审核退课
     *
     * @param dto
     * @return
     */
    @OperationLog("更新 - 审核退课")
    @ApiOperation(value = "审核退课")
    @PostMapping("/audit-refund")
    public ResponseEntity<Result<PageResult<OpenCourseRefund>>> auditRefund(@Valid @RequestBody AuditRefundDto dto) {
        String organId = SystemConstant.organId;
        openCourseRefundService.auditRefund(organId, dto.getOpenCourseRefundId(), dto.getState(), dto.getRemarks(), dto.getRefundId());
        return new ResponseEntity(new Result().ok(), HttpStatus.OK);
    }

    @ApiOperation(value = "导出[订单]")
    @PostMapping("/export-excel")
    public void exportExcel(@Valid @RequestBody ExportOrderDto params, HttpServletRequest request, HttpServletResponse response) throws Exception {
        openCourseOrderExportService.exportRefundExcel(params, request, response);
    }

    /**
     * 功能描述：审核退课
     *
     * @param refundDto
     * @return
     */
    @OperationLog("更新 - 退课")
    @ApiOperation(value = "退课")
    @PostMapping("/refund")
    public ResponseEntity<Result<?>> refund(@Valid @RequestBody RefundDto refundDto) {
       try {
           String organId = SystemConstant.organId;
           OpenCourseRefund refund = openCourseRefundService.apply(organId, refundDto.getOpenCourseOrderItemId(), refundDto.getRemarks(), refundDto.getAccountId(), refundDto.getWxmpFormId());
           return new ResponseEntity(new Result().ok(), HttpStatus.OK);
       } catch (Exception e) {
           return new ResponseEntity(new Result().serverError(e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
       }
    }

}
