package com.jeesharp.business.modules.pay.paysubmerchantapplyment.controller;

import com.jeesharp.business.modules.pay.paysubmerchantapplyment.application.PaySubMerchantApplymentApplicationService;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.application.PaySubMerchantCancelApplicationService;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.application.PaySubMerchantSettlementModifyApplicationService;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.dto.PaySubMerchantApplymentSaveDTO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.dto.PaySubMerchantCancelConfirmDTO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.dto.PaySubMerchantCancelSubmitDTO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.dto.PaySubMerchantSettlementModifyDTO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantApplymentInfoVO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantApplymentListVO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantApplymentQueryVO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantApplymentSubmitResultVO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantBusinessCodeVO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantMediaUploadVO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantCancelListVO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantCancelStatusVO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantCancelSubmitVO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantCancelValidateVO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantSettlementInfoVO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantSettlementModifyListVO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantSettlementModifyStatusVO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantSettlementModifySubmitVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import com.jeesharp.framework.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 微信支付特约商户进件管理端 REST 接口。
 *
 * @author jeesharp
 * @since 2026-05-26
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/manager/business/pay/paySubMerchantApplyment/v1")
@Tag(name = "特约商户进件")
@AllArgsConstructor
public class PaySubMerchantApplymentController extends BaseController {

    private final PaySubMerchantApplymentApplicationService paySubMerchantApplymentApplicationService;
    private final PaySubMerchantSettlementModifyApplicationService paySubMerchantSettlementModifyApplicationService;
    private final PaySubMerchantCancelApplicationService paySubMerchantCancelApplicationService;

    @GetMapping("info")
    @Operation(summary = "获取详情")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:paySubMerchantApplyment:info')")
    public AjaxResult<PaySubMerchantApplymentInfoVO> info(@RequestParam("id") String id) {
        return AjaxResult.okData(paySubMerchantApplymentApplicationService.get(id));
    }

    @PostMapping("list")
    @Operation(summary = "分页列表")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:paySubMerchantApplyment:list')")
    public AjaxResult<PageRespVO<PaySubMerchantApplymentListVO>> list(@RequestBody PaySubMerchantApplymentQueryVO query) {
        return AjaxResult.okData(paySubMerchantApplymentApplicationService.findPage(new Page<>(query), query));
    }

    @GetMapping("existsByTenant")
    @Operation(summary = "进件租户是否已有进件（每租户仅一条）")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:paySubMerchantApplyment:list')")
    public AjaxResult<Boolean> existsByTenant(
            @RequestParam("applymentTenantId") String applymentTenantId) {
        return AjaxResult.okData(paySubMerchantApplymentApplicationService.existsByApplymentTenant(applymentTenantId));
    }

    @GetMapping("nextBusinessCode")
    @Operation(summary = "生成业务申请编号")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:paySubMerchantApplyment:save')")
    public AjaxResult<PaySubMerchantBusinessCodeVO> nextBusinessCode(
            @RequestParam("applymentTenantId") String applymentTenantId) {
        return AjaxResult.okData(paySubMerchantApplymentApplicationService.nextBusinessCode(applymentTenantId));
    }

    @PostMapping("save")
    @Operation(summary = "保存草稿")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:paySubMerchantApplyment:save')")
    public AjaxResult<Void> save(@RequestBody @Valid PaySubMerchantApplymentSaveDTO dto) {
        paySubMerchantApplymentApplicationService.save(dto);
        return AjaxResult.ok("保存成功");
    }

    @GetMapping("delete")
    @Operation(summary = "删除")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:paySubMerchantApplyment:delete')")
    public AjaxResult<Void> delete(@RequestParam("ids") String ids) {
        paySubMerchantApplymentApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }

    @PostMapping("submit")
    @Operation(summary = "提交申请单到微信")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:paySubMerchantApplyment:submit')")
    public AjaxResult<PaySubMerchantApplymentSubmitResultVO> submit(@RequestParam("id") String id) throws Exception {
        return AjaxResult.okData(paySubMerchantApplymentApplicationService.submit(id));
    }

    @GetMapping("syncStatus")
    @Operation(summary = "同步微信审核状态")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:paySubMerchantApplyment:syncStatus')")
    public AjaxResult<PaySubMerchantApplymentSubmitResultVO> syncStatus(@RequestParam("id") String id) throws Exception {
        return AjaxResult.okData(paySubMerchantApplymentApplicationService.syncStatus(id));
    }

