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.FactoryPageReqVO;
import com.zt.plat.module.base.controller.admin.base.vo.FactoryRespVO;
import com.zt.plat.module.base.controller.admin.base.vo.FactorySaveReqVO;
import com.zt.plat.module.base.dal.dataobject.base.FactoryDO;
import com.zt.plat.module.base.service.base.FactoryService;
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/factory")
@Validated
public class FactoryController {


    @Resource
    private FactoryService factoryService;

    @PostMapping("/create")
    @Operation(summary = "创建工厂")
    @PreAuthorize("@ss.hasPermission('base:factory:create')")
    public CommonResult<FactoryRespVO> createFactory(@Valid @RequestBody FactorySaveReqVO createReqVO) {
        return success(factoryService.createFactory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工厂")
    @PreAuthorize("@ss.hasPermission('base:factory:update')")
    public CommonResult<Boolean> updateFactory(@Valid @RequestBody FactorySaveReqVO updateReqVO) {
        factoryService.updateFactory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工厂")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:factory:delete')")
    public CommonResult<Boolean> deleteFactory(@RequestParam("id") Long id) {
        factoryService.deleteFactory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除工厂")
                @PreAuthorize("@ss.hasPermission('base:factory:delete')")
    public CommonResult<Boolean> deleteFactoryList(@RequestBody BatchDeleteReqVO req) {
        factoryService.deleteFactoryListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工厂")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:factory:query')")
    public CommonResult<FactoryRespVO> getFactory(@RequestParam("id") Long id) {
        FactoryDO factory = factoryService.getFactory(id);
        return success(BeanUtils.toBean(factory, FactoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工厂分页")
    @PreAuthorize("@ss.hasPermission('base:factory:query')")
    public CommonResult<PageResult<FactoryRespVO>> getFactoryPage(@Valid FactoryPageReqVO pageReqVO) {
        PageResult<FactoryDO> pageResult = factoryService.getFactoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FactoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工厂 Excel")
    @PreAuthorize("@ss.hasPermission('base:factory:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFactoryExcel(@Valid FactoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FactoryDO> list = factoryService.getFactoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工厂.xls", "数据", FactoryRespVO.class,
                        BeanUtils.toBean(list, FactoryRespVO.class));
    }

    @PutMapping("/enable-list")
    @Operation(summary = "批量更新")
    @PreAuthorize("@ss.hasPermission('base:factory:update')")
    public CommonResult<Boolean> enableFactoryList(@RequestBody List<FactoryRespVO> saveReqVOS) {
        factoryService.enableFactoryList(saveReqVOS);
        return success(true);
    }
}