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

import com.zt.plat.module.base.controller.admin.base.vo.ElementRespVO;
import com.zt.plat.module.base.controller.admin.internalsupplyfactory.vo.InternalSupplyFactoryEnableReqVO;
import com.zt.plat.module.base.controller.admin.internalsupplyfactory.vo.InternalSupplyFactoryPageReqVO;
import com.zt.plat.module.base.controller.admin.internalsupplyfactory.vo.InternalSupplyFactoryRespVO;
import com.zt.plat.module.base.controller.admin.internalsupplyfactory.vo.InternalSupplyFactorySaveReqVO;
import com.zt.plat.module.base.dal.dataobject.internalsupplyfactory.InternalSupplyFactoryDO;
import com.zt.plat.module.base.service.internalsupplyfactory.InternalSupplyFactoryService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
import com.zt.plat.framework.business.annotation.FileUploadController;
import com.zt.plat.framework.business.controller.AbstractFileUploadController;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

import com.zt.plat.framework.excel.core.util.ExcelUtils;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.*;



@Tag(name = "管理后台 - 供应链内部工厂")
@RestController
@RequestMapping("/base/internal-supply-factory")
@Validated
@FileUploadController(source = "base.internalsupplyfactory")
public class InternalSupplyFactoryController extends AbstractFileUploadController implements BusinessControllerMarker{

    static {
        FileUploadController annotation = InternalSupplyFactoryController.class.getAnnotation(FileUploadController.class);
        if (annotation != null) {
        setFileUploadInfo(annotation);
        }
    }

    @Resource
    private InternalSupplyFactoryService internalSupplyFactoryService;

    @PostMapping("/create")
    @Operation(summary = "创建供应链内部工厂")
    @PreAuthorize("@ss.hasPermission('basic:factory-config:create')")
    public CommonResult<InternalSupplyFactoryRespVO> createInternalSupplyFactory(@Valid @RequestBody InternalSupplyFactorySaveReqVO createReqVO) {
        return success(internalSupplyFactoryService.createInternalSupplyFactory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供应链内部工厂")
    @PreAuthorize("@ss.hasPermission('basic:factory-config:edit')")
    public CommonResult<Boolean> updateInternalSupplyFactory(@Valid @RequestBody InternalSupplyFactorySaveReqVO updateReqVO) {
        internalSupplyFactoryService.updateInternalSupplyFactory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除供应链内部工厂")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('basic:factory-config:delete')")
    public CommonResult<Boolean> deleteInternalSupplyFactory(@RequestParam("id") Long id) {
        internalSupplyFactoryService.deleteInternalSupplyFactory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除供应链内部工厂")
                @PreAuthorize("@ss.hasPermission('basic:factory-config:delete')")
    public CommonResult<Boolean> deleteInternalSupplyFactoryList(@RequestBody BatchDeleteReqVO req) {
        internalSupplyFactoryService.deleteInternalSupplyFactoryListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供应链内部工厂")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:internal-supply-factory:query')")
    public CommonResult<InternalSupplyFactoryRespVO> getInternalSupplyFactory(@RequestParam("id") Long id) {
        InternalSupplyFactoryDO internalSupplyFactory = internalSupplyFactoryService.getInternalSupplyFactory(id);
        return success(BeanUtils.toBean(internalSupplyFactory, InternalSupplyFactoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得供应链内部工厂分页")
    @PreAuthorize("@ss.hasPermission('base:internal-supply-factory:query')")
    public CommonResult<PageResult<InternalSupplyFactoryRespVO>> getInternalSupplyFactoryPage(@Valid InternalSupplyFactoryPageReqVO pageReqVO) {
        PageResult<InternalSupplyFactoryDO> pageResult = internalSupplyFactoryService.getInternalSupplyFactoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InternalSupplyFactoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出供应链内部工厂 Excel")
    @PreAuthorize("@ss.hasPermission('base:internal-supply-factory:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInternalSupplyFactoryExcel(@Valid InternalSupplyFactoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InternalSupplyFactoryDO> list = internalSupplyFactoryService.getInternalSupplyFactoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "供应链内部工厂.xls", "数据", InternalSupplyFactoryRespVO.class,
                        BeanUtils.toBean(list, InternalSupplyFactoryRespVO.class));
    }

    @PutMapping("/enable-list")
    @Operation(summary = "批量停用启用")
    @PreAuthorize("@ss.hasPermission('basic:metal-config:update')")
    public CommonResult<Boolean> enableElementList(@RequestBody List<InternalSupplyFactoryEnableReqVO> enableReqVOS) {
        internalSupplyFactoryService.enableInternalSupplyFactoryList(enableReqVOS);
        return success(true);
    }
}