package com.cf.imes.module.system.controller.admin.assemblyconfig;

import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.module.system.controller.admin.assemblyconfig.vo.AssemblyConfigImportReqVO;
import com.cf.imes.module.system.controller.admin.assemblyconfig.vo.AssemblyConfigListRespVO;
import com.cf.imes.module.system.controller.admin.assemblyconfig.vo.AssemblyConfigRespVO;
import com.cf.imes.module.system.controller.admin.assemblyconfig.vo.AssemblyConfigSaveReqVO;
import com.cf.imes.module.system.service.assemblyconfig.AssemblyConfigService;
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.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *
 *
 * @author Gqr
 * @since 2026/2/25 15:13
 */
@Tag(name = "管理后台 - 系统配置 - 装配设置")
@RestController
@RequestMapping("/system/assembly/config")
@Validated
@Slf4j
public class AssemblyConfigController {
    @Resource
    private AssemblyConfigService assemblyConfigService;

    @PostMapping("/order/import")
    @Operation(summary = "导入装配顺序")
    @PreAuthorize("@ss.hasPermission('systemconfig:assemblyconfig:import')")
    public CommonResult<Boolean> importAssemblyConfigFromOrders(@RequestBody @Valid AssemblyConfigImportReqVO reqVO) {
        assemblyConfigService.importAssemblyConfigFromOrders(reqVO);
        return CommonResult.success(true);
    }

    @GetMapping("/list")
    @Operation(summary = "查询组织装配设置列表")
    @PreAuthorize("@ss.hasPermission('systemconfig:assemblyconfig:query')")
    public CommonResult<List<AssemblyConfigListRespVO>> getAssemblyConfigList(@RequestParam("name") String name) {
        return CommonResult.success(assemblyConfigService.getOrgAssemblyConfigList(name));
    }

    @GetMapping("/{configId}")
    @Operation(summary = "查询装配设置")
    @Parameter(name = "configId", description = "装配设置编号", required = true)
    @PreAuthorize("@ss.hasPermission('systemconfig:assemblyconfig:query')")
    public CommonResult<AssemblyConfigRespVO> getAssemblyConfig(@PathVariable Long configId) {
        return CommonResult.success(assemblyConfigService.getAssemblyConfig(configId));
    }

    @PostMapping("/{configId}")
    @Operation(summary = "待选部件转加工部件")
    @Parameter(name = "configId", description = "装配设置编号", required = true)
    @PreAuthorize("@ss.hasPermission('systemconfig:assemblyconfig:save')")
    public CommonResult<Boolean> doConfig(@PathVariable Long configId) {
        assemblyConfigService.doConfig(configId);
        return CommonResult.success(true);
    }

    @PostMapping("/new/{configId}")
    @Operation(summary = "新增加工部件")
    @Parameter(name = "configId", description = "装配设置编号", required = true)
    @PreAuthorize("@ss.hasPermission('systemconfig:assemblyconfig:save')")
    public CommonResult<Boolean> newConfig(@PathVariable Long configId) {
        assemblyConfigService.newConfig(configId);
        return CommonResult.success(true);
    }

    @DeleteMapping("/{configId}")
    @Operation(summary = "删除已配置的加工部件，恢复到待选")
    @Parameter(name = "configId", description = "装配设置编号", required = true)
    @PreAuthorize("@ss.hasPermission('systemconfig:assemblyconfig:delete')")
    public CommonResult<Boolean> removeConfigured(@PathVariable Long configId) {
        assemblyConfigService.delete(configId);
        return CommonResult.success(true);
    }

    @PostMapping("/save")
    @Operation(summary = "保存装配设置")
    @PreAuthorize("@ss.hasPermission('systemconfig:assemblyconfig:save')")
    public CommonResult<Boolean> saveAssemblyConfig(@RequestBody AssemblyConfigSaveReqVO reqVO) {
        assemblyConfigService.saveConfig(reqVO);
        return CommonResult.success(true);
    }
}