package com.vincent.rsf.server.manager.service.impl; 
 | 
  
 | 
import cn.afterturn.easypoi.excel.ExcelImportUtil; 
 | 
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult; 
 | 
import com.alibaba.fastjson.JSONObject; 
 | 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; 
 | 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 
 | 
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; 
 | 
import com.vincent.rsf.framework.common.R; 
 | 
import com.vincent.rsf.framework.exception.CoolException; 
 | 
import com.vincent.rsf.server.common.domain.BaseParam; 
 | 
import com.vincent.rsf.server.common.domain.PageParam; 
 | 
import com.vincent.rsf.server.common.utils.CommonUtil; 
 | 
import com.vincent.rsf.server.common.utils.ExcelUtil; 
 | 
import com.vincent.rsf.server.common.utils.FieldsUtils; 
 | 
import com.vincent.rsf.server.manager.controller.params.MatnrToGroupParams; 
 | 
import com.vincent.rsf.server.manager.entity.MatnrGroup; 
 | 
import com.vincent.rsf.server.manager.entity.excel.MatnrsTemplate; 
 | 
import com.vincent.rsf.server.manager.enums.MatnrLocLevel; 
 | 
import com.vincent.rsf.server.manager.mapper.MatnrMapper; 
 | 
import com.vincent.rsf.server.manager.entity.Matnr; 
 | 
import com.vincent.rsf.server.manager.service.MatnrGroupService; 
 | 
import com.vincent.rsf.server.manager.service.MatnrService; 
 | 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 
 | 
import com.vincent.rsf.server.system.constant.SerialRuleCode; 
 | 
import com.vincent.rsf.server.system.service.FieldsService; 
 | 
import com.vincent.rsf.server.system.utils.SerialRuleUtils; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.stereotype.Service; 
 | 
import org.springframework.transaction.annotation.Transactional; 
 | 
import org.springframework.web.multipart.MultipartFile; 
 | 
  
 | 
import java.util.ArrayList; 
 | 
import java.util.List; 
 | 
import java.util.Map; 
 | 
import java.util.Objects; 
 | 
import java.util.stream.Collectors; 
 | 
  
 | 
@Service("matnrService") 
 | 
public class MatnrServiceImpl extends ServiceImpl<MatnrMapper, Matnr> implements MatnrService { 
 | 
  
 | 
    @Autowired 
 | 
    private MatnrGroupService matnrGroupService; 
 | 
  
 | 
    @Autowired 
 | 
    private FieldsService fieldsService; 
 | 
  
