package shop.alien.lawyer.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LawyerPaymentTransaction;
import shop.alien.lawyer.service.LawyerPaymentTransactionService;
import shop.alien.util.myBaticsPlus.QueryBuilder;

import java.util.List;

/**
 * 支付交易 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"律师平台-支付交易"})
@ApiSort(20)
@CrossOrigin
@RestController
@RequestMapping("/lawyer/paymentTransaction")
@RequiredArgsConstructor
public class LawyerPaymentTransactionController {

    private final LawyerPaymentTransactionService paymentTransactionService;

    @ApiOperation("新增支付交易")
    @ApiOperationSupport(order = 1)
    @PostMapping("/addPaymentTransaction")
    public R<LawyerPaymentTransaction> addPaymentTransaction(@RequestBody LawyerPaymentTransaction paymentTransaction) {
        log.info("LawyerPaymentTransactionController.addPaymentTransaction?paymentTransaction={}", paymentTransaction);
        return paymentTransactionService.addPaymentTransaction(paymentTransaction);
    }

    @ApiOperation("编辑支付交易")
    @ApiOperationSupport(order = 2)
    @PostMapping("/editPaymentTransaction")
    public R<LawyerPaymentTransaction> editPaymentTransaction(@RequestBody LawyerPaymentTransaction paymentTransaction) {
        log.info("LawyerPaymentTransactionController.editPaymentTransaction?paymentTransaction={}", paymentTransaction);
        return paymentTransactionService.editPaymentTransaction(paymentTransaction);
    }

    @ApiOperation("删除支付交易")
    @ApiOperationSupport(order = 3)
    @DeleteMapping("/deletePaymentTransaction")
    public R<Boolean> deletePaymentTransaction(@RequestParam(value = "id") Integer id) {
        log.info("LawyerPaymentTransactionController.deletePaymentTransaction?id={}", id);
        return paymentTransactionService.deletePaymentTransaction(id);
    }

    @ApiOperation("保存或更新支付交易")
    @ApiOperationSupport(order = 4)
    @PostMapping("/saveOrUpdate")
    public R<LawyerPaymentTransaction> saveOrUpdate(@RequestBody LawyerPaymentTransaction paymentTransaction) {
        log.info("LawyerPaymentTransactionController.saveOrUpdate?paymentTransaction={}", paymentTransaction);
        boolean result = paymentTransactionService.saveOrUpdate(paymentTransaction);
        if (result) {
            return R.data(paymentTransaction);
        }
        return R.fail("操作失败");
    }

    @ApiOperation("通用列表查询")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "consultationOrderId", value = "咨询订单ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "transactionStatus", value = "交易状态", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "createdTime_Start", value = "创建时间开始（范围查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createdTime_End", value = "创建时间结束（范围查询）", dataType = "String", paramType = "query")
    })
    @GetMapping("/getList")
    public R<List<LawyerPaymentTransaction>> getList(@ModelAttribute LawyerPaymentTransaction paymentTransaction) {
        log.info("LawyerPaymentTransactionController.getList?paymentTransaction={}", paymentTransaction);
        List<LawyerPaymentTransaction> list = QueryBuilder.of(paymentTransaction)
                .build()
                .list(paymentTransactionService);
        return R.data(list);
    }

    @ApiOperation("通用分页查询")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数（默认1）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页容（默认10）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "consultationOrderId", value = "咨询订单ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "transactionStatus", value = "交易状态", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "createdTime_Start", value = "创建时间开始（范围查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createdTime_End", value = "创建时间结束（范围查询）", dataType = "String", paramType = "query")
    })
    @GetMapping("/getPage")
    public R<IPage<LawyerPaymentTransaction>> getPage(@ModelAttribute LawyerPaymentTransaction paymentTransaction,
                                                      @RequestParam(defaultValue = "1") int page,
                                                      @RequestParam(defaultValue = "10") int size) {
        log.info("LawyerPaymentTransactionController.getPage?paymentTransaction={},page={},size={}", paymentTransaction, page, size);
        int pageNum = page > 0 ? page : 1;
        int pageSize = size > 0 ? size : 10;
        IPage<LawyerPaymentTransaction> pageResult = QueryBuilder.of(paymentTransaction)
                .page(pageNum, pageSize)
                .build()
                .page(paymentTransactionService);
        return R.data(pageResult);
    }
}

