package com.propertyManagement.web.controller.business.api;

import com.propertyManagement.business.domain.bo.WxPayRecordBo;
import com.propertyManagement.business.domain.vo.WxPayRecordVo;
import com.propertyManagement.business.service.IWxPayRecordService;
import com.propertyManagement.business.support.util.AuthUtil;
import com.propertyManagement.common.annotation.Anonymous;
import com.propertyManagement.common.annotation.RepeatSubmit;
import com.propertyManagement.common.core.controller.BaseController;
import com.propertyManagement.common.core.domain.R;
import com.propertyManagement.common.core.validate.AddGroup;
import com.propertyManagement.common.enums.PayState;
import com.propertyManagement.pay.support.pay.PayReqModel;
import com.siashan.unipay4j.core.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 缴费记录
 *
 * @author liushuai
 * @date 2025-05-14
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/payRecord")
public class ApiWxPayRecordController extends BaseController {

    private final IWxPayRecordService iWxPayRecordService;

    /**
     * 查询缴费记录列表
     */
    @GetMapping("/listAll")
    public R<List<WxPayRecordVo>> listAll(@NotNull(message = "类型不能为空") Integer type) {
        WxPayRecordBo recordBo = new WxPayRecordBo();
        recordBo.setProprietorId(AuthUtil.getWxUser().getProprietorId());
        recordBo.setOrderType(type);
//        recordBo.setPayState(PayState.NO.getCode());
        return R.ok(iWxPayRecordService.queryList(recordBo));
    }

    /**
     * 支付订单
     */
    @RepeatSubmit()
    @PostMapping("/pay")
    public R<Result<Object>> add(@Validated(AddGroup.class) @RequestBody PayReqModel payReqModel) {
        return R.ok(iWxPayRecordService.pay(payReqModel));
    }

    /**
     * 维修单支付回调
     *
     * @return  结果
     */
    @Anonymous
    @PostMapping("/maintain-notify/{orderId}/{payChannelCode}/{payMchCode}/{payAppCode}")
    public void maintainCallback(HttpServletRequest request, HttpServletResponse response,
                         @PathVariable String orderId,
                         @PathVariable String payChannelCode,
                         @PathVariable String payMchCode,
                         @PathVariable String payAppCode) {
        log.info("===================>维修单支付回调成功");
        iWxPayRecordService.payCallback(orderId, payChannelCode, payMchCode, payAppCode ,"MAINTAIN");
    }

    /**
     * 预存支付回调
     *
     * @return  结果
     */
    @Anonymous
    @PostMapping("/prestore-notify/{orderId}/{payChannelCode}/{payMchCode}/{payAppCode}")
    public void callback(HttpServletRequest request, HttpServletResponse response,
                         @PathVariable String orderId,
                         @PathVariable String payChannelCode,
                         @PathVariable String payMchCode,
                         @PathVariable String payAppCode) {
        log.info("===================>预存支付回调成功");
        iWxPayRecordService.payCallback(orderId, payChannelCode, payMchCode, payAppCode, "PRESTORE");
    }

}
