SnPartServiceImpl.java 3 KB
package com.huaheng.pc.config.sn.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.huaheng.common.utils.Wrappers;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.sn.domain.SnPart;
import com.huaheng.pc.config.sn.mapper.SnPartMapper;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeaderHistory.domain.ReceiptHeaderHistory;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.exception.BusinessException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import org.springframework.transaction.annotation.Transactional;


@Service
public class SnPartServiceImpl extends ServiceImpl<SnPartMapper, SnPart> implements SnPartService {

    /**
     * 随机生成部件号 9位数
     * @param type
     * @return
     */
    public  String createdPartCode(String type){
        int length=8;
        String characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        StringBuilder sb = new StringBuilder(length);
        Random random=new Random();
        for (int i = 0; i < length; i++) {
            sb.append(characters.charAt(random.nextInt(characters.length())));
        }
        return type+sb.toString();
    }

    /**
     * 查找最大的code
     * @param type
     * @return
     */
    public  String getMaxCode(String type){
        LambdaQueryWrapper<SnPart> lambda = new LambdaQueryWrapper<>();
        lambda.select(SnPart::getCode).eq(SnPart::getType, type);
        lambda.orderByDesc(SnPart::getId).last("Limit 1");
        //如果指定类型的最后的code存在,并且日期一致。那么 code = 入库单类型 + 年月日 + (排序号 + 1)
        String maxCode = null;
        SnPart snPart = this.getOne(lambda);
        if (snPart != null) {
            maxCode = this.getOne(lambda).getCode();
        }
        return maxCode;
    }



    /**
     * 随机生成电路板或电源n个
     * @param type
     * @param num
     * @return
     */
    @Transactional
    public AjaxResult savePart(String type,Integer num){
        String maxCode=getMaxCode(type);
        Integer Count =0;
        if(maxCode!=null){
            Count = Integer.valueOf(maxCode.substring(maxCode.length() - 1, maxCode.length()));
        }
        List<SnPart> list=new ArrayList<>();
        for (int i=0;i<num;i++){
            String code=type + String.format("%010d", Count ++);
            SnPart snPart=new SnPart();
            snPart.setCode(code);
            snPart.setType(type);
            snPart.setCreatedBy(ShiroUtils.getUser().getLoginName());
            snPart.setLastUpdatedBy(ShiroUtils.getLoginName());
            list.add(snPart);
        }
        this.saveBatch(list);
        return AjaxResult.success();
    }


}