zhangherong
2025-06-25 23855599412c4d61b38d78f0f3abd3430a48b5b1
lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/service/impl/GuideCardBatchServiceImpl.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,143 @@
package org.jeecg.modules.dnc.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.dnc.constant.DocAttributionTypeEnum;
import org.jeecg.modules.dnc.entity.*;
import org.jeecg.modules.dnc.mapper.GuideCardBatchMapper;
import org.jeecg.modules.dnc.service.*;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
/**
 * @Description: nc文件对应数控程序加工确认表
 * @Author: jeecg-boot
 * @Date:   2025-05-27
 * @Version: V1.0
 */
@Service
public class GuideCardBatchServiceImpl extends ServiceImpl<GuideCardBatchMapper, GuideCardBatch> implements IGuideCardBatchService {
    @Autowired
    private ISysDictService sysDictService;
    @Autowired
    private IDocInfoService docInfoService;
    @Autowired
    private IPartsInfoService partsInfoService;
    @Autowired
    private IProcessStreamService processStreamService;
    @Autowired
    private IWorkStepService workStepService;
    @Autowired
    private IDeviceTypeService deviceTypeService;
    /**
     * ç”Ÿæˆæµæ°´å·
     * @param code
     * @return
     */
    @Override
    public String getSerialNumber(String code) {
        // äº‹ä»¶ç¼–号格式:年份后两位 + å•位编码 + æ“ä½œå·¥è´¦å· + æµæ°´å·ï¼ˆ4位)
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // èŽ·å–å½“å‰å¹´ä»½åŽä¸¤ä½
        String yearSuffix = DateUtils.formatDate(new Date(), "yy");
        // æŸ¥è¯¢å½“年所有记录
        QueryWrapper<GuideCardBatch> wrapper = new QueryWrapper<>();
        wrapper.apply("YEAR(create_time) = YEAR(GETDATE())");
        wrapper.isNotNull("serial_number");
        wrapper.orderByDesc("SUBSTRING(serial_number, LEN(serial_number)-3, 4)");
        List<GuideCardBatch> list = this.list(wrapper);
        // ç”Ÿæˆæµæ°´å·é€»è¾‘
        String serialSuffix;
        if (!list.isEmpty()) {
            // æå–最新流水号的后四位
            String lastSerial = list.get(0).getSerialNumber();
            String lastSuffix = lastSerial.substring(lastSerial.length() - 4);
            // æµæ°´å·è‡ªå¢žï¼ˆå¤„理9999溢出)
            int nextNum = Integer.parseInt(lastSuffix) + 1;
            serialSuffix = String.format("%04d", nextNum > 9999 ? 1 : nextNum); // è¶…过9999则重置为0001
        } else {
            serialSuffix = "0001"; // å½“年无记录则初始化
        }
        // æ‹¼æŽ¥å®Œæ•´ç¼–号
        return yearSuffix+"-"+ code+"-"+ user.getUsername()+"-"+ serialSuffix;
    }
    /**
     * å¯¼å…¥NC文件默认产生nc文件对应数控程序加工确认表
     * @param docId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean importGuideCardBatch(String docId,String attributionId,Integer attributionType){
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        DocInfo docInfo=docInfoService.getById(docId);
        if (docInfo==null){
            return false;
        }
        PartsInfo partsInfo=new PartsInfo();
        ProcessStream processStream;
        WorkStep workStep;
        DeviceType deviceType;
        GuideCardBatch guideCardBatch=new GuideCardBatch();
        if (DocAttributionTypeEnum.PROCESS.getCode().equals(attributionType)){
            //工序设备类
            deviceType=deviceTypeService.getById(attributionId);
            if (deviceType==null){
                return false;
            }
            processStream=processStreamService.getById(deviceType.getAttributionId());
            if (processStream==null){
                return false;
            }
            guideCardBatch.setProcessWorkCode(processStream.getProcessCode());
            partsInfo=partsInfoService.getById(processStream.getPartsId());
        }else if (DocAttributionTypeEnum.WORKSITE.getCode().equals(attributionType)){
            //工步设备类
            deviceType=deviceTypeService.getById(attributionId);
            if (deviceType==null){
                return false;
            }
            workStep=workStepService.getById(deviceType.getAttributionId());
            if (workStep==null){
                return false;
            }
            guideCardBatch.setProcessWorkCode(workStep.getStepCode());
            partsInfo=partsInfoService.getById(workStep.getPartsId());
        }
        guideCardBatch.setDocId(docId);
        guideCardBatch.setSerialNumber(getSerialNumber("C140"));
        guideCardBatch.setUnit(sysDictService.queryDictTextByKey("unit_code", "C140"));
        guideCardBatch.setDocName(docInfo.getDocName());
        if (partsInfo!=null){
            guideCardBatch.setPartsCode(partsInfo.getPartsCode());
            guideCardBatch.setPartsName(partsInfo.getPartsName());
            guideCardBatch.setMaterielDesp(partsInfo.getMaterielDesp());
        }
        guideCardBatch.setFlowStatus("0");
        guideCardBatch.setCompiler(user.getUsername());
        guideCardBatch.setCreateTime(new Date());
        return this.save(guideCardBatch);
    }
}