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

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.erp.controller.admin.erp.vo.ErpWarehousePageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpWarehouseRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpWarehouseSaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpWarehouseDO;
import com.zt.plat.module.erp.service.erp.ErpWarehouseService;
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 = "管理后台 - ERP库位")
@RestController
@RequestMapping("/base/erp-warehouse")
@Validated
public class ErpWarehouseController {


    @Resource
    private ErpWarehouseService erpWarehouseService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP库位")
    @PreAuthorize("@ss.hasPermission('sply:erp-warehouse:create')")
    public CommonResult<ErpWarehouseRespVO> createErpWarehouse(@Valid @RequestBody ErpWarehouseSaveReqVO createReqVO) {
        return success(erpWarehouseService.createErpWarehouse(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP库位")
    @PreAuthorize("@ss.hasPermission('sply:erp-warehouse:update')")
    public CommonResult<Boolean> updateErpWarehouse(@Valid @RequestBody ErpWarehouseSaveReqVO updateReqVO) {
        erpWarehouseService.updateErpWarehouse(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP库位")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sply:erp-warehouse:delete')")
    public CommonResult<Boolean> deleteErpWarehouse(@RequestParam("id") Long id) {
        erpWarehouseService.deleteErpWarehouse(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除ERP库位")
    @PreAuthorize("@ss.hasPermission('sply:erp-warehouse:delete')")
    public CommonResult<Boolean> deleteErpWarehouseList(@RequestBody BatchDeleteReqVO req) {
        erpWarehouseService.deleteErpWarehouseListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP库位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sply:erp-warehouse:query')")
    public CommonResult<ErpWarehouseRespVO> getErpWarehouse(@RequestParam("id") Long id) {
        ErpWarehouseRespVO erpWarehouse = erpWarehouseService.getErpWarehouse(id);
        return success(erpWarehouse);
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP库位分页")
    @PreAuthorize("@ss.hasPermission('sply:erp-warehouse:query')")
    public CommonResult<PageResult<ErpWarehouseDO> > getErpWarehousePage(@Valid ErpWarehousePageReqVO pageReqVO) {
        PageResult<ErpWarehouseDO>  pageResult = erpWarehouseService.getErpWarehousePage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/page-distinct-factory")
    @Operation(summary = "获得ERP库位分页，根据工厂去重")
    @PreAuthorize("@ss.hasPermission('sply:erp-warehouse:query')")
    public CommonResult<PageResult<ErpWarehouseRespVO>> getErpWarehousePageDistinctByFactory(@Valid ErpWarehousePageReqVO pageReqVO) {
        PageResult<ErpWarehouseDO> pageResult = erpWarehouseService.getErpWarehousePageDistinctByFactory(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpWarehouseRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP库位 Excel")
    @PreAuthorize("@ss.hasPermission('sply:erp-warehouse:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportErpWarehouseExcel(@Valid ErpWarehousePageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpWarehouseDO> list = BeanUtils.toBean(erpWarehouseService.getErpWarehousePage(pageReqVO).getList(), ErpWarehouseDO.class);
        // 导出 Excel
        ExcelUtils.write(response, "ERP库位.xls", "数据", ErpWarehouseRespVO.class,
                BeanUtils.toBean(list, ErpWarehouseRespVO.class));
    }

    @PutMapping("/enable-list")
    @Operation(summary = "批量更新")
    @PreAuthorize("@ss.hasPermission('sply:warehouse:update')")
    public CommonResult<Boolean> enableWarehouseList(@RequestBody List<ErpWarehouseSaveReqVO> saveReqVOS) {
        erpWarehouseService.enableWarehouseList(saveReqVOS);
        return success(true);
    }

    @PostMapping("/getErpWarehouseTask")
    @Operation(summary = "定时获得erp更新库位")
    @PreAuthorize("@ss.hasPermission('sply:erp-warehouse:sync')")
    public CommonResult<Boolean> getErpWarehouseTask() {
        erpWarehouseService.callErpRfcInterface();
        return success(true);
    }

}