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.ErpFactoryPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpFactoryRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpFactorySaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpFactoryDO;
import com.zt.plat.module.erp.service.erp.ErpFactoryService;
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-factory")
@Validated
public class ErpFactoryController {


    @Resource
    private ErpFactoryService erpFactoryService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP工厂")
    @PreAuthorize("@ss.hasPermission('sply:erp-factory:create')")
    public CommonResult<ErpFactoryRespVO> createErpFactory(@Valid @RequestBody ErpFactorySaveReqVO createReqVO) {
        return success(erpFactoryService.createErpFactory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP工厂")
    @PreAuthorize("@ss.hasPermission('sply:erp-factory:update')")
    public CommonResult<Boolean> updateErpFactory(@Valid @RequestBody ErpFactorySaveReqVO updateReqVO) {
        erpFactoryService.updateErpFactory(updateReqVO);
        return success(true);
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得ERP工厂")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sply:erp-factory:query')")
    public CommonResult<ErpFactoryRespVO> getErpFactory(@RequestParam("id") Long id) {
        ErpFactoryDO erpFactory = erpFactoryService.getErpFactory(id);
        return success(BeanUtils.toBean(erpFactory, ErpFactoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP工厂分页")
    @PreAuthorize("@ss.hasPermission('sply:erp-factory:query')")
    public CommonResult<PageResult<ErpFactoryRespVO>> getErpFactoryPage(@Valid ErpFactoryPageReqVO pageReqVO) {
        PageResult<ErpFactoryDO> pageResult = erpFactoryService.getErpFactoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpFactoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP工厂 Excel")
    @PreAuthorize("@ss.hasPermission('sply:erp-factory:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportErpFactoryExcel(@Valid ErpFactoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpFactoryDO> list = erpFactoryService.getErpFactoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP工厂.xls", "数据", ErpFactoryRespVO.class,
                        BeanUtils.toBean(list, ErpFactoryRespVO.class));
    }

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

    @PostMapping("/getErpFactoryTask")
    @Operation(summary = "定时获得erp工厂数据")
    @PreAuthorize("@ss.hasPermission('sply:erp-factory:sync')")
    public CommonResult<Boolean> getErpFactoryTask() {
        erpFactoryService.callErpRfcInterface();
        return success(true);
    }

}