package com.wzj.soopin.transaction.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzj.soopin.transaction.convert.DivideRuleConvert;
import com.wzj.soopin.transaction.domain.bo.DivideRuleBO;
import com.wzj.soopin.transaction.domain.po.DivideRule;
import com.wzj.soopin.transaction.domain.vo.DivideRuleVO;
import com.wzj.soopin.transaction.service.IDivideRuleService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.springframework.web.bind.annotation.*;

/**
 * 用户封禁
 */
@Tag(name = "分账")
@RestController
@RequestMapping("/trans/divide/rule")
@RequiredArgsConstructor
public class DivideRuleController {

    private final IDivideRuleService service;
    private final DivideRuleConvert convert;

    @Tag(name = "查询列表")
    @PostMapping("/list")
    public R<IPage<DivideRuleVO>> list(@RequestBody DivideRuleBO bo, @RequestBody Page page) {
        Page<DivideRule> pages = service.page(page, bo.toWrapper());
        return R.ok(convert.toVO(pages));
    }

    @Tag(name = "新增")
    @Log(title = "新增 ", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R add(@RequestBody DivideRuleBO bo) {
        service.save(bo);
        return R.ok();
    }

    @Tag(name = "详情")
    @GetMapping(value = "/{id}")
    public R<DivideRuleVO> getInfo(@PathVariable("id") Long id) {
        return R.ok(service.getVOById(id));
    }

    @Tag(name = ("修改"))
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public R update(@RequestBody DivideRuleBO bo) {
        service.update(bo);
        return R.ok();
    }

    /**
     * 启/停用规则，status=1启用，status=0停用
     */
    @Tag(name = ("启用停用"))
    @Log(title = "启用停用", businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus/{id}/{status}")
    public R changeStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        service.changeStatus(id, status);
        return R.ok();
    }


    @Tag(name = "删除")
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Boolean> remove(@PathVariable Long id) {
        return R.ok(service.removeById(id));
    }
}
