package com.bonus.boot.manager.basic.controller;

import cn.hutool.json.JSONUtil;
import com.bonus.boot.manager.basic.dao.SysLogDao;
import com.bonus.boot.manager.basic.entity.SysLogs;
import com.bonus.boot.manager.basic.service.SysLogService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (SysLogs)表控制层
 *
 * @author syruan
 * @since 2023-01-07 16:33:35
 */
@RestController
@RequestMapping("sysLog")
public class SysLogController {
    /**
     * 服务对象
     */
    @Resource
    private SysLogService sysLogService;

    @Resource
    private SysLogDao sysLogDao;

    /**
     * 系统操作日志分页查询
     * @param sysLogs 筛选条件对象
     * @return 查询结果
     */
    @RequestMapping(value = "/queryByPage")
    public String queryByPage(SysLogs sysLogs, @RequestParam("page") Integer page, @RequestParam("limit") Integer pageSize) {
        int count =  sysLogDao.count(sysLogs);
        page = (page - 1) * pageSize;
        List<SysLogs> list = this.sysLogDao.queryAllByLimit(sysLogs, page, pageSize);
        String data = JSONUtil.toJsonStr(list);
        return "{\"code\":0 , \"count\":" + count + " , \"data\":" + data + "}";
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<SysLogs> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.sysLogService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param sysLogs 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<SysLogs> add(SysLogs sysLogs) {
        return ResponseEntity.ok(this.sysLogService.insert(sysLogs));
    }

    /**
     * 编辑数据
     *
     * @param sysLogs 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<SysLogs> edit(SysLogs sysLogs) {
        return ResponseEntity.ok(this.sysLogService.update(sysLogs));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.sysLogService.deleteById(id));
    }

}

