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.ErpCompanyPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpCompanyRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpCompanySaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpCompanyDO;
import com.zt.plat.module.erp.service.erp.ErpCompanyService;
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-company")
@Validated
public class ErpCompanyController {


    @Resource
    private ErpCompanyService erpCompanyService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP公司")
    @PreAuthorize("@ss.hasPermission('sply:erp-company:create')")
    public CommonResult<ErpCompanyRespVO> createErpCompany(@Valid @RequestBody ErpCompanySaveReqVO createReqVO) {
        return success(erpCompanyService.createErpCompany(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP公司")
    @PreAuthorize("@ss.hasPermission('sply:erp-company:update')")
    public CommonResult<Boolean> updateErpCompany(@Valid @RequestBody ErpCompanySaveReqVO updateReqVO) {
        erpCompanyService.updateErpCompany(updateReqVO);
        return success(true);
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得ERP公司")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sply:erp-company:query')")
    public CommonResult<ErpCompanyRespVO> getErpCompany(@RequestParam("id") Long id) {
        ErpCompanyDO erpCompany = erpCompanyService.getErpCompany(id);
        return success(BeanUtils.toBean(erpCompany, ErpCompanyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP公司分页")
    @PreAuthorize("@ss.hasPermission('sply:erp-company:query')")
    public CommonResult<PageResult<ErpCompanyRespVO>> getErpCompanyPage(@Valid ErpCompanyPageReqVO pageReqVO) {
        PageResult<ErpCompanyDO> pageResult = erpCompanyService.getErpCompanyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpCompanyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP公司 Excel")
    @PreAuthorize("@ss.hasPermission('sply:erp-company:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportErpCompanyExcel(@Valid ErpCompanyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpCompanyDO> list = erpCompanyService.getErpCompanyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP公司.xls", "数据", ErpCompanyRespVO.class,
                        BeanUtils.toBean(list, ErpCompanyRespVO.class));
    }

    @PostMapping("/getErpCompanyTask")
    @Operation(summary = "定时获得erp更新公司")
    @PreAuthorize("@ss.hasPermission('sply:erp-company:sync')")
    public CommonResult<Boolean> getErpCompanyTask() {
        erpCompanyService.callErpRfcInterface();
        return success(true);
    }

    @PostMapping("/test")
    @Operation(summary = "定时获得erp更新公司")
    @PreAuthorize("@ss.hasPermission('sply:erp-company:create')")
    public CommonResult<Boolean> test() {
        erpCompanyService.test();
        return success(true);
    }

    @PostMapping("/test1")
    @Operation(summary = "获取配置")
    @PreAuthorize("@ss.hasPermission('sply:erp-company:get')")
    public CommonResult<String> test1() {
        String TEST = erpCompanyService.test1();
        return success(TEST);
    }

}