    @PostMapping("uploadMedia")
    @Operation(summary = "上传进件图片资料")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:paySubMerchantApplyment:uploadMedia')")
    public AjaxResult<PaySubMerchantMediaUploadVO> uploadMedia(
            @RequestParam("file") MultipartFile file,
            @RequestParam("applymentTenantId") String applymentTenantId) throws Exception {
        return AjaxResult.okData(paySubMerchantApplymentApplicationService.uploadMedia(file, applymentTenantId));
    }

    @GetMapping("settlement/current")
    @Operation(summary = "查询微信当前结算账户")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:paySubMerchantApplyment:settlementCurrent')")
    public AjaxResult<PaySubMerchantSettlementInfoVO> settlementCurrent(@RequestParam("applymentId") String applymentId)
            throws Exception {
        return AjaxResult.okData(paySubMerchantSettlementModifyApplicationService.getCurrentSettlement(applymentId));
    }

    @PostMapping("settlement/modify")
    @Operation(summary = "提交修改结算账户")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:paySubMerchantApplyment:settlementModify')")
    public AjaxResult<PaySubMerchantSettlementModifySubmitVO> settlementModify(
            @RequestBody @Valid PaySubMerchantSettlementModifyDTO dto) throws Exception {
        return AjaxResult.okData(paySubMerchantSettlementModifyApplicationService.submitModify(dto));
    }

    @GetMapping("settlement/modify/syncStatus")
    @Operation(summary = "同步修改结算账户审核状态")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:paySubMerchantApplyment:settlementSync')")
    public AjaxResult<PaySubMerchantSettlementModifyStatusVO> settlementModifySyncStatus(
            @RequestParam("modifyId") String modifyId) throws Exception {
        return AjaxResult.okData(paySubMerchantSettlementModifyApplicationService.syncModifyStatus(modifyId));
    }

    @GetMapping("settlement/modify/list")
    @Operation(summary = "修改结算账户历史记录")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:paySubMerchantApplyment:settlementList')")
    public AjaxResult<java.util.List<PaySubMerchantSettlementModifyListVO>> settlementModifyList(
            @RequestParam("applymentId") String applymentId) {
        return AjaxResult.okData(paySubMerchantSettlementModifyApplicationService.listModifyRecords(applymentId));
    }

    @GetMapping("cancel/validate")
    @Operation(summary = "商户注销资格校验")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:paySubMerchantApplyment:cancelValidate')")
    public AjaxResult<PaySubMerchantCancelValidateVO> cancelValidate(@RequestParam("applymentId") String applymentId)
            throws Exception {
        return AjaxResult.okData(paySubMerchantCancelApplicationService.validateCancel(applymentId));
    }

    @PostMapping("cancel/submit")
    @Operation(summary = "提交商户注销申请")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:paySubMerchantApplyment:cancelSubmit')")
    public AjaxResult<PaySubMerchantCancelSubmitVO> cancelSubmit(@RequestBody @Valid PaySubMerchantCancelSubmitDTO dto)
            throws Exception {
        return AjaxResult.okData(paySubMerchantCancelApplicationService.submitCancel(dto));
    }

    @GetMapping("cancel/syncStatus")
    @Operation(summary = "同步商户注销状态")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:paySubMerchantApplyment:cancelSync')")
    public AjaxResult<PaySubMerchantCancelStatusVO> cancelSyncStatus(@RequestParam("cancelId") String cancelId)
            throws Exception {
        return AjaxResult.okData(paySubMerchantCancelApplicationService.syncCancelStatus(cancelId));
    }

    @PostMapping("cancel/confirm")
    @Operation(summary = "平台代商户确认注销")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:paySubMerchantApplyment:cancelConfirm')")
    public AjaxResult<PaySubMerchantCancelStatusVO> cancelConfirm(@RequestBody @Valid PaySubMerchantCancelConfirmDTO dto)
            throws Exception {
        return AjaxResult.okData(paySubMerchantCancelApplicationService.confirmCancel(dto));
    }

    @GetMapping("cancel/list")
    @Operation(summary = "商户注销历史记录")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:paySubMerchantApplyment:cancelList')")
    public AjaxResult<java.util.List<PaySubMerchantCancelListVO>> cancelList(
            @RequestParam("applymentId") String applymentId) {
        return AjaxResult.okData(paySubMerchantCancelApplicationService.listCancelRecords(applymentId));
    }
}
