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

import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.base.controller.admin.base.vo.MasterDataSyncReqVO;
import com.zt.plat.module.base.service.masterdatasync.MasterDataCategorySyncService;
import com.zt.plat.module.base.service.masterdatasync.MasterDataSyncService;
import com.zt.plat.module.base.service.masterdatasync.dto.MasterDataSyncCommand;
import com.zt.plat.module.base.service.masterdatasync.dto.MasterDataSyncReport;
import com.zt.plat.module.base.service.masterdatasync.dto.MasterDataCategorySyncReport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.zt.plat.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 主数据同步")
@RestController
@RequestMapping("/base/master-data-sync")
@Validated
public class MasterDataSyncController {

    @Resource
    private MasterDataSyncService masterDataSyncService;
    @Resource
    private MasterDataCategorySyncService masterDataCategorySyncService;

    @PostMapping("/execute")
    @Operation(summary = "执行主数据同步")
    @PreAuthorize("@ss.hasPermission('base:master-data-sync:execute')")
    public CommonResult<MasterDataSyncReport> execute(@Valid @RequestBody MasterDataSyncReqVO reqVO) {
        MasterDataSyncCommand command = BeanUtils.toBean(reqVO, MasterDataSyncCommand.class);
        MasterDataSyncReport report = masterDataSyncService.sync(command);
        return success(report);
    }

    @PostMapping("/categories")
    @Operation(summary = "同步物料分类")
    @PreAuthorize("@ss.hasPermission('base:master-data-sync:categories')")
    public CommonResult<MasterDataCategorySyncReport> syncCategories() {
        MasterDataCategorySyncReport report = masterDataCategorySyncService.syncAll();
        return success(report);
    }
}
