/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.youchain.businessdata.service.impl;

import com.alibaba.fastjson.JSON;
import com.sun.jna.platform.win32.WinDef;
import com.youchain.basicdata.domain.Area;
import com.youchain.basicdata.domain.Item;
import com.youchain.basicdata.domain.Point;
import com.youchain.basicdata.domain.Stock;
import com.youchain.basicdata.service.PointService;
import com.youchain.businessdata.domain.*;
import com.youchain.businessdata.inputJson.buttenJson.InvYW;
import com.youchain.businessdata.inputJson.buttenJson.InventoryButton;
import com.youchain.businessdata.inputJson.buttenJson.dataAll.InventoryYW;
import com.youchain.businessdata.repository.PickDetailRepository;
import com.youchain.businessdata.returnJson.RInvQuery;
import com.youchain.businessdata.returnJson.RPTaskList;
import com.youchain.businessdata.returnJson.XdInventory;
import com.youchain.businessdata.service.InventoryLogService;
import com.youchain.businessdata.service.dto.*;
import com.youchain.businessdata.service.mapstruct.InventoryQQMapper;
import com.youchain.exception.BadRequestException;
import com.youchain.modules.system.domain.Dept;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import com.youchain.businessdata.repository.InventoryRepository;
import com.youchain.businessdata.service.InventoryService;
import com.youchain.businessdata.service.mapstruct.InventoryMapper;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.JsonUtil;
import org.hibernate.SQLQuery;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.math.BigInteger;
import java.util.*;
import java.io.IOException;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletResponse;

/**
 * @author huojin
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2023-08-22
 **/
@Service
@RequiredArgsConstructor
@Slf4j
public class InventoryServiceImpl implements InventoryService {

    private final InventoryRepository inventoryRepository;
    private final InventoryMapper inventoryMapper;
    private final InventoryQQMapper inventoryQQMapper;

    private final EntityManager entityManager;
    private final InventoryLogService invLogService;
    private final PointService pointService;
    private final PickDetailRepository pickDetailRepository;

