package com.zt.plat.module.base.controller.admin.base;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.excel.core.util.ExcelUtils;
import com.zt.plat.module.base.controller.admin.base.vo.ElementPageReqVO;
import com.zt.plat.module.base.controller.admin.base.vo.ElementRespVO;
import com.zt.plat.module.base.controller.admin.base.vo.ElementSaveReqVO;
import com.zt.plat.module.base.dal.dataobject.base.ElementDO;
import com.zt.plat.module.base.service.base.ElementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 金属元素")
@RestController
@RequestMapping("/base/element")
@Validated
public class ElementController {


    @Resource
    private ElementService elementService;

    @PostMapping("/create")
    @Operation(summary = "创建金属元素")
    @PreAuthorize("@ss.hasPermission('basic:metal-config:create')")
    public CommonResult<ElementRespVO> createElement(@Valid @RequestBody ElementSaveReqVO createReqVO) {
        return success(elementService.createElement(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新金属元素")
    @PreAuthorize("@ss.hasPermission('basic:metal-config:update')")
    public CommonResult<Boolean> updateElement(@Valid @RequestBody ElementSaveReqVO updateReqVO) {
        elementService.updateElement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除金属元素")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('basic:metal-config:delete')")
    public CommonResult<Boolean> deleteElement(@RequestParam("id") Long id) {
        elementService.deleteElement(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除金属元素")
                @PreAuthorize("@ss.hasPermission('basic:metal-config:delete')")
    public CommonResult<Boolean> deleteElementList(@RequestBody BatchDeleteReqVO req) {
        elementService.deleteElementListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得金属元素")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:element:query')")
    public CommonResult<ElementRespVO> getElement(@RequestParam("id") Long id) {
        ElementDO element = elementService.getElement(id);
        return success(BeanUtils.toBean(element, ElementRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得金属元素分页")
    @PreAuthorize("@ss.hasPermission('base:element:query')")
    public CommonResult<PageResult<ElementRespVO>> getElementPage(@Valid ElementPageReqVO pageReqVO) {
        PageResult<ElementDO> pageResult = elementService.getElementPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ElementRespVO.class));
    }

    @GetMapping("/pageByEnable")
    @Operation(summary = "获得启用的金属元素分页")
    @PreAuthorize("@ss.hasPermission('base:element:query')")
    public CommonResult<PageResult<ElementRespVO>> getElementPageByEnable(@Valid ElementPageReqVO pageReqVO) {
        PageResult<ElementDO> pageResult = elementService.getElementPageByEnable(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ElementRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出金属元素 Excel")
    @PreAuthorize("@ss.hasPermission('basic:metal-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportElementExcel(@Valid ElementPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ElementDO> list = elementService.getElementPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "金属元素.xls", "数据", ElementRespVO.class,
                        BeanUtils.toBean(list, ElementRespVO.class));
    }

    @PutMapping("/enable-list")
    @Operation(summary = "批量更新")
    @PreAuthorize("@ss.hasPermission('basic:metal-config:update')")
    public CommonResult<Boolean> enableElementList(@RequestBody List<ElementRespVO> saveReqVOS) {
        elementService.enableElementList(saveReqVOS);
        return success(true);
    }
}