package com.czg.controller.admin;

import com.czg.log.annotation.OperationLog;
import com.czg.order.entity.CreditBuyer;
import com.czg.order.param.CreditBuyerQueryParam;
import com.czg.order.param.CreditBuyerRepaymentParam;
import com.czg.order.service.CreditBuyerService;
import com.czg.order.vo.CreditBuyerRepaymentVo;
import com.czg.resp.CzgResult;
import com.czg.utils.AssertUtil;
import com.mybatisflex.core.paginate.Page;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 挂账人
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-03-04
 */
@AllArgsConstructor
@RestController
@RequestMapping("/admin/order/credit/buyer")
public class CreditBuyerController {
    private final CreditBuyerService creditBuyerService;

    /**
     * 分页
     */
    @GetMapping("page")
    @OperationLog("挂账人-分页")
    //@SaAdminCheckPermission("creditBuyer:page")
    public CzgResult<Page<CreditBuyer>> getCreditBuyerPage(CreditBuyerQueryParam param) {
        Page<CreditBuyer> data = creditBuyerService.getCreditBuyerPage(param);
        return CzgResult.success(data);
    }

    /**
     * 详情
     * param id 挂账人id
     */
    @GetMapping("{id}")
    @OperationLog("挂账人-详情")
    //@SaAdminCheckPermission("creditBuyer:info")
    public CzgResult<CreditBuyer> getCreditBuyerById(@PathVariable("id") String id) {
        AssertUtil.isNull(id, "{}不能为空", "id");
        return CzgResult.success(creditBuyerService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping
    @OperationLog("挂账人-新增")
    //@SaAdminCheckPermission("creditBuyer:add")
    public CzgResult<Void> addCreditBuyer(@RequestBody CreditBuyer dto) {
        creditBuyerService.addCreditBuyer(dto);
        return CzgResult.success();
    }

    /**
     * 修改
     */
    @PutMapping
    @OperationLog("挂账人-修改")
    //@SaAdminCheckPermission("creditBuyer:update")
    public CzgResult<Void> updateCreditBuyer(@RequestBody CreditBuyer dto) {
        creditBuyerService.updateCreditBuyer(dto);
        return CzgResult.success();
    }

    /**
     * 删除
     *
     * @param id 挂账人id
     */
    @DeleteMapping("{id}")
    @OperationLog("挂账人-删除")
    //@SaAdminCheckPermission("creditBuyer:delete")
    public CzgResult<Void> deleteCreditBuyer(@PathVariable("id") String id) {
        //效验数据
        AssertUtil.isNull(id, "{}不能为空", "id");
        creditBuyerService.deleteCreditBuyer(id);
        return CzgResult.success();
    }

    /**
     * 还款
     */
    @PostMapping("repayment")
    @OperationLog("挂账人-还款")
    //@SaAdminCheckPermission("creditBuyer:repayment")
    public CzgResult<CreditBuyerRepaymentVo> repayment(@RequestBody CreditBuyerRepaymentParam param) {
        CreditBuyerRepaymentVo data = creditBuyerService.repayment(param);
        return CzgResult.success(data);
    }

}