package com.kxs.user.biz.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kxs.common.core.util.R;
import com.kxs.common.log.annotation.SysLog;
import com.kxs.system.api.model.SysPublicParam;
import com.kxs.user.api.model.KxsTotalDebt;
import com.kxs.user.api.model.KxsTotalDebtLog;
import com.kxs.user.biz.service.KxsTotalDebtLogService;
import com.kxs.user.biz.service.KxsTotalDebtService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户模块(SysTotalDebt)控制层
 *
 * @author 系统
 * @since 2024-03-22 11:05:45
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("sysTotalDebt")
public class SysTotalDebtController {

    /**
     * KXS总偿债额
     * 服务对象
     */
    private final KxsTotalDebtService kxsTotalDebtService;
    private final KxsTotalDebtLogService kxsTotalDebtLogService;



    /**
     * 账户管理-总欠款管理-列表
     *
     * @param page 分页对象
     * @param keyword 关键字
     * @return 账户管理-总欠款管理-列表
     */
    @GetMapping("/page")
    public R page(Page<KxsTotalDebt> page, @RequestParam(value = "keyword",required = false) String keyword) {

        return R.ok(kxsTotalDebtService.getByPage(page, keyword));
    }


    /**
     * 账户管理-总欠款管理-新增
     *
     * @param param 实体对象
     * @return 账户管理-总欠款管理-新增
     */
    @SysLog("总欠款管理-新增")
    @PostMapping("/add")
//    @PreAuthorize("@pms.hasPermission('sys_debt_add')")
    public R add(@RequestBody @Valid KxsTotalDebt param) {

        return kxsTotalDebtService.addData(param);
    }


    /**
     * 账户管理-总欠款管理-修改
     *
     * @param param 实体对象
     * @return 账户管理-总欠款管理-修改
     */
    @SysLog("总欠款管理-修改")
    @PostMapping("/update")
//    @PreAuthorize("@pms.hasPermission('sys_debt_edit')")
    public R update(@RequestBody @Valid KxsTotalDebt param) {

        return R.ok(kxsTotalDebtService.update(Wrappers.<KxsTotalDebt>lambdaUpdate()
                .eq(KxsTotalDebt::getId, param.getId())
                .set(KxsTotalDebt::getTotalDebt, param.getTotalDebt())
                .set(KxsTotalDebt::getRemark, param.getRemark())
                .set(KxsTotalDebt::getUpdateTime, LocalDateTime.now())));
    }


    /**
     * 账户管理-总欠款管理-删除
     *
     * @param idsList id集合
     * @return 账户管理-总欠款管理-删除
     */
    @SysLog("总欠款管理-删除")
    @PostMapping("/remove")
//    @PreAuthorize("@pms.hasPermission('sys_debt_del')")
    public R remove(@RequestBody List<Integer> idsList) {
        return R.ok(kxsTotalDebtService.removeBatchByIds(idsList));
    }


    /**
     * 账户管理-总欠款管理-欠款明细列表
     *
     * @return 账户管理-总欠款管理-欠款明细列表
     */
    @GetMapping("/pageLog")
    public R pageLog(Page<KxsTotalDebtLog> page, KxsTotalDebtLog param) {

        return R.ok(kxsTotalDebtLogService.page(page, Wrappers.<KxsTotalDebtLog>lambdaQuery().eq(KxsTotalDebtLog::getUserId, param.getUserId())));
    }


}

