package com.ktg.iscs.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.pojo.CommonResult;
import com.ktg.common.utils.poi.ExcelUtil;
import com.ktg.iscs.domain.IsSystemAttribute;
import com.ktg.iscs.service.IIsSystemAttributeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 基础数据Controller
 *
 * @author cgj
 * @date 2025-03-11
 */
@Api(tags = "基础数据")
@RestController
@RequestMapping("/iscs/attribute")
public class IsSystemAttributeController extends BaseController {
    @Autowired
    private IIsSystemAttributeService isSystemAttributeService;

    @ApiOperation("查询基础数据-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "isSystemAttribute", description = "实体参数")
    })
    @PreAuthorize("@ss.hasPermi('iscs:attribute:list')")
    @GetMapping("/getIsSystemAttributePage")
    public CommonResult<Page<IsSystemAttribute>> getIsSystemAttributePage(Page<IsSystemAttribute> page, IsSystemAttribute isSystemAttribute) {
        Page<IsSystemAttribute> result = isSystemAttributeService.getIsSystemAttributePage(page, isSystemAttribute);
        return CommonResult.success(result);
    }

    @ApiOperation("导出基础数据列表")
    @Parameter(name = "isSystemAttribute", description = "实体参数")
    @PreAuthorize("@ss.hasPermi('iscs:attribute:export')")
    @Log(title = "基础数据", businessType = BusinessType.EXPORT)
    @PostMapping("/exportIsSystemAttribute")
    public void exportIsSystemAttribute(HttpServletResponse response, IsSystemAttribute isSystemAttribute) {
        Page<IsSystemAttribute> page = new Page<>();
        page.setSize(-1);
        page.setCurrent(1);
        List<IsSystemAttribute> list = isSystemAttributeService.page(page, Wrappers.<IsSystemAttribute>lambdaQuery()
                .orderByDesc(IsSystemAttribute::getSysAttrId)).getRecords();
        ExcelUtil<IsSystemAttribute> util = new ExcelUtil<IsSystemAttribute>(IsSystemAttribute.class);
        util.exportExcel(response, list, "基础数据数据");
    }

    @ApiOperation("获取基础数据详细信息")
    @Parameter(name = "id", description = "id")
    @PreAuthorize("@ss.hasPermi('iscs:attribute:query')")
    @GetMapping(value = "/selectIsSystemAttributeById")
    public CommonResult<IsSystemAttribute> selectIsSystemAttributeById(Long id) {
        return CommonResult.success(isSystemAttributeService.getById(id));
    }

    @ApiOperation("新增基础数据")
    @PreAuthorize("@ss.hasPermi('iscs:attribute:add')")
    @Log(title = "基础数据", businessType = BusinessType.INSERT)
    @PostMapping("/insertIsSystemAttribute")
    public CommonResult<Boolean> insertIsSystemAttribute(@RequestBody @Parameter(name = "isSystemAttribute", description = "新增数据类,放到body") IsSystemAttribute isSystemAttribute) {
        return CommonResult.success(isSystemAttributeService.insertIsSystemAttribute(isSystemAttribute));
    }

    @ApiOperation("修改基础数据")
    @PreAuthorize("@ss.hasPermi('iscs:attribute:edit')")
    @Log(title = "基础数据", businessType = BusinessType.UPDATE)
    @PostMapping("/updateIsSystemAttribute")
    public CommonResult<Boolean> updateIsSystemAttribute(@RequestBody @Parameter(name = "isSystemAttribute", description = "修改数据类,放到body") IsSystemAttribute isSystemAttribute) {
        return CommonResult.success(isSystemAttributeService.updateIsSystemAttribute(isSystemAttribute));
    }

    @ApiOperation("删除基础数据")
    @PreAuthorize("@ss.hasPermi('iscs:attribute:remove')")
    @Log(title = "基础数据", businessType = BusinessType.DELETE)
    @PostMapping("/deleteIsSystemAttributeByIds")
    public CommonResult<Boolean> deleteIsSystemAttributeByIds(String ids) {
        return CommonResult.success(isSystemAttributeService.deleteIsSystemAttributeByIds(ids));
    }

    @ApiOperation("通过sysAttrKey查询数据")
    @Parameter(name = "sysAttrKey", description = "参数键名")
    @GetMapping("/getIsSystemAttributeByKey")
    public CommonResult<IsSystemAttribute> getIsSystemAttributeByKey(String sysAttrKey) {
        IsSystemAttribute result = isSystemAttributeService.getIsSystemAttributeByKey(sysAttrKey);
        return CommonResult.success(result);
    }

    @ApiOperation("刷新基础数据")
    @Log(title = "刷新基础数据", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshAttrCache")
    public CommonResult<Boolean> refreshAttrCache()
    {
        return CommonResult.success(isSystemAttributeService.refreshAttrCache());
    }

}
