package com.xjrsoft.module.ledger.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.db.Entity;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.mybatis.SqlRunnerAdapter;
import com.xjrsoft.module.ledger.dto.LedgerCustomDeleteDto;
import com.xjrsoft.module.ledger.dto.LedgerCustomUpdateDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
* @title: 台账自定义接口
* @Author dzx
* @Date: 2024年9月26日
* @Version 1.0
*/
@RestController
@RequestMapping("/ledger" + "/ledgerCustom")
@Api(value = "/ledger"  + "/ledgerCustom",tags = "台账自定义处理接口")
@AllArgsConstructor
public class LedgerCustomController {


    @PutMapping
    @ApiOperation(value = "修改状态(仅限于表中有enabled_mark)")
    @SaCheckPermission("ledgerconfigrelease:edit")
    public RT<Boolean> update(@Valid @RequestBody LedgerCustomUpdateDto dto){
        Entity params = Entity.create(dto.getTableName());
        params.set("enabled_mark", dto.getEnabledMark());

        Entity where = Entity.create(dto.getTableName());
        where.set("id", dto.getId());

        return RT.ok(SqlRunnerAdapter.db().dynamicUpdate(dto.getTableName(), params, where));
    }

    @DeleteMapping
    @ApiOperation(value = "删除数据(仅限于表中有delete_mark)")
    @SaCheckPermission("ledgerconfigrelease:delete")
    public RT<Boolean> delete(@Valid @RequestBody LedgerCustomDeleteDto dto){
        Entity where = Entity.create(dto.getTableName());
        where.set("id", dto.getId());
        return RT.ok(SqlRunnerAdapter.db().dynamicDelete(dto.getTableName(), where));
    }

}