package com.ssm.controller;

import com.ssm.filter.LoginInterceptor;
import com.ssm.manage.dto.RegionListDto;
import com.ssm.manage.entity.RegionList;
import com.ssm.manage.query.RegionListVo;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.service.RegionListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (RegionList)表控制层
 *
 * @author makejava
 * @since 2023-12-16 11:26:08
 */
@RestController
@RequestMapping("regionList")
@Slf4j
@Api(tags = "区域")
public class RegionListController {
    /**
     * 服务对象
     */
    @Autowired
    private RegionListService regionListService;

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


    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @GetMapping("/get")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", required = true)
    })
//@RequiresPermissions("system:regionList:query")
    public GenericResponse listPage(RegionListDto dto) {
        GenericResponse pageResponse = new GenericResponse<>();
        List<RegionListVo> list = new ArrayList<>();
        try {
            list = this.regionListService.selectPage(dto);
        } catch (Exception e) {
            log.error("SsmCollectionTypeController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setData(list);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }

    /**
     * 新增
     *
     * @param regionList
     * @return
     */
    @PostMapping
    @ApiOperation("新增")
//@RequiresPermissions("system:regionList:add")
    public GenericResponse add(@RequestBody RegionList regionList) {
        GenericResponse genericResponse = new GenericResponse();
        StaffResponse staffResponse = LoginInterceptor.getCurrentUserInfo();
        LoginInterceptor.clearThreadLocal();
        int i = 0;
        try {
            regionList.setInputId(staffResponse.getId());
            regionList.setInputName(staffResponse.getStaffName());
            regionList.setInputDate(new Date());
            i = this.regionListService.save(regionList);
        } 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 regionList
     * @return
     */
    @PutMapping
    @ApiOperation("编辑")
//@RequiresPermissions("system:regionList:edit")
    public GenericResponse edit(@RequestBody RegionList regionList) {
        GenericResponse genericResponse = new GenericResponse();
        int i = 0;
        try {
            StaffResponse userInfo = LoginInterceptor.getCurrentUserInfo();
            LoginInterceptor.clearThreadLocal();
            regionList.setUpdateId(userInfo.getId());
            regionList.setUpdateName(userInfo.getStaffName());
            regionList.setUpdateDate(new Date());
            i = this.regionListService.updateByPrimaryKey(regionList);
        } 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:regionList:delete")
    public GenericResponse delete(@PathVariable Integer id) {
        GenericResponse genericResponse = new GenericResponse();
        int i = 0;
        try {
            RegionList regionList = new RegionList();
            regionList.setId(id);
            i = this.regionListService.delete(regionList);
        } 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;
    }

}
