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

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
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.erp.controller.admin.erp.vo.WarehouseFactoryPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.WarehouseFactoryRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.WarehouseFactorySaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.WarehouseFactoryDO;
import com.zt.plat.module.erp.service.erp.WarehouseFactoryService;
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/warehouse-factory")
@Validated
public class WarehouseFactoryController implements BusinessControllerMarker {


    @Resource
    private WarehouseFactoryService warehouseFactoryService;

    @PostMapping("/create")
    @Operation(summary = "创建仓库工厂关系")
    @PreAuthorize("@ss.hasAnyPermissions({'base:warehouse-factory:create','basic:warehouse-config:create'})")
    public CommonResult<WarehouseFactoryRespVO> createWarehouseFactory(@Valid @RequestBody WarehouseFactorySaveReqVO createReqVO) {
        return success(warehouseFactoryService.createWarehouseFactory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新仓库工厂关系")
    @PreAuthorize("@ss.hasAnyPermissions({'base:warehouse-factory:update','basic:warehouse-config:update'})")
    public CommonResult<Boolean> updateWarehouseFactory(@Valid @RequestBody WarehouseFactorySaveReqVO updateReqVO) {
        warehouseFactoryService.updateWarehouseFactory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除仓库工厂关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasAnyPermissions({'base:warehouse-factory:delete','basic:warehouse-config:delete'})")
    public CommonResult<Boolean> deleteWarehouseFactory(@RequestParam("id") Long id) {
        warehouseFactoryService.deleteWarehouseFactory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除仓库工厂关系")
    @PreAuthorize("@ss.hasAnyPermissions({'base:warehouse-factory:delete','basic:warehouse-config:delete'})")
    public CommonResult<Boolean> deleteWarehouseFactoryList(@RequestBody BatchDeleteReqVO req) {
        warehouseFactoryService.deleteWarehouseFactoryListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得仓库工厂关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasAnyPermissions({'base:warehouse-factory:query','basic:warehouse-config:query'})")
    public CommonResult<WarehouseFactoryRespVO> getWarehouseFactory(@RequestParam("id") Long id) {
        WarehouseFactoryDO warehouseFactory = warehouseFactoryService.getWarehouseFactory(id);
        return success(BeanUtils.toBean(warehouseFactory, WarehouseFactoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得仓库工厂关系分页")
    @PreAuthorize("@ss.hasAnyPermissions({'base:warehouse-factory:query','basic:warehouse-config:query'})")
    public CommonResult<PageResult<WarehouseFactoryRespVO>> getWarehouseFactoryPage(@Valid WarehouseFactoryPageReqVO pageReqVO) {
        PageResult<WarehouseFactoryDO> pageResult = warehouseFactoryService.getWarehouseFactoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WarehouseFactoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出仓库工厂关系 Excel")
    @PreAuthorize("@ss.hasAnyPermissions({'base:warehouse-factory:export','basic:warehouse-config:export'})")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWarehouseFactoryExcel(@Valid WarehouseFactoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WarehouseFactoryDO> list = warehouseFactoryService.getWarehouseFactoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "仓库工厂关系.xls", "数据", WarehouseFactoryRespVO.class,
                        BeanUtils.toBean(list, WarehouseFactoryRespVO.class));
    }

    //通过主库位Id查询库位
    @GetMapping("/warehouse-factory-list-mainId")
    @Operation(summary = "通过主库位Id查询库位")
    @PreAuthorize("@ss.hasAnyPermissions({'base:warehouse-factory:query','basic:warehouse-config:query'})")
    public CommonResult<List<WarehouseFactoryRespVO>> getWarehouseFactoryListByMainId(@RequestParam("mainId") String mainId) {
        return success(warehouseFactoryService.getWarehouseFactoryByMainId(mainId));
    }

}