MaterialServiceImpl.java 10.3 KB
package com.huaheng.pc.config.material.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.exception.BusinessException;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.mobile.shipment.Materialforecast;
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.company.service.CompanyService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.mapper.MaterialMapper;
import com.huaheng.pc.config.materialMultiple.domain.MaterialMultiple;
import com.huaheng.pc.config.materialType.domain.MaterialType;
import com.huaheng.pc.config.materialType.service.MaterialTypeService;
import com.huaheng.pc.config.materialUnit.domain.MaterialUnit;
import com.huaheng.pc.config.materialUnit.service.MaterialUnitService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.pc.config.material.mapper.MaterialMapper;
import com.huaheng.pc.config.material.domain.Material;

@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService{

    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private MaterialUnitService materialUnitService;
    @Resource
    private MaterialTypeService materialTypeService;
    @Resource
    private MaterialMapper materialMapper;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private CompanyService companyService;

    @Override
    public AjaxResult removeByIds(String ids) {
        int count = 0;
        for (Integer id : Convert.toIntArray(ids)) {
            Material material = this.getById(id);

            LambdaQueryWrapper<ReceiptDetail> lambda = Wrappers.lambdaQuery();
            lambda.eq(ReceiptDetail::getWarehouseCode, ShiroUtils.getWarehouseCode())
                  .eq(ReceiptDetail::getMaterialCode, material.getCode())
                  .eq(ReceiptDetail::getDeleted, false)
                .last("LIMIT 1");

            Map<String, Object> map = receiptDetailService.getMap(lambda);
            if (map != null) {
                return AjaxResult.error("物料编码(" + material.getCode() +")存在入库单,不能删除!");
            }
            material.setDeleted(true);
            material.setLastUpdatedBy(ShiroUtils.getLoginName());
            if (this.updateById(material)){
                count++;
            }
        }
        return AjaxResult.success("成功删除"+count+"条记录");
    }

    /**
     * 导入物料数据
     *
     * @param materialList        用户数据列表
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    public String importMaterial(List<Material> materialList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(materialList) || materialList.size() == 0) {
            throw new  BusinessException("导入数据不能为空!");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Material material : materialList) {
            try {
                LambdaQueryWrapper<Material> lambdaQueryWrapper = Wrappers.lambdaQuery();
                lambdaQueryWrapper.eq(Material::getCode, material.getCode());
                // 验证是否存在这个用户
                Material m = this.getOne(lambdaQueryWrapper);
                if (StringUtils.isNull(m)) {
                    System.out.println(ShiroUtils.getLoginName());
                    material.setCreatedBy(ShiroUtils.getUser().getLoginName());
                    material.setLastUpdatedBy(ShiroUtils.getLoginName());
                    this.save(material);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、编码 " + material.getCode() + " 导入成功");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、编码" + material.getCode() + " 导入失败:";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
            throw new BusinessException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
        }
        return successMsg.toString();
    }

    /**
     * 添加物料
     * @param material
     * @return
     */
    @Override
    public AjaxResult addSave(Material material) {
        LambdaQueryWrapper<Material> lambda = Wrappers.lambdaQuery();
        //如果物料编码为空则生成物料编码
        if (material.getCode() == null) {
            material.setCode(createCode(material.getType()));
        }
        lambda.eq(Material::getCode, material.getCode())
                .eq(Material::getWarehouseCode, ShiroUtils.getWarehouseCode())
                .eq(Material::getDeleted, false);
        Map<String, Object> map = this.getMap(lambda);
        if (map != null) {
            return AjaxResult.error("物料已经存在");
        }

        LambdaQueryWrapper<MaterialUnit> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(MaterialUnit::getMaterialCode, material.getCode())
                .eq(MaterialUnit::getUnit, material.getUnit());
        //如果不存在该物料的单位是新建物料单位
        if (materialUnitService.getOne(lambdaQueryWrapper) == null){
            MaterialUnit materialUnit = new MaterialUnit();
            materialUnit.setMaterialCode(material.getCode());
            materialUnit.setMaterialName(material.getName());
            materialUnit.setMaterialSpec(material.getSpec());
            materialUnit.setCompanyCode(material.getCompanyCode());
            materialUnit.setWarehouseCode(ShiroUtils.getWarehouseCode());
            materialUnit.setUnit(material.getUnit());
            materialUnit.setCreatedBy(ShiroUtils.getLoginName());
            materialUnit.setLastUpdatedBy(ShiroUtils.getLoginName());

            materialUnitService.save(materialUnit);
        }
        material.setWarehouseCode(ShiroUtils.getWarehouseCode());
        material.setCreatedBy(ShiroUtils.getLoginName());
        material.setLastUpdatedBy(ShiroUtils.getLoginName());
        Boolean flag=this.save(material);
        if(flag==false){
            return AjaxResult.error("新增物料失败,存入数据库时报错");
        }
        return AjaxResult.success("新增物料成功");
    }

    @Override
    public Material findAllByCode(String code) {
        LambdaQueryWrapper<Material> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Material::getCode, code);
        return getOne(queryWrapper);
    }


    @Override
    public List<Material> findLatest() {
        return materialMapper.findLatest();
    }

    /**
     * 获得物料联想词
     *
     * @param code
     * @return
     */
    @Override
    public List<Materialforecast> getMaterialForecast(String code) {
        List<Materialforecast> forecast = new ArrayList<>();
        LambdaQueryWrapper<InventoryDetail> detailLambdaQueryWrapper = Wrappers.lambdaQuery();
        detailLambdaQueryWrapper.eq(InventoryDetail::getMaterialCode, code)
                .eq(InventoryDetail::getWarehouseCode, ShiroUtils.getWarehouseCode())
                .in(InventoryDetail::getCompanyCode,ShiroUtils.getCompanyCodeList());
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(detailLambdaQueryWrapper);
        if (!inventoryDetailList.isEmpty()) {
            for (InventoryDetail inventoryDetail : inventoryDetailList) {
                Materialforecast materialforecast = new Materialforecast();
                materialforecast.setMaterialCode(inventoryDetail.getMaterialCode());
                materialforecast.setType(0);
                forecast.add(materialforecast);
            }
        } else {
            detailLambdaQueryWrapper = Wrappers.lambdaQuery();
            detailLambdaQueryWrapper.like(InventoryDetail::getMaterialName, code)
                    .eq(InventoryDetail::getWarehouseCode, ShiroUtils.getWarehouseCode())
                    .in(InventoryDetail::getCompanyCode,ShiroUtils.getCompanyCodeList());
            List<InventoryDetail> inventoryDetailList1 = inventoryDetailService.list(detailLambdaQueryWrapper);
            if (!inventoryDetailList1.isEmpty()) {
                for (InventoryDetail inventoryDetail : inventoryDetailList) {
                    Materialforecast materialforecast = new Materialforecast();
                    materialforecast.setMaterialCode(inventoryDetail.getMaterialCode());
                    materialforecast.setType(0);
                    forecast.add(materialforecast);
                }
            }
        }
        return null;
    }

    /**
     * 生成物料编码
     * @param code
     * @return
     */
    private String createCode(String code){
        LambdaQueryWrapper<MaterialType> lambda = Wrappers.lambdaQuery();
        lambda.eq(MaterialType::getCode, code);
        MaterialType materialType = materialTypeService.getOne(lambda);

        return String.format(materialType.getAutoGenSerialNumFormat(), materialType.getTrackSerialNum());
    }
}