CycleCountPreferenceService.java 4.21 KB
package com.huaheng.pc.config.cycleCountPreference.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.containerType.domain.ContainerType;
import com.huaheng.pc.inventory.cycleCountDetail.domain.CycleCountDetail;
import com.huaheng.pc.inventory.cycleCountDetail.service.CycleCountDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.pc.config.cycleCountPreference.mapper.CycleCountPreferenceMapper;
import com.huaheng.pc.config.cycleCountPreference.domain.CycleCountPreference;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CycleCountPreferenceService extends ServiceImpl<CycleCountPreferenceMapper, CycleCountPreference> {

    /**
     *新增盘点首选项
     * @param cycleCountPreference
     * @return
     */
    @Transactional
    public AjaxResult addSave (CycleCountPreference cycleCountPreference){

        cycleCountPreference.setCode(newCode());
        cycleCountPreference.setWarehouseCode(ShiroUtils.getWarehouseCode());
        cycleCountPreference.setCreatedBy(ShiroUtils.getLoginName());
        cycleCountPreference.setCreated(new Date());
        cycleCountPreference.setLastUpdatedBy(ShiroUtils.getLoginName());
        this.save(cycleCountPreference);
        return AjaxResult.success("新增完成!");
    }


    /**
     * 自动生成盘点首选项Code
     * 使用特定字段和ID组合成Code
     */
    public String newCode(){
        //先查询前一个ID生成当前的数字,再拼接
        String code = null;
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        LambdaQueryWrapper<CycleCountPreference> lambda = Wrappers.lambdaQuery();
        lambda.select(CycleCountPreference::getCode)
                .orderByDesc(CycleCountPreference::getId).last("Limit 1");
        // code = ccp + 日期 + (排序号 + 1)
        String maxCode = null;
        CycleCountPreference cycleCountPreference = this.getOne(lambda);
        if ( cycleCountPreference != null){
            maxCode = this.getOne(lambda).getCode();
        }
        if (maxCode != null && maxCode.substring(maxCode.length() - 13, maxCode.length() - 5).equals(df.format(now))) {
            Integer Count = Integer.valueOf(maxCode.substring(maxCode.length() - 5 ));
            code = "ccp" + df.format(now) + String.format("%05d", Count + 1);
        } else {
            code = "ccp" + df.format(now) +"00001";
        }
        return code;
    }

    /**
     * 复制盘点首选项表
     *
     * @param warehouseCode    原仓库编码
     * @param newWarehouseCode 新仓库编码
     * @return 是否复制成功
     */
    @Transactional
    public boolean cycleCountPreferenceCopy(String warehouseCode, String newWarehouseCode) {
        log.trace("开始复盘点首选项表");
        LambdaQueryWrapper<CycleCountPreference> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(CycleCountPreference::getWarehouseCode, newWarehouseCode);
        if (!this.list(lambdaQueryWrapper).isEmpty()){
            log.error(newWarehouseCode+"仓库已存在");
            return false;
        }

        lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(CycleCountPreference::getWarehouseCode, warehouseCode);
        List<CycleCountPreference> cycleCountPreferenceList = this.list(lambdaQueryWrapper);

        for ( CycleCountPreference countPreference : cycleCountPreferenceList) {
            countPreference.setId(null);
            countPreference.setWarehouseCode(newWarehouseCode);
        }

        if ( this.saveBatch(cycleCountPreferenceList) ){
            log.trace("复制盘点首选项表成功,新仓库编码是:"+newWarehouseCode);
            return true;
        } else {
            return false;
        }
    }








}