    @Override
    public Map<String, Object> queryAll(InventoryQueryCriteria criteria, Pageable pageable) {
        Page<Inventory> page = inventoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(inventoryMapper::toDto));
    }

    @Override
    public List<InventoryDto> queryAll(InventoryQueryCriteria criteria) {
        return inventoryMapper.toDto(inventoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    public List<InventoryDto> queryAll(InvQueryCriteria criteria) {
        return inventoryMapper.toDto(inventoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    @Transactional
    public InventoryDto findById(Long id) {
        Inventory inventory = inventoryRepository.findById(id).orElseGet(Inventory::new);
        ValidationUtil.isNull(inventory.getId(), "Inventory", "id", id);
        return inventoryMapper.toDto(inventory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public InventoryDto create(Inventory resources) {
        return inventoryMapper.toDto(inventoryRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Inventory resources) {
        Inventory inventory = inventoryRepository.findById(resources.getId()).orElseGet(Inventory::new);
        ValidationUtil.isNull(inventory.getId(), "Inventory", "id", resources.getId());
        inventory.copy(resources);
        inventoryRepository.save(inventory);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            inventoryRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<InventoryDto> all, HttpServletResponse response) throws Exception {



        List<Map<String, Object>>  list=ExcelDownUtils.CreateMap(all,"Inventory");
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void downloadXb(List<InventoryDto> all, HttpServletResponse response) throws Exception {
        List<Map<String, Object>>  list=ExcelDownUtils.CreateMap(all,"InvXb");
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void downloadXt(List<InventoryDto> all, HttpServletResponse response) throws Exception {
        List<Map<String, Object>>  list=ExcelDownUtils.CreateMap(all,"InvXt");
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void downloadZz(List<InventoryQueryDto> all, HttpServletResponse response) throws Exception {
        List<Map<String, Object>>  list=ExcelDownUtils.CreateMap(all,"InvZz");
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<Inventory> queryInventoryAllocate(long itemId, long areaId,Long zzkwId,Long pointId,String stockCode,String propC2,String type) {
        String hql = " from Inventory inv where 1=1 " +
                " and inv.itemKey.item.id=" + itemId + " " +
                " and inv.quantity-inv.queuedQty>0 and inv.quantity>0 and inv.point.code !='BH_ZC'";
        if(zzkwId!=null){
            hql+=" and inv.area.id=" + areaId + " ";
            hql+=" and inv.zzkw.id="+zzkwId;
            hql+="and inv.point.type in ('"+BaseStatus.HCKW+"','"+BaseStatus.ZZKW+"')";
        }
        if(pointId!=null){
            hql+=" and inv.point.id="+pointId;
        }else{
            hql+="and inv.point.type='"+BaseStatus.CH+"' ";
        }
        if(type.equals(BaseStatus.ZZJL)) {
            hql+="and inv.point.type='"+BaseStatus.CH+"'";
        }
        if(stockCode!=null){
            hql += " and inv.stockCode='" + stockCode + "'";
        }else{
            if(type.equals(BaseStatus.ZCCK)) {//非箱单出库需出无箱单品番，其他出库只需要满足库存即可出库
                hql += " and inv.stockCode is null";
            }
        }
        if(propC2!=null){
            hql+=" and inv.itemKey.propC2='"+propC2+"'";
        }
        hql+=" and inv.area.code not like '%盘点%'";
        hql+=" and inv.area.code not like '%临时%'";
        hql+= " order by inv.itemKey.propC1 asc,inv.point.bqlx asc,inv.stockCode asc  ";
        Query query = entityManager.createQuery(hql);
        List<Inventory> inventoryList = query.getResultList();
        return inventoryList;
    }
    @Override
    public List<InventoryQueryDto> queryInvAll(InventoryQueryCriteria criteria) {
        //return inventoryQQMapper.toDto(inventoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
        Query query = getQuery(criteria,BizStatus.QUERY_PAGE);
        List  queryResultList = query.unwrap(SQLQuery.class)
                .setResultTransformer(
                        AliasToEntityMapResultTransformer.INSTANCE
                )
                .list();
        List<InventoryQueryDto> list = JSON.parseArray(JSON.toJSONString(queryResultList),InventoryQueryDto.class);
//        List<InventoryQueryDto> list=new ArrayList<>();
//        for (InventoryQueryDto obj: queryResultList) {
//            InventoryQueryDto dto=new InventoryQueryDto();
//            dto.setId(obj.getId());
//            dto.setItem_code(obj.getItem_code());
//            dto.setItem_name(obj.getItem_name());
//            dto.setArea_name(obj.getArea_name());
//            dto.setPoint_code(obj.getPoint_code());
//            dto.setProp_c2(obj.getProp_c2());
//            dto.setQuantity(obj.getQuantity());
//            dto.setInv_min(obj.getInv_min());
//            dto.setKcyj_qty(obj.getKcyj_qty());
//            dto.setQuantity(obj.getQuantity());
//            list.add(dto);
//        }
        return list;
    }

    @Override
    public Map<String,Object> queryInvAll(InventoryQueryCriteria criteria, Pageable pageable) {
        //库存查询
        int pageNum=pageable.getPageNumber();//当前页
        int pageSize=pageable.getPageSize();//条数
        Query queryTotal = getQuery(criteria,BizStatus.QUERY_TOTAL);
        List l=queryTotal.getResultList();
        int pageCount=((BigInteger)l.get(0)).intValue();
        Query queryPage = getQuery(criteria,BizStatus.QUERY_PAGE);
        queryPage.setFirstResult((pageNum)*pageSize);
        queryPage.setMaxResults((pageNum+1)*pageSize);
        long st=System.currentTimeMillis();
        List<InventoryQueryDto> queryResultList = queryPage.unwrap(SQLQuery.class)
                .setResultTransformer(
                        AliasToEntityMapResultTransformer.INSTANCE
                )
                .list();
        log.error("查询制造库存用时："+(System.currentTimeMillis()-st));
        Map<String,Object> map=new HashMap<>();
        map.put("content",queryResultList);
        map.put("totalElements",pageCount);
        return map;
    }

    public Query getQuery(InventoryQueryCriteria criteria,String type){
        String midSql="SELECT inv.id,area.name area_name,p.code point_code,p.zflx point_zflx,it.code item_code,it.name item_name," +
                " inv.quantity ,inv.queued_qty,ik.prop_c2 ,p.inv_min,IF(inv.quantity<p.inv_min,(p.inv_min-inv.quantity),'') kcyj_qty " ;
        if(type.equals(BizStatus.QUERY_TOTAL)){
            midSql="SELECT count(inv.id) ";
        }
        String sql =midSql+" " +
                " from data_inventory inv\n" +
                "left join base_point p on p.id=inv.point_id\n" +
                "left join base_area area on area.id=inv.area_id\n" +
                "left join data_item_key ik on ik.id=inv.item_key_id\n" +
                "left join base_item it on it.id=ik.item_id" +
                " where 1=1 and inv.quantity!=0 " ;
        if(criteria!=null) {
            String item_code=criteria.getItemCodeOne();
            String item_name=criteria.getItemName();
            String point_code=criteria.getPointCode();
            String prop_c2=criteria.getBonded();
            String point_type=criteria.getPointType();
            String area_name=criteria.getAreaName();
            Boolean kcyj=criteria.getKcyj();
            Double quantity2=criteria.getQuantity2();
            if (item_code!=null &&!item_code.equals("") ) {
                sql += " and it.code like '%" + item_code + "%'";
            }
            if (item_name!=null &&!item_name.equals("") ) {
                sql += " and it.name like '%" + item_code + "%'";
            }
            if (point_code!=null &&!point_code.equals("") ) {
                sql += " and p.code like '%" + point_code + "%'";
            }
            if (point_type!=null &&!point_type.equals("") ) {
                sql += " and p.type='" + point_type + "'";
            }
            if (area_name!=null &&!area_name.equals("") ) {
                sql += " and area.name='" + area_name + "'";
            }
            if (prop_c2!=null &&!prop_c2.equals("") ) {
                sql += " and it.name like '%" + item_code + "%'";
            }
            if(kcyj!=null&&kcyj){
                sql += " and inv.quantity<p.inv_min ";
            }
            if(quantity2!=null){
                sql += " and inv.quantity<"+quantity2;
            }
        }
        sql+=" order by it.code ";
        Query query = entityManager.createNativeQuery(sql);
        return query;
    }

    public Double getInvQty(long itemId, long areaId,Long zzkwId) {
        String hql = "select sum(inv.quantity-inv.queuedQty) from Inventory inv where 1=1 " +
                " and inv.itemKey.item.id=" + itemId + " " +
                " and inv.area.id=" + areaId + " " +
                " and inv.quantity>0 ";
        if(zzkwId!=null){
            hql+=" and inv.zzkw.id="+zzkwId;
            hql+="and inv.point.type in ('"+BaseStatus.HCKW+"','"+BaseStatus.ZZKW+"')";
        }else{
            hql+="and inv.point.type='"+BaseStatus.CH+"'";
        }
        Query query = entityManager.createQuery(hql);
        List inventoryList = query.getResultList();
        if(inventoryList.size()>0){
            Object obj=inventoryList.get(0);
            if(obj==null){
                return 0d;
            }else{
                return (Double)obj;
            }
        }
        return 0d;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Inventory getInventory(ItemKey itemKey, Area area, Point point, Point zzkw,Dept dept, String type,String stockCode) {
        //生成Inventory
        String hql = " from Inventory inv where 1=1 ";
//        if(type.equals(BizStatus.SL_ALL)||type.equals(BizStatus.ZZKW_TL)||type.equals(BizStatus.YK_JS)){
//            if(zzkw==null){
//                throw new BadRequestException(HttpStatus.NOT_FOUND, "制造库位必填");
//            }
//            if(!area.getBexb()){
//                throw new BadRequestException(HttpStatus.NOT_FOUND, area.getCode()+"接收库区必须为线边仓");
//            }
//            if(area.getPoint()==null){
//            if(area.getPointId()==null){
//                throw new BadRequestException(HttpStatus.NOT_FOUND, "接收库区线边库位必填");
//            }
//        }
        if(type.equals(BizStatus.SL_ALL)){
            //取库区默认
            hql += " and inv.point.id=" + area.getPointId() + " ";
        }else{
            if (point != null) {
                hql += " and inv.point.id=" + point.getId() + " ";
            }
        }
        if (itemKey != null) {
            hql += " and inv.itemKey.id=" + itemKey.getId() + " ";
        }
        hql += " and inv.area.id=" + area.getId() + " ";
        if (dept != null) {
            hql += " and inv.dept.id=" + dept.getId() + " ";
        }
//        if(area.getBexb()) {
//            if (zzkw != null) {
//                hql += " and inv.zzkw.id=" + zzkw.getId() + " ";
//            }else{
//                throw new BadRequestException(HttpStatus.NOT_FOUND, "线边仓制造库位必填");
//            }
//        }
        if(stockCode!=null){
            hql += " and inv.stockCode='" + stockCode + "' ";
        } else {
            hql += " and inv.stockCode is null";
        }

        Query query = entityManager.createQuery(hql);
        List<Inventory> inventoryList = query.getResultList();
        Inventory inventory = null;
        if (inventoryList.size() > 0) {
            inventory = inventoryList.get(0);
        } else {
            //创建Inventory
            if (type.equals(BizStatus.ZZKW_MOVE) ||type.equals(BizStatus.ZZKW_TL) ||type.equals(BizStatus.RECEIVING_UP) || type.equals(BizStatus.MOVE)||type.equals(BizStatus.PICK_DOWN)||type.equals(BizStatus.PICK_CANCEL)||type.equals(BizStatus.SL_ALL)||type.equals(BizStatus.COUNT_MOVE)||type.equals(BizStatus.YK_JS)||type.equals(BizStatus.TC_RK)||type.equals(BizStatus.TC_XX)) {
                inventory = new Inventory();
                inventory.setItemKey(itemKey);
                inventory.setPoint(point);
                inventory.setPointCode(point.getCode());
//                if(zzkw==null){
//                    zzkw=pointService.getPoint("ZZKW",null,null,null);
//                }
                inventory.setStockCode(stockCode);
//                    inventory.setZzkw(zzkw);
//                inventory.setStock(stock);
//                inventory.setStockCode(stock.getCode());
                inventory.setDept(dept);
                inventory.setArea(area);
                create(inventory);
            }
        }
        return inventory;
    }


    @Override
    public List<Inventory> getXbHyInv(Item item, Area area, Point zzkw,String type,String big_item_code) {
        //生成Inventory
        String hql = " from Inventory inv where 1=1 and inv.point.type not in ('"+BaseStatus.BHZC+"','"+BaseStatus.SHZC+"')";
        if (item != null) {
            hql += " and inv.itemKey.item.id=" + item.getId() + " ";
        }
        if(area!=null) {
            hql += " and inv.area.id=" + area.getId()  ;
        }
        if(type.equals("耗用")||type.equals("台车下线")){//直接制造库位库存耗用
            hql += " and inv.point.id=" + zzkw.getId();
        }
        if(big_item_code!=null){
            hql += " and inv.stockCode='" + big_item_code+"'"  ;
        }
        hql+=" order by inv.itemKey.propC1 asc";
        Query query = entityManager.createQuery(hql);
        List<Inventory> inventoryList = query.getResultList();
        return  inventoryList;
    }

    @Override
    public List<Inventory> getInventoryXbTl(Item item, Area area,Point point,Point zzkw) {
        //生成Inventory
        String hql = " from Inventory inv where 1=1 and inv.point.id ="+point.getId();
        hql += " and inv.itemKey.item.id=" + item.getId() + " ";
        hql += " and inv.area.id="+area.getId();
        hql += " and inv.zzkw.id=" + zzkw.getId();
        Query query = entityManager.createQuery(hql);
        List<Inventory> inventoryList = query.getResultList();
        return  inventoryList;
    }

    @Override
    public List<Inventory> queryInventory(Stock stock) {
        String hql = " from Inventory inv where inv.stock.id=" + stock.getId() + "  and inv.quantity>0 ";
        Query query = entityManager.createQuery(hql);
        List<Inventory> inventoryList = query.getResultList();
        return inventoryList;
    }

    public List<Inventory> queryInventory(Long areaId, Long pointId, Long itemKeyId) {
        String hql = " from Inventory inv where inv.area.id=" + areaId + "  and inv.point.id=" + pointId + " and inv.itemKey.id=" + itemKeyId;
        Query query = entityManager.createQuery(hql);
        List<Inventory> inventoryList = query.getResultList();
        return inventoryList;
    }

    @Override
    public List<Inventory> getInvForPlan(String type, Long areaId, Long itemId, Long deptId) {
        String hql = " from Inventory inv where 1=1 and inv.queuedQty<=0 and inv.quantity>=0";
        if (type.equals(BizStatus.ZONE)) {
            hql += " and inv.point.area.id=" + areaId;
        } else if (type.equals(BizStatus.ITEM)) {
            hql += " and inv.itemKey.item.id=" + itemId;
        } else if (type.equals(BizStatus.WAREHOUSE)) {
            hql += " and 1=1";
        }
        Query query = entityManager.createQuery(hql);
        List<Inventory> list = query.getResultList();
        return list;
    }

    @Override
    public void kuneMovePosition(InvYW invYW) {
        Inventory srcInv = inventoryRepository.getById(invYW.getId());
        if(srcInv.getStockCode()!=null){
            throw new BadRequestException( "箱单库存需使用箱单移位功能");
        }
        Point dstPoint=invYW.getDstPoint();
        // 位移数量
        double rmNumber = invYW.getRmNumber();
        //判断 库存-要移动的数量
        double rmNum = srcInv.getQuantity() - rmNumber;
        if(srcInv.getPoint().getCode().equals(invYW.getDstPoint().getCode())){
            throw new BadRequestException( "库位不能一致");
        }
        if (rmNum >= 0) {//移除数量小于库存数量
            //生成库存或生成记录
            Inventory dstInv = this.getInventory(srcInv.getItemKey(), dstPoint.getArea(), dstPoint, srcInv.getZzkw(),  srcInv.getDept(), BizStatus.MOVE,srcInv.getStockCode());
            //生成移除的日志
            invLogService.storeInventoryLog(BizStatus.MOVE, BizStatus.REDUCE, null, srcInv.getArea(), srcInv.getItemKey(), srcInv.getPoint(), invYW.getDstPoint(), srcInv.getStock(), srcInv.getStock(), srcInv.getQuantity(), rmNumber,
                    srcInv.getStockCode(),null, BizStatus.MOVE, srcInv.getId(), srcInv.getId(), srcInv.getDescription());
            //生成目标点位的日志
            invLogService.storeInventoryLog(BizStatus.MOVE, BizStatus.ADD, null, dstPoint.getArea(), dstInv.getItemKey(), dstPoint, dstPoint, dstInv.getStock(), dstInv.getStock(), dstInv.getQuantity(), rmNumber,
                    dstInv.getStockCode(),null, BizStatus.MOVE, dstInv.getId(), dstInv.getId(), dstInv.getDescription());
            //减库存
            srcInv.setQuantity(rmNum);
            this.update(srcInv);
            //加库存
            dstInv.setQuantity(dstInv.getQuantity() + rmNumber);
            this.update(dstInv);
        } else if (rmNum < 0) {//移除数量大于库存数量
            throw new BadRequestException(HttpStatus.NOT_FOUND, "移除数量大于库存数量");
        }
    }

    public List<Object[]> queryItemStock() {
        String hql = "select inv.itemKey.item.code,max(inv.itemKey.item.name),count(inv.id) " +
                " from Inventory inv where 1=1 and inv.quantity>0 group by inv.itemKey.item.code order by inv.itemKey.item.code";
        Query query = entityManager.createQuery(hql);
        List<Object[]> list = query.getResultList();
        return list;
    }

    public List<RInvQuery> queryInvApp(String code,String pointCode) {
        String sql = "SELECT inv.id invId,it.`code` itemCode,it.name itemName,inv.quantity qty,ik.prop_C1 propC1,ik.prop_C2 propC2,p.code pointCode FROM `data_inventory` inv \n" +
                "left join data_item_key ik on ik.id=inv.item_key_id\n" +
                "left join base_item it on it.id=ik.item_id\n" +
                "left join base_point p on p.id=inv.point_id \n" +
                "left join base_area area on area.id=inv.area_id\n" +
                "where 1=1 and area.besh=1 and p.code!='BH_ZC' and inv.quantity!=0 " ;
        if(code!=null&&!code.equals("")){
            sql+=" and it.code='"+code+"'";
        }
        if(pointCode!=null&&!pointCode.equals("")){
            sql+=" and p.code='"+pointCode+"'";
        }
                sql+=" order by it.code";
        List ts= entityManager.createNativeQuery(sql)
                .unwrap(SQLQuery.class)
                .setResultTransformer(
                        AliasToEntityMapResultTransformer.INSTANCE
                )
                .list();
        List<RInvQuery> list = JSON.parseArray(JSON.toJSONString(ts),RInvQuery.class);
        return list;
    }

    public List<XdInventory> queryInvForPickDetail(Long pickDetailId) {
        PickDetail pickDetail = pickDetailRepository.getById(pickDetailId);
        Item item = pickDetail.getItem();
        List<XdInventory> xds=new ArrayList<>();
        if (pickDetail.getOrderQty() > pickDetail.getPickedQty()) {
            Long zzkwId = null;
            Long areaId = 0L;
            this.queryInventoryAllocate(item.getId(), areaId,zzkwId,null,pickDetail.getPo(),pickDetail.getPropC2(), BaseStatus.ZZJL);
            List<Inventory> invs =this.queryInventoryAllocate(item.getId(), areaId,zzkwId,null,pickDetail.getPo(),pickDetail.getPropC2(), BaseStatus.ZZJL);
            for(Inventory inv:invs){
                XdInventory xd=new XdInventory();
                xd.setInvId(inv.getId());
                xd.setPickDetailId(pickDetailId);
                xd.setItemCode(item.getCode());
                xd.setItemName(item.getName());
                xd.setStockCode(inv.getStockCode());
                xd.setPointCode(inv.getPointCode());
                xd.setBonded(pickDetail.getPropC2());
                xd.setPlanQty(pickDetail.getOrderQty().intValue()+"");
                xd.setInvQty(inv.getQuantity().intValue()+"");
                xd.setPickQty(pickDetail.getPickedQty().intValue()+"");
                xd.setPropC1(inv.getItemKey().getPropC1());
                xds.add(xd);
            }
        }
        return xds;
    }

}
