package com.ruoyi.cai.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.cai.domain.AccountBusiness;
import com.ruoyi.cai.service.AccountBusinessService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 账户业务
 *
 * @author 77
 * @date 2023-12-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cai/accountBusiness")
public class AccountBusinessController extends BaseController {

    private final AccountBusinessService accountBusinessService;

    /**
     * 查询账户业务列表
     */
    @SaCheckPermission("cai:accountBusiness:list")
    @GetMapping("/list")
    public TableDataInfo<AccountBusiness> list(AccountBusiness bo, PageQuery pageQuery) {
        Page<AccountBusiness> page = accountBusinessService.page(pageQuery.build(), Wrappers.lambdaQuery(bo));
        return TableDataInfo.build(page);
    }

    /**
     * 获取账户业务详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cai:accountBusiness:query")
    @GetMapping("/{id}")
    public R<AccountBusiness> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(accountBusinessService.getById(id));
    }

    /**
     * 新增账户业务
     */
    @SaCheckPermission("cai:accountBusiness:add")
    @Log(title = "账户业务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AccountBusiness bo) {
        return toAjax(accountBusinessService.save(bo));
    }

    /**
     * 修改账户业务
     */
    @SaCheckPermission("cai:accountBusiness:edit")
    @Log(title = "账户业务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AccountBusiness bo) {
        return toAjax(accountBusinessService.updateById(bo));
    }

    /**
     * 删除账户业务
     *
     * @param ids 主键串
     */
    @SaCheckPermission("cai:accountBusiness:remove")
    @Log(title = "账户业务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(accountBusinessService.removeBatchByIds(Arrays.asList(ids), true));
    }
}
