package com.klp.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.klp.domain.DrMillProcessRecipe;
import com.klp.domain.DrMillProcessRecipeVersion;
import com.klp.domain.WmsProcessSpec;
import com.klp.domain.WmsProcessSpecVersion;
import com.klp.domain.WmsProductionLine;
import com.klp.mapper.WmsProcessSpecMapper;
import com.klp.mapper.WmsProcessSpecVersionMapper;
import com.klp.mapper.WmsProductionLineMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.List;
/**
* 双机架工艺方案 → L3规程(wms_process_spec)同步服务
*
* 注意:此 Service 不标注 @DS,默认走 master 数据源,
* 供 Controller 在 double-rack 查询完方案列表后调用,
* 将缺失的 wms_process_spec 记录自动补齐。
*
*/
@Slf4j
@RequiredArgsConstructor
@Service
public class DrRecipeSyncService {
/** 双机架产线编号,与 wms_production_line.line_code 对应 */
private static final String DR_LINE_CODE = "DR";
private static final String DR_LINE_NAME = "双机架轧机";
/** spec_code 前缀,区分同名方案号属于哪条产线 */
private static final String SPEC_CODE_PREFIX = "DR-";
private final WmsProcessSpecMapper specMapper;
private final WmsProcessSpecVersionMapper specVersionMapper;
private final WmsProductionLineMapper lineMapper;
/**
* 检查并补充 wms_process_spec:
* 对列表中每条方案,若在 L3 规程表中不存在则自动新增。
*/
@Transactional(rollbackFor = Exception.class)
public void syncRecipesToSpec(List recipes) {
if (recipes == null || recipes.isEmpty()) return;
Long lineId = getOrCreateDrLineId();
int created = 0;
for (DrMillProcessRecipe recipe : recipes) {
String specCode = SPEC_CODE_PREFIX + recipe.getRecipeNo();
LambdaQueryWrapper qw = Wrappers.lambdaQuery();
qw.eq(WmsProcessSpec::getSpecCode, specCode);
if (specMapper.selectCount(qw) == 0) {
WmsProcessSpec spec = new WmsProcessSpec();
spec.setSpecCode(specCode);
spec.setSpecName(recipe.getRecipeNo());
spec.setSpecType("PROCESS");
spec.setLineId(lineId);
spec.setIsEnabled(1);
spec.setRemark("由双机架工艺方案自动同步");
specMapper.insert(spec);
created++;
log.info("[DR同步] 新增 wms_process_spec: specCode={}, recipeId={}",
specCode, recipe.getRecipeId());
}
}
if (created > 0) {
log.info("[DR同步] 本次共补充 {} 条 L3 工艺规程", created);
}
}
/**
* 将双机架工艺版本(mill_process_recipe_version)同步到主库规程版本(wms_process_spec_version)。
* 已存在的版本更新 isActive / status;不存在则新增。
*
* @param recipeNo 对应方案号(用于定位 wms_process_spec)
* @param drVersions 已从 double-rack 库查出的版本列表
*/
@Transactional(rollbackFor = Exception.class)
public void syncVersionsToSpec(String recipeNo, List drVersions) {
if (recipeNo == null || drVersions == null || drVersions.isEmpty()) return;
String specCode = SPEC_CODE_PREFIX + recipeNo;
LambdaQueryWrapper sqw = Wrappers.lambdaQuery();
sqw.eq(WmsProcessSpec::getSpecCode, specCode);
WmsProcessSpec spec = specMapper.selectOne(sqw);
if (spec == null) return; // spec 尚未同步,跳过
Long specId = spec.getSpecId();
for (DrMillProcessRecipeVersion drVer : drVersions) {
LambdaQueryWrapper vqw = Wrappers.lambdaQuery();
vqw.eq(WmsProcessSpecVersion::getSpecId, specId)
.eq(WmsProcessSpecVersion::getVersionCode, drVer.getVersionCode());
WmsProcessSpecVersion existing = specVersionMapper.selectOne(vqw);
int isActive = drVer.getIsActive() != null ? drVer.getIsActive() : 0;
String status = "1".equals(drVer.getStatus()) ? "PUBLISHED" : "DRAFT";
if (existing == null) {
WmsProcessSpecVersion ver = new WmsProcessSpecVersion();
ver.setSpecId(specId);
ver.setVersionCode(drVer.getVersionCode());
ver.setIsActive(isActive);
ver.setStatus(status);
ver.setRemark(drVer.getRemark() != null ? drVer.getRemark() : "由双机架工艺版本自动同步");
specVersionMapper.insert(ver);
log.info("[DR同步] 新增 wms_process_spec_version: specCode={}, versionCode={}",
specCode, drVer.getVersionCode());
} else {
existing.setIsActive(isActive);
existing.setStatus(status);
specVersionMapper.updateById(existing);
}
}
}
/**
* 查找或创建双机架产线记录,返回 line_id。
*/
private Long getOrCreateDrLineId() {
LambdaQueryWrapper lqw = Wrappers.lambdaQuery();
lqw.eq(WmsProductionLine::getLineCode, DR_LINE_CODE);
WmsProductionLine line = lineMapper.selectOne(lqw);
if (line == null) {
line = new WmsProductionLine();
line.setLineCode(DR_LINE_CODE);
line.setLineName(DR_LINE_NAME);
line.setCapacity(BigDecimal.ZERO);
line.setUnit("t");
line.setIsEnabled(1);
line.setRemark("双机架轧机产线,系统自动创建");
lineMapper.insert(line);
log.info("[DR同步] 新建 wms_production_line: lineCode={}, lineId={}",
DR_LINE_CODE, line.getLineId());
}
return line.getLineId();
}
}