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.ErpCustomerPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpCustomerRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpCustomerSaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpCustomerDO;
import com.zt.plat.module.erp.service.erp.ErpCustomerService;
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-customer")
@Validated
public class ErpCustomerController {


    @Resource
    private ErpCustomerService erpCustomerService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP客商主数据")
    @PreAuthorize("@ss.hasPermission('sply:erp-customer:create')")
    public CommonResult<ErpCustomerRespVO> createErpCustomer(@Valid @RequestBody ErpCustomerSaveReqVO createReqVO) {
        return success(erpCustomerService.createErpCustomer(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP客商主数据")
    @PreAuthorize("@ss.hasPermission('sply:erp-customer:update')")
    public CommonResult<Boolean> updateErpCustomer(@Valid @RequestBody ErpCustomerSaveReqVO updateReqVO) {
        erpCustomerService.updateErpCustomer(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP客商主数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sply:erp-customer:delete')")
    public CommonResult<Boolean> deleteErpCustomer(@RequestParam("id") Long id) {
        erpCustomerService.deleteErpCustomer(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除ERP客商主数据")
                @PreAuthorize("@ss.hasPermission('sply:erp-customer:delete')")
    public CommonResult<Boolean> deleteErpCustomerList(@RequestBody BatchDeleteReqVO req) {
        erpCustomerService.deleteErpCustomerListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP客商主数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sply:erp-customer:query')")
    public CommonResult<ErpCustomerRespVO> getErpCustomer(@RequestParam("id") Long id) {
        ErpCustomerDO erpCustomer = erpCustomerService.getErpCustomer(id);
        return success(BeanUtils.toBean(erpCustomer, ErpCustomerRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP客商主数据分页")
    @PreAuthorize("@ss.hasPermission('sply:erp-customer:query')")
    public CommonResult<PageResult<ErpCustomerRespVO>> getErpCustomerPage(@Valid ErpCustomerPageReqVO pageReqVO) {
        PageResult<ErpCustomerDO> pageResult = erpCustomerService.getErpCustomerPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpCustomerRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP客商主数据 Excel")
    @PreAuthorize("@ss.hasPermission('sply:erp-customer:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportErpCustomerExcel(@Valid ErpCustomerPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpCustomerDO> list = erpCustomerService.getErpCustomerPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP客商主数据.xls", "数据", ErpCustomerRespVO.class,
                        BeanUtils.toBean(list, ErpCustomerRespVO.class));
    }

    @PostMapping("/getErpCustomerTask")
    @Operation(summary = "定时获得erp更新客商主数据")
    @PreAuthorize("@ss.hasPermission('sply:erp-customer:sync')")
    public CommonResult<Boolean> getErpCustomerTask() {
        erpCustomerService.callErpRfcInterface();
        return success(true);
    }
}