package com.cool.store.controller.webb;

import com.cool.store.context.CurrentUserHolder;
import com.cool.store.context.LoginUserInfo;
import com.cool.store.enums.tp.TpFormTypeEnum;
import com.cool.store.request.tp.*;
import com.cool.store.response.AuditInfoResponse;
import com.cool.store.response.ResponseResult;
import com.cool.store.service.tp.TpApplyService;
import com.cool.store.vo.tp.*;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 十二分制-申请单 前端控制器
 * </p>
 *
 * @author wangff
 * @since 2025/11/6
 */
@Api(tags = "十二分制-申请单")
@RestController
@RequestMapping("/pc/tp/apply")
@RequiredArgsConstructor
public class TpApplyController {
    private final TpApplyService tpApplyService;

    @ApiOperation("加分申请提交or保存")
    @PostMapping("/rewardApplySubmit")
    public ResponseResult<Boolean> rewardApplySubmit(@RequestBody @Validated TpApplyRequest request) {
        LoginUserInfo user = CurrentUserHolder.getUser();
        request.setSource(0);
        request.setApplyUserId(user.getUserId());
        request.setApplyUserName(user.getName());
        return ResponseResult.success(tpApplyService.rewardApplySubmit(request));
    }

    @ApiOperation("加分申请单详情")
    @GetMapping("/rewardDetail")
    @ApiImplicitParam(name = "applyId", value = "加分申请单id", required = true, dataType = "Long", paramType = "query")
    public ResponseResult<TpRewardApplyDetailVO> rewardDetail(@NotNull(message = "加分申请单id不能为空") Long applyId) {
        return ResponseResult.success(tpApplyService.rewardDetail(applyId));
    }

    @ApiOperation("加分申请单分页查询")
    @PostMapping("/rewardPage")
    public ResponseResult<PageInfo<TpRewardApplyListVO> > rewardPage(@RequestBody TpApplyQueryRequest request) {
        request.setType(TpFormTypeEnum.REWARD.getType());
        return ResponseResult.success(tpApplyService.rewardPage(request));
    }

    @ApiOperation("加分申请审批")
    @PostMapping("/rewardAudit")
    public ResponseResult<Boolean> rewardAudit(@RequestBody @Validated TpApplyAuditRequest request) {
        return ResponseResult.success(tpApplyService.rewardAudit(request));
    }

    @ApiOperation("惩处申请提交or保存")
    @PostMapping("/penaltyApplySubmit")
    public ResponseResult<Boolean> penaltyApplySubmit(@RequestBody @Validated TpApplyRequest request) {
        LoginUserInfo user = CurrentUserHolder.getUser();
        request.setSource(0);
        request.setApplyUserId(user.getUserId());
        request.setApplyUserName(user.getName());
        return ResponseResult.success(tpApplyService.penaltyApplySubmit(request));
    }

    @ApiOperation("惩处申请单详情")
    @GetMapping("/penaltyDetail")
    @ApiImplicitParam(name = "applyId", value = "惩处申请单id", required = true, dataType = "Long", paramType = "query")
    public ResponseResult<TpPenaltyApplyDetailVO> penaltyDetail(@NotNull(message = "惩处申请单id不能为空") Long applyId) {
        return ResponseResult.success(tpApplyService.penaltyDetail(applyId));
    }

    @ApiOperation("惩处申请单分页查询")
    @PostMapping("/penaltyPage")
    public ResponseResult<PageInfo<TpPenaltyApplyListVO>> penaltyPage(@RequestBody TpApplyQueryRequest request) {
        request.setIsPenalty(true);
        return ResponseResult.success(tpApplyService.penaltyPage(request));
    }

    @ApiOperation("惩处申请审批")
    @PostMapping("/penaltyAudit")
    public ResponseResult<Boolean> penaltyAudit(@RequestBody @Validated TpApplyAuditRequest request) {
        return ResponseResult.success(tpApplyService.penaltyAudit(request));
    }

    @ApiOperation("复议申请提交or保存")
    @PostMapping("/appealApplySubmit")
    public ResponseResult<Boolean> appealApplySubmit(@RequestBody @Validated TpAppealApplyRequest request) {
        LoginUserInfo user = CurrentUserHolder.getUser();
        request.setSource(0);
        request.setApplyUserId(user.getUserId());
        request.setApplyUserName(user.getName());
        return ResponseResult.success(tpApplyService.appealApplySubmit(request));
    }

    @ApiOperation("复议申请单详情")
    @GetMapping("/appealDetail")
    @ApiImplicitParam(name = "applyId", value = "复议申请单id", required = true, dataType = "Long", paramType = "query")
    public ResponseResult<TpAppealFormDetailVO> appealDetail(@NotNull(message = "复议申请单id不能为空") Long applyId) {
        return ResponseResult.success(tpApplyService.appealDetail(applyId));
    }

    @ApiOperation("复议申请单分页查询")
    @PostMapping("/appealPage")
    public ResponseResult<PageInfo<TpAppealListVO> > appealPage(@RequestBody TpApplyQueryRequest request) {
        request.setType(TpFormTypeEnum.APPEAL.getType());
        return ResponseResult.success(tpApplyService.appealPage(request));
    }

    @ApiOperation("复议申请审批")
    @PostMapping("/appealAudit")
    public ResponseResult<Boolean> appealAudit(@RequestBody @Validated TpApplyAuditRequest request) {
        return ResponseResult.success(tpApplyService.appealAudit(request));
    }

    @ApiOperation("根据申请单id批量删除")
    @PostMapping("/deleteBatch")
    public ResponseResult<Boolean> deleteBatch(@RequestBody List<Long> applyIds) {
        return ResponseResult.success(tpApplyService.deleteBatch(applyIds));
    }

    @ApiOperation("审批记录列表")
    @GetMapping("/auditRecords")
    @ApiImplicitParam(name = "applyId", value = "申请单id", required = true, dataType = "Long", paramType = "query")
    public ResponseResult<List<AuditInfoResponse>> auditRecords(@NotNull(message = "申请单id不能为空") Long applyId) {
        return ResponseResult.success(tpApplyService.getAuditRecordList(applyId));
    }

    @ApiOperation("完成缴费")
    @PostMapping("/completePayment")
    @ApiImplicitParam(name = "applyId", value = "申请单id", required = true, dataType = "Long", paramType = "query")
    public ResponseResult<Boolean> completePayment(@NotNull(message = "申请单id不能为空") Long applyId) {
        return ResponseResult.success(tpApplyService.completePayment(applyId));
    }

    @ApiOperation("惩处申请单Excel导入")
    @PostMapping("/penaltyImport")
    public ResponseResult<Boolean> penaltyImport(MultipartFile file) {
        return ResponseResult.success(tpApplyService.penaltyImport(file));
    }

    @ApiOperation("批量审批")
    @PostMapping("/batchAudit")
    public ResponseResult<List<TpBatchAuditVO>> batchAudit(@RequestBody @Validated TpBatchAuditRequest request) {
        return ResponseResult.success(tpApplyService.batchAudit(request));
    }
}
