CheckDetailService.java 4.38 KB
package com.huaheng.pc.check.checkDetail.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.check.checkHeader.domain.CheckHeader;
import com.huaheng.pc.check.checkHeader.service.CheckHeaderService;
import com.huaheng.pc.check.checkingRegister.domain.CheckingRegister;
import com.huaheng.pc.check.checkingRegister.service.CheckingRegisterService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.pc.check.checkDetail.mapper.CheckDetailMapper;
import com.huaheng.pc.check.checkDetail.domain.CheckDetail;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CheckDetailService extends ServiceImpl<CheckDetailMapper, CheckDetail> {

    @Resource
    private CheckHeaderService checkHeaderService;
    @Resource
    private CheckingRegisterService checkingRegisterService;
    @Resource
    private ReceiptDetailService receiptDetailService;

    /**
     * 质检完成
     * @param id 质检明细id
     * @return AjaxResult
     */
    @Transactional
    public AjaxResult complete(Integer id){
        LambdaQueryWrapper<CheckingRegister> checkingRegisterLambda = Wrappers.lambdaQuery();
        checkingRegisterLambda.eq(CheckingRegister::getCheckDetailId, id);
        List<CheckingRegister> checkingRegisters = checkingRegisterService.list(checkingRegisterLambda);
        int total = 0; //质检登记中的总数量
        for (CheckingRegister checkingRegister: checkingRegisters){
            total += checkingRegister.getQty();
        }

        //更新传入明细id为完成质检状态
        CheckDetail checkDetail = this.getById(id);
        if ( checkDetail.getQty().compareTo(BigDecimal.valueOf(total)) != 0){
            return AjaxResult.error("质检登记中总数量不等于质检明细中数量,不能完成质检");
        }
        checkDetail.setCheckBy(ShiroUtils.getLoginName());
        checkDetail.setCheckAt(new Date());

        checkDetail.setStatus((QuantityConstant.CHECK_STATUS_COMPLETED).toString());
        checkDetail.setLastUpdated(new Date());
        checkDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
        if (!this.updateById(checkDetail)){
            throw new ServiceException("更新质检明细表失败");
        }
        LambdaQueryWrapper<CheckDetail> lambda = Wrappers.lambdaQuery();
        lambda.eq(CheckDetail::getCheckHeaderId, checkDetail.getCheckHeaderId());
        List<CheckDetail> checkDetails = this.list(lambda);

        //判断头表下所有明细是否全部完成
        boolean result = false;
        for (CheckDetail checkDetail1 : checkDetails){
            if ((QuantityConstant.CHECK_STATUS_COMPLETED).toString().equals(checkDetail1.getStatus())){
                result = true;
            } else {
                result = false;
            }
        }
        if (result){
            CheckHeader checkHeader = new CheckHeader();
            checkHeader.setId(checkDetail.getCheckHeaderId());
            checkHeader.setStatus((QuantityConstant.CHECK_STATUS_COMPLETED).toString());
            if ( !checkHeaderService.updateById(checkHeader)){
                throw new ServiceException("质检头表更新失败");
            }

            //更新入库单明细状态
            ReceiptDetail receiptDetail = receiptDetailService.queryflow(receiptDetailService.getById(checkDetail.getReceiptDetailId()));
            if (!receiptDetailService.updateById(receiptDetail)){
                throw new ServiceException("入库单明细更新失败");
            }
            //更新头表状态
            receiptDetailService.updateReceiptHeaderLastStatus(receiptDetail.getReceiptId());

        }
        return AjaxResult.success("质检完成");
    }
}