 | 
    /** 
 | 
     * @return 
 | 
     * @throws 
 | 
     * @author Ryan 
 | 
     * @description 物料数据导入接处理实现 
 | 
     * @time 2025/3/3 13:08 
 | 
     */ 
 | 
    @Override 
 | 
    @Transactional(rollbackFor = Exception.class) 
 | 
    public R importExcels(MultipartFile file) throws Exception { 
 | 
        //读取上传文件内容 
 | 
        ExcelImportResult result = ExcelImportUtil.importExcelMore(file.getInputStream(), MatnrsTemplate.class, ExcelUtil.getDefaultImportParams()); 
 | 
        if (result.getList().isEmpty()) { 
 | 
            throw new CoolException("物料导入失败!!"); 
 | 
        } 
 | 
  
 | 
        List<MatnrsTemplate> list = result.getList(); 
 | 
        /** 
 | 
         * 获取导入物料模板 
 | 
         */ 
 | 
        List<Matnr> matnrs = getMatnrTempletes(list); 
 | 
  
 | 
        if (!this.saveBatch(matnrs)) { 
 | 
            throw new CoolException("导入失败!!"); 
 | 
        } 
 | 
        return R.ok("导入成功!!"); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 获取物料模板信息 
 | 
     * @param list 
 | 
     * @return 
 | 
     */ 
 | 
    @Override 
 | 
    public List<Matnr> getMatnrTempletes(List<MatnrsTemplate> list) { 
 | 
        List<Matnr> matnrs = new ArrayList<>(); 
 | 
        list.forEach(template -> { 
 | 
            Matnr matnr = new Matnr(); 
 | 
            matnr.setBarcode(template.getBarcode()) 
 | 
                    .setCode(template.getCode()) 
 | 
                    .setName(template.getName()) 
 | 
                    .setId(null) 
 | 
                    .setDescrible(template.getDescrible()) 
 | 
                    .setColor(template.getColor()) 
 | 
                    .setFlagCheck(!Objects.isNull(template.getFlagCheck()) ? Short.parseShort(template.getFlagCheck()) : 0) 
 | 
                    .setWeight(!Objects.isNull(template.getWeight()) ? Double.parseDouble(template.getWeight()) : 0.0) 
 | 
                    .setValidWarn(!Objects.isNull(template.getValidWarn()) ? Integer.parseInt(template.getValidWarn()): 0) 
 | 
                    .setValid(!Objects.isNull(template.getValid()) ? Integer.parseInt( template.getValid()) : 0) 
 | 
                    .setUnit(template.getUnit()) 
 | 
                    .setStockUnit(template.getPurUnit()) 
 | 
                    .setSize(template.getSize()) 
 | 
                    .setSpec(template.getSpec()) 
 | 
                    .setStagn(!Objects.isNull(template.getStagn()) ? Integer.parseInt(template.getStagn()) : 0) 
 | 
                    .setModel(template.getModel()) 
 | 
                    .setGroupCode(template.getGroupCode()) 
 | 
                    .setPurUnit(template.getPurUnit()) 
 | 
                    .setStockLevel(!Objects.isNull(template.getStockLevel()) ? MatnrLocLevel.getTypeVal(template.getStockLevel()) : null) 
 | 
                    .setSafeQty(!Objects.isNull(template.getSafeQty()) ? Double.parseDouble(template.getSafeQty()) : 0) 
 | 
                    .setMinQty(!Objects.isNull(template.getMinQty()) ? Double.parseDouble(template.getMinQty()) : 0); 
 | 
            if (!Objects.isNull(template.getGroupCode()) || !Objects.isNull(template.getGroupName())) { 
 | 
                MatnrGroup matnrGroups = matnrGroupService.getOne(new LambdaQueryWrapper<MatnrGroup>() 
 | 
                        .eq(!Objects.isNull(template.getGroupCode()), MatnrGroup::getCode, template.getGroupCode()) 
 | 
                        .eq(!Objects.isNull(template.getGroupName()), MatnrGroup::getName, template.getGroupName())); 
 | 
                if (!Objects.isNull(matnrGroups)) { 
 | 
                    matnr.setGroupId(matnrGroups.getId()).setGroupCode(matnrGroups.getCode()); 
 | 
                } 
 | 
            } 
 | 
//            List<Fields> fields = fieldsService.list(new LambdaQueryWrapper<Fields>() 
 | 
//                    .eq(Fields::getUnique, CommonStatus.COMMONSTATUS_NO.val) 
 | 
//                    .eq(Fields::getFlagEnable, CommonStatus.COMMONSTATUS_YES.val)); 
 | 
// 
 | 
//            //获取动态字段,并保存明细内容 
 | 
//            if (!fields.isEmpty()) { 
 | 
//                //保存物料扩展属性值 
 | 
//                try { 
 | 
//                    Map<String, Object> map = JSONObject.parseObject(JSONObject.toJSONString(template), Map.class); 
 | 
//                    String uuid = CommonUtil.randomUUID16(); 
 | 
//                    boolean b = FieldsUtils.saveFields(map, uuid); 
 | 
//                    if (b) { 
 | 
//                        matnr.setFieldsIndex(uuid); 
 | 
//                    } 
 | 
//                } catch (Exception e) { 
 | 
//                    throw new RuntimeException(e); 
 | 
//                } 
 | 
//            } 
 | 
            matnrs.add(matnr); 
 | 
        }); 
 | 
        return matnrs; 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public PageParam<Matnr, BaseParam> getMatnrPage(PageParam<Matnr, BaseParam> pageParam, Map<String, Object> params) { 
 | 
        List<Long> longs = new ArrayList<>(); 
 | 
        if (params.containsKey("groupId")) { 
 | 
            Object groupId = params.get("groupId"); 
 | 
            MatnrGroup group = matnrGroupService.getById(groupId.toString()); 
 | 
            if (null != group){ 
 | 
                pageParam.getWhere().getMap().remove("groupId"); 
 | 
                if(!group.getCode().equals("01")){ 
 | 
                    List<MatnrGroup> matnrGroups = matnrGroupService.list(new LambdaQueryWrapper<MatnrGroup>() 
 | 
                            .eq(MatnrGroup::getParentId, Long.parseLong(groupId.toString())) 
 | 
                            .select(MatnrGroup::getId)); 
 | 
                    if (!matnrGroups.isEmpty()) { 
 | 
                        longs = matnrGroups.stream().map(MatnrGroup::getId).collect(Collectors.toList()); 
 | 
  
 | 
                    } 
 | 
                    longs.add(group.getId()); 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        QueryWrapper<Matnr> queryWrapper = pageParam.buildWrapper(true); 
 | 
        queryWrapper.in(!longs.isEmpty(),"group_id", longs); 
 | 
  
 | 
        FieldsUtils.setFieldsFilters(queryWrapper,pageParam,Matnr.class); 
 | 
        /**拼接扩展字段*/ 
 | 
        PageParam<Matnr, BaseParam> page = this.page(pageParam, queryWrapper); 
 | 
        List<Matnr> records = page.getRecords(); 
 | 
        for (Matnr record : records) { 
 | 
            if (!Objects.isNull(record.getFieldsIndex())) { 
 | 
                Map<String, String> fields = FieldsUtils.getFields(record.getFieldsIndex()); 
 | 
                record.setExtendFields(fields); 
 | 
            } 
 | 
        } 
 | 
        page.setRecords(records); 
 | 
        return page; 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public Matnr selectMatnrById(Long id) { 
 | 
        Matnr matnr = this.baseMapper.selectById(id); 
 | 
        if (Objects.isNull(matnr)) { 
 | 
            throw new CoolException("当前物料不存在!!"); 
 | 
        } 
 | 
        if (!Objects.isNull(matnr.getFieldsIndex())) { 
 | 
            Map<String, String> fields = FieldsUtils.getFields(matnr.getFieldsIndex()); 
 | 
            matnr.setExtendFields(fields); 
 | 
        } 
 | 
        return matnr; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 物料绑定 
 | 
     * @param params 
 | 
     * @return 
 | 
     */ 
 | 
    @Override 
 | 
    public boolean bindMatnrs(MatnrToGroupParams params) { 
 | 
        if (Objects.isNull(params) && params.getIds().isEmpty()) { 
 | 
            throw new CoolException("物料不能为空!!"); 
 | 
        } 
 | 
        if (Objects.isNull(params.getGroupId())) { 
 | 
            throw new CoolException("分组不能为空!!"); 
 | 
        } 
 | 
        MatnrGroup groups = matnrGroupService.getOne(new LambdaQueryWrapper<MatnrGroup>().eq(MatnrGroup::getId, params.getGroupId())); 
 | 
        if (Objects.isNull(groups)) { 
 | 
            throw new CoolException("物料分组不存在!!"); 
 | 
        } 
 | 
        if (!this.update(new LambdaUpdateWrapper<Matnr>() 
 | 
                .in(Matnr::getId, params.getIds()) 
 | 
                .set(Matnr::getGroupId, groups.getId()) 
 | 
                .set(Matnr::getGroupCode, groups.getCode()))) { 
 | 
            throw new CoolException("绑定失败!!"); 
 | 
        } 
 | 
        return true; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 批量修改物料 
 | 
     * @param params 
 | 
     * @return 
 | 
     */ 
 | 
    @Override 
 | 
    public boolean batchUpdate(MatnrToGroupParams params) { 
 | 
        if (Objects.isNull(params.getMatnr())) { 
 | 
            throw new CoolException("物料属性不能为空!!"); 
 | 
        } 
 | 
        if (Objects.isNull(params.getIds()) || params.getIds().isEmpty()) { 
 | 
            throw new CoolException("所选物料不能为空!!"); 
 | 
        } 
 | 
        Matnr matnr = params.getMatnr(); 
 | 
        if (!this.update(new LambdaUpdateWrapper<Matnr>().in(Matnr::getId, params.getIds()) 
 | 
                .set(!Objects.isNull(matnr.getStatus()), Matnr::getStatus, matnr.getStatus()) 
 | 
                .set(!Objects.isNull(matnr.getFlagCheck()), Matnr::getFlagCheck, matnr.getFlagCheck()) 
 | 
                .set(!Objects.isNull(matnr.getFlagLabelMange()), Matnr::getFlagLabelMange, matnr.getFlagLabelMange()) 
 | 
                .set(!Objects.isNull(matnr.getStockLevel()), Matnr::getStockLevel, matnr.getStockLevel()) 
 | 
                .set(!Objects.isNull(matnr.getShipperId()), Matnr::getShipperId, matnr.getShipperId()) 
 | 
                .set(!Objects.isNull(matnr.getMinQty()), Matnr::getMinQty, matnr.getMinQty()) 
 | 
                .set(!Objects.isNull(matnr.getMaxQty()), Matnr::getMaxQty, matnr.getMaxQty()) 
 | 
                .set(!Objects.isNull(matnr.getValid()), Matnr::getValid, matnr.getValid()) 
 | 
                .set(!Objects.isNull(matnr.getValidWarn()), Matnr::getValidWarn, matnr.getValidWarn()) 
 | 
                .set(!Objects.isNull(matnr.getStockUnit()), Matnr::getStockUnit, matnr.getStockUnit()) 
 | 
        )) { 
 | 
            throw new CoolException("批量操作失败!!"); 
 | 
        } 
 | 
  
 | 
        return true; 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public PageParam<Matnr, BaseParam> getPages(Map<String, Object> map) { 
 | 
  
 | 
  
 | 
        return null; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * @desc 更新扩展物料扩展字段值 
 | 
     * @param matnr 
 | 
     * @return 
 | 
     */ 
 | 
    @Override 
 | 
    @Transactional(rollbackFor = Exception.class) 
 | 
    public R saveMatnrs(Map<String, Object> matnr) { 
 | 
        Matnr matnr1 = JSONObject.parseObject(JSONObject.toJSONString(matnr), Matnr.class); 
 | 
        if (Objects.isNull(matnr1.getCode())) { 
 | 
            String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_MATNR_CODE, null); 
 | 
            matnr1.setCode(ruleCode); 
 | 
        } 
 | 
        /** 
 | 
         * 扩展字段存入库 
 | 
         */ 
 | 
        String uuid16 = CommonUtil.randomUUID16(); 
 | 
        if (!FieldsUtils.getFieldsSta().isEmpty()) { 
 | 
//            Map<String, ?> extendFields = (Map<String, ?>) matnr.get("extendFields"); 
 | 
            try { 
 | 
                FieldsUtils.saveFields(matnr, uuid16); 
 | 
                matnr1.setFieldsIndex(uuid16); 
 | 
            } catch (Exception ex) { 
 | 
                log.error(ex.toString()); 
 | 
                throw new CoolException("动态字段保存失败"); 
 | 
            } 
 | 
        } 
 | 
        if (!this.saveOrUpdate(matnr1)) { 
 | 
            return R.error("Save Fail"); 
 | 
        } 
 | 
        return R.ok(matnr1); 
 | 
    } 
 | 
} 
 |