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.ErpSalesOrganizationPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpSalesOrganizationRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpSalesOrganizationSaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpSalesOrganizationDO;
import com.zt.plat.module.erp.service.erp.ErpSalesOrganizationService;
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-sales-organization")
@Validated
public class ErpSalesOrganizationController {


    @Resource
    private ErpSalesOrganizationService erpSalesOrganizationService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP销售组织")
    @PreAuthorize("@ss.hasPermission('sply:erp-sales-organization:create')")
    public CommonResult<ErpSalesOrganizationRespVO> createErpSalesOrganization(@Valid @RequestBody ErpSalesOrganizationSaveReqVO createReqVO) {
        return success(erpSalesOrganizationService.createErpSalesOrganization(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP销售组织")
    @PreAuthorize("@ss.hasPermission('sply:erp-sales-organization:update')")
    public CommonResult<Boolean> updateErpSalesOrganization(@Valid @RequestBody ErpSalesOrganizationSaveReqVO updateReqVO) {
        erpSalesOrganizationService.updateErpSalesOrganization(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP销售组织")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sply:erp-sales-organization:delete')")
    public CommonResult<Boolean> deleteErpSalesOrganization(@RequestParam("id") Long id) {
        erpSalesOrganizationService.deleteErpSalesOrganization(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除ERP销售组织")
                @PreAuthorize("@ss.hasPermission('sply:erp-sales-organization:delete')")
    public CommonResult<Boolean> deleteErpSalesOrganizationList(@RequestBody BatchDeleteReqVO req) {
        erpSalesOrganizationService.deleteErpSalesOrganizationListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP销售组织")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sply:erp-sales-organization:query')")
    public CommonResult<ErpSalesOrganizationRespVO> getErpSalesOrganization(@RequestParam("id") Long id) {
        ErpSalesOrganizationDO erpSalesOrganization = erpSalesOrganizationService.getErpSalesOrganization(id);
        return success(BeanUtils.toBean(erpSalesOrganization, ErpSalesOrganizationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP销售组织分页")
    @PreAuthorize("@ss.hasPermission('sply:erp-sales-organization:query')")
    public CommonResult<PageResult<ErpSalesOrganizationRespVO>> getErpSalesOrganizationPage(@Valid ErpSalesOrganizationPageReqVO pageReqVO) {
        PageResult<ErpSalesOrganizationDO> pageResult = erpSalesOrganizationService.getErpSalesOrganizationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpSalesOrganizationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP销售组织 Excel")
    @PreAuthorize("@ss.hasPermission('sply:erp-sales-organization:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportErpSalesOrganizationExcel(@Valid ErpSalesOrganizationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpSalesOrganizationDO> list = erpSalesOrganizationService.getErpSalesOrganizationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP销售组织.xls", "数据", ErpSalesOrganizationRespVO.class,
                        BeanUtils.toBean(list, ErpSalesOrganizationRespVO.class));
    }

    @PostMapping("/getErpSalesOrganizationTask")
    @Operation(summary = "定时获得erp更新销售组织")
    @PreAuthorize("@ss.hasPermission('sply:erp-sales-organization:sync')")
    public CommonResult<Boolean> getErpSalesOrganizationTask() {
        erpSalesOrganizationService.callErpRfcInterface();
        return success(true);
    }

}