package com.ssm.controller;


import javax.persistence.Column;
import javax.persistence.Id;

import com.github.pagehelper.PageInfo;
import com.ssm.manage.entity.BeisenUserList;
import com.ssm.manage.dto.BeisenUserListDto;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.PageResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.BeisenUserListService;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

import java.io.Serializable;

import lombok.extern.slf4j.Slf4j;
import com.ssm.core.utils.SnowflakeUtil;

import java.util.Date;
import java.util.Random;

/**
 * (BeisenUserList)表控制层
 *
 * @author makejava
 * @since 2024-01-12 15:26:22
 */
@RestController
@RequestMapping("beisenUserList")
@Slf4j
@Api(tags = "北森停用名单")
public class BeisenUserListController {
    /**
     * 服务对象
     */
    @Autowired
    private BeisenUserListService beisenUserListService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return
     @GetMapping("{id}")
     @ApiOperation("通过主键查询单条数据")
     //@RequiresPermissions("system:beisenUserList:query")
     public ResultVo get(@PathVariable Serializable id) {
     return this.beisenUserListService.queryById(id);
     }
     */


    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", required = true)
    })
//@RequiresPermissions("system:beisenUserList:query")
    public PageResponse<BeisenUserList> listPage(BeisenUserListDto dto) {
        PageResponse<BeisenUserList> pageResponse = new PageResponse<>();
        PageInfo<BeisenUserList> pageInfo = new PageInfo<>();
        try {
            pageInfo = this.beisenUserListService.selectPage(dto);
        } catch (Exception e) {
            log.error("SsmCollectionTypeController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setPageInfo(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }

    /**
     * 新增
     *
     * @param beisenUserList
     * @return
     */
    @PostMapping
    @ApiOperation("新增")
//@RequiresPermissions("system:beisenUserList:add")
    public GenericResponse add(@RequestBody BeisenUserList beisenUserList) {
        GenericResponse genericResponse = new GenericResponse();
        int i = 0;
        try {
            beisenUserList.setOriginalId(beisenUserList.getId());
            beisenUserList.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
            beisenUserList.setInputDate(new Date());
            i = this.beisenUserListService.save(beisenUserList);
        } catch (Exception e) {
            log.error("SsmCollectionTypeController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    /**
     * 编辑
     *
     * @param beisenUserList
     * @return
     */
    @PutMapping
    @ApiOperation("编辑")
//@RequiresPermissions("system:beisenUserList:edit")
    public GenericResponse edit(@RequestBody BeisenUserList beisenUserList) {
        GenericResponse genericResponse = new GenericResponse();
        int i = 0;
        try {
            i = this.beisenUserListService.updateByPrimaryKey(beisenUserList);
        } catch (Exception e) {
            log.error("SsmCollectionTypeController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    /**
     * 根据主键删除
     *
     * @param id 主键
     * @return
     */
    @DeleteMapping("{id}")
    @ApiOperation("根据主键删除")
//@RequiresPermissions("system:beisenUserList:delete")
    public GenericResponse delete(@PathVariable Long id) {
        GenericResponse genericResponse = new GenericResponse();
        int i = 0;
        try {
            BeisenUserList beisenUserList = new BeisenUserList();
            beisenUserList.setId(id);
            i = this.beisenUserListService.delete(beisenUserList);
        } catch (Exception e) {
            log.error("SsmCollectionTypeController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

}
