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

import cn.hutool.json.JSONUtil;
import com.bonus.boot.manager.basic.dao.AppLoginAuditDao;
import com.bonus.boot.manager.basic.entity.AppLoginAudit;
import com.bonus.boot.manager.basic.service.AppLoginAuditService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

/**
 * APP登录记录表(AppLoginAudit)表控制层
 *
 * @author sy ruan
 * @since 2022-12-12 13:57:35
 */
@RestController
@RequestMapping("appLoginAudit")
public class AppLoginAuditController {
    /**
     * 服务对象
     */
    @Resource
    private AppLoginAuditService appLoginAuditService;

    @Resource
    private AppLoginAuditDao appLoginAuditDao;

    /**
     * 分页查询
     */
    @RequestMapping(value = "/queryByPage")
    public String queryByPage(AppLoginAudit appLoginAudit, String keyWord, @RequestParam("page")Integer page, @RequestParam("limit")Integer pageSize) {
        if (keyWord != null){
            appLoginAudit.setTelepNumber(keyWord);
        }
        int count = appLoginAuditDao.count(appLoginAudit);
        page = (page - 1) * pageSize;
        List<AppLoginAudit> list = this.appLoginAuditService.queryByPage(appLoginAudit,page,pageSize);
        String data = JSONUtil.toJsonStr(list);
        return "{\"code\":0 , \"count\":" + count + " , \"data\":" + data + "}";
    }


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

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

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

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping(value = "/deleteById")
    public String deleteById(Integer id) {
        boolean result = this.appLoginAuditService.deleteById(id);
        if (result){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }

}

