/*
*  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.basicdata.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import com.alibaba.fastjson.JSON;
import com.youchain.appupdate.ReturnJson.RLocLayout;
import com.youchain.basicdata.domain.*;
import com.youchain.basicdata.repository.AreaRepository;
import com.youchain.basicdata.repository.ItemRepository;
import com.youchain.basicdata.repository.PointRepository;
import com.youchain.basicdata.service.BomAccountLogService;
import com.youchain.basicdata.service.PointService;
import com.youchain.basicdata.service.dto.*;
import com.youchain.businessdata.domain.Inventory;
import com.youchain.businessdata.domain.InventoryLog;
import com.youchain.businessdata.domain.ItemKey;
import com.youchain.businessdata.domain.Spareparts;
import com.youchain.businessdata.returnJson.BomPrint;
import com.youchain.businessdata.returnJson.TcOutList;
import com.youchain.businessdata.returnJson.ZzjlPickList;
import com.youchain.businessdata.service.InventoryLogService;
import com.youchain.businessdata.service.InventoryService;
import com.youchain.businessdata.service.ItemKeyService;
import com.youchain.exception.BadRequestException;
import com.youchain.modules.system.domain.Dict;
import com.youchain.modules.system.domain.DictDetail;
import com.youchain.modules.system.service.DictDetailService;
import com.youchain.modules.system.service.dto.DictDto;
import com.youchain.modules.system.service.dto.DictQueryCriteria;
import com.youchain.modules.system.service.impl.DictDetailServiceImpl;
import com.youchain.modules.system.service.impl.DictServiceImpl;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import com.youchain.basicdata.repository.BomAccountRepository;
import com.youchain.basicdata.service.BomAccountService;
import com.youchain.basicdata.service.mapstruct.BomAccountMapper;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.SQLQuery;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
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.sql.Timestamp;
import java.util.*;
import java.io.IOException;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author baobinglin
* @date 2024-01-02
**/
@Service
@Slf4j
@RequiredArgsConstructor
@CacheConfig(cacheNames = "bomAccount")
public class BomAccountServiceImpl implements BomAccountService {

    private final BomAccountRepository bomAccountRepository;
    private final PointRepository pointRepository;
    private final AreaRepository areaRepository;
    private final ItemRepository itemRepository;
    private final BomAccountMapper bomAccountMapper;
    private final PointService pointService;
    private final EntityManager entityManager;
    private final RedisUtils redisUtils;
    private final BomAccountLogService bomAccountLogService;
    private final InventoryService inventoryService;
    private final InventoryLogService inventoryLogService;
    private final ItemKeyService itemKeyService;

    @Override
//    @Cacheable(key = "'queryALL1:' + #p0")
    public Map<String,Object> queryAll(BomAccountQueryCriteria criteria, Pageable pageable){
        Page<BomAccount> page = bomAccountRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(bomAccountMapper::toDto));
    }

    @Override
//    @Cacheable(key = "'queryALL2:' + #p0")
    public List<BomAccountDto> queryAll(BomAccountQueryCriteria criteria){
        return bomAccountMapper.toDto(bomAccountRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    public List<BomPrint> queryPrintAll(BomAccountQueryCriteria criteria,String dxw){
        List<BomPrint> bomPrints=bomAccountRepository.queryPrintAll(criteria.getBigItemCode(),criteria.getItemCode(),criteria.getZPointCode(),
                criteria.getNs(),criteria.getRAreaCode(),criteria.getBp_type(),criteria.getTckw(),criteria.getSupplier(),dxw);
        return bomPrints;
    }

    public List<BomPrint> queryPrintAll(BomAccountQueryCriteria criteria){
        String hws2=null;
        List<String> hws1= criteria.getZPointCodes();
        if(hws1!=null&&hws1.size()>0){
            hws2=criteria.getZPointCodes().get(0);
        }else{
            hws1=new ArrayList<>();
            hws1.add("XXX");
        }
        List<BomPrint> bomPrints=bomAccountRepository.queryPrintAll(criteria.getBigItemCode(),criteria.getItemCode(),criteria.getZPointCode(),
                criteria.getNs(),criteria.getRAreaCode(),criteria.getBp_type(),criteria.getTckw(),
                criteria.getSupplier(),hws1,hws2);
        return bomPrints;
    }


    public List<BomPrint> queryTckwPrintAll(BomAccountQueryCriteria criteria){
        List<BomPrint> bomPrints=bomAccountRepository.queryTckwPrintAll(criteria.getTckw(),criteria.getBigItemCode());
        return bomPrints;
    }


    @Override
    @Transactional
    @Cacheable(key = "'id:' + #p0")
    public BomAccountDto findById(Long id) {
        BomAccount bomAccount = bomAccountRepository.findById(id).orElseGet(BomAccount::new);
        ValidationUtil.isNull(bomAccount.getId(),"BomAccount","id",id);
        return bomAccountMapper.toDto(bomAccount);
    }

    @Override
    @Transactional
    public BomAccount findByEntity(Long id) {
        BomAccount bomAccount = bomAccountRepository.findById(id).orElseGet(BomAccount::new);
        ValidationUtil.isNull(bomAccount.getId(),"BomAccount","id",id);
        return bomAccount;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BomAccountDto create(BomAccount resources) {
        resources.setDept(UserUtils.getDept());
        BomAccount ba=bomAccountRepository.save(resources);
        delCaches();
        return bomAccountMapper.toDto(ba);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BomAccount resources) {
        BomAccount bomAccount = bomAccountRepository.findById(resources.getId()).orElseGet(BomAccount::new);
        ValidationUtil.isNull( bomAccount.getId(),"BomAccount","id",resources.getId());
        bomAccount.copy(resources);
        bomAccountRepository.save(bomAccount);
        //更新物料收容数
        Item it=resources.getItem();
        it.setSrs(bomAccount.getSrs());
        it.setExtendD3(resources.getSrs()+0d);
        it.setXz(bomAccount.getXz());
        it.setAc(bomAccount.getAc());
        it.setContents(bomAccount.getContents());
        itemRepository.save(it);
        delCaches();
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            bomAccountLogService.copyBomAccount(id,"del");
            bomAccountRepository.deleteById(id);
        }
        delCaches();

    }
    public void delCaches(){

        Set<Object> keys=redisUtils.getAllKey("*bomAccount*");
        for (Object key:keys) {
            log.error("key:"+key);
            redisUtils.del(key.toString());
        }


    }

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



    @Override
    public List<BigItem> queryBigItemByType(String stationType) {
        String hql = "select t.bigItem from BomAccount t where t.stationType = '" + stationType + "' group by t.bigItem";
        Query query = entityManager.createQuery(hql);
        List<BigItem> resultList = query.getResultList();
        if (!resultList.isEmpty()){
            return resultList;
        }
        return  null;
    }

    @Override
    public List<BigItem> queryByAreaId(Long areaId) {
        String hql = "select t.bigItem from BomAccount t where t.rArea.id = " + areaId+" group by t.bigItem";
        Query query = entityManager.createQuery(hql);
        List<BigItem> resultList = query.getResultList();
        if (!resultList.isEmpty()){
            return resultList;
        }
        return  null;
    }



    @Override
    @Cacheable(key = "'areaid:' + #p0")
    public List<BomAccountDto> queryByArea(Long areaId) {
//        String hql = "from BomAccount ba where ba.rArea.id = " + areaId+" group by ba.item.id,ba.zPoint.id";
        String hql = "from BomAccount ba where ba.rArea.id = " + areaId+" and ba.id in (select max(t.id) from BomAccount t where t.rArea.id = " + areaId + " group by t.item.id,t.zPoint.id)";
        Query query = entityManager.createQuery(hql);
        List<BomAccount> resultList = query.getResultList();
        long ed=System.currentTimeMillis();
        return  bomAccountMapper.toDto(resultList);
    }

    @Override
    public List queryItemByArea(Long areaId) {
        String hql = "select max(t.id),t.item.id,max(t.item.code),max(t.item.name),t.zPoint.id,max(t.zPoint.code),max(t.rArea.id),max(t.rArea.code) from BomAccount t where t.rArea.id = " + areaId + " group by t.item.id,t.zPoint.id";
        Query query = entityManager.createQuery(hql);
        List<Object[]> resultList = query.getResultList();
        JSONArray array=new JSONArray();
        if (resultList.size()>0){
            for(Object[] obj:resultList){
                HashMap<String,Object> d=new HashMap<>();
                d.put("id",obj[0]);
                d.put("itemId",obj[1]);
                d.put("itemCode",obj[2]);
                d.put("itemName",obj[3]);
                d.put("zzkwId",obj[4]);
                d.put("zzkwCode",obj[5]);
                d.put("rAreaId",obj[6]);
                d.put("rAreaCode",obj[7]);
                array.add(d);
            }
        }
        return  array;
    }


    @Override
    public List<BomAccountPointDto> queryBomAccountPoints() {
        String sql = "select max(ba.id) bom_account_id,max(rk.id) rk_id,max(rk.code) gw_code,max(rk.name) gw_name," +
                "max(it.id) item_id,max(it.code) item_code,max(it.name) item_name,max(p.id) zzkw_id,max(p.code) zzkw_code,\n" +
                "max(it.extend_d3) srs,max(ba.bonded) bonded \n" +
                "from base_bom_account ba \n" +
                "LEFT JOIN base_item it on it.id=ba.item_id\n" +
                "LEFT JOIN base_area rk on rk.id=ba.r_area_id\n" +
                "left join base_point p on p.id=ba.z_point_id\n" +
                "GROUP BY ba.r_area_id,ba.item_id,ba.z_point_id,ba.bonded";
        log.info("sql==="+sql);
        List ts = entityManager.createNativeQuery(sql)
                .unwrap(SQLQuery.class)
                .setResultTransformer(
                        AliasToEntityMapResultTransformer.INSTANCE
                )
                .list();
        List<BomAccountPointDto> l = com.alibaba.fastjson.JSON.parseArray(JSON.toJSONString(ts), BomAccountPointDto.class);
        return  l;
    }

    public List<BomAccount> queryBomAccount(Long bigItemId,Long shAreaId,String gw) {
        String hql = "from BomAccount t where t.bigItem.id="+bigItemId+" and t.outType='单点' and t.item.enabled=true";
        if(shAreaId!=null){
            hql+=" and t.rArea.id = " + shAreaId;
        }
        if(gw!=null&&!gw.equals("")){
            hql+=" and t.rArea.workingStation = '" + gw+"'";
        }
        Query query = entityManager.createQuery(hql);
        List<BomAccount> resultList = query.getResultList();
        return  resultList;
    }


    public List<BomAccount> queryBomAccountKy(Long bigItemId,Long shAreaId,String kyh) {
        String hql = "from BomAccount t where t.bigItem.id="+bigItemId+" and t.item.enabled=true";
        if(shAreaId!=null){
            hql+=" and t.rArea.id = " + shAreaId;
        }
        if(kyh!=null&&!kyh.equals("")){
            hql+=" and t.kyQz = '" + kyh+"' and t.zPoint.type='"+BaseStatus.ZZKW+"'";
        }
        Query query = entityManager.createQuery(hql);
        List<BomAccount> resultList = query.getResultList();
        return  resultList;
    }
    @Override
    public List<Object[]> queryBomAccountList(String code,Long outAreaId, Long shAreaId,String bp_type,String type) {
        String hql =null;
        if(type.equals("0")){
             hql = "select t.cArea.id,t.bp_type from BomAccount t,PlanPickDetail  ptd " +
                    " where ptd.code='"+code+"' and t.bigItem.id=ptd.bigItem.id and t.outType='单点' and t.item.enabled=true and t.rArea.id = " + shAreaId +
                    " group by t.cArea.id,t.bp_type " +
                     " order by t.cArea.id";
        }else{
            hql = "select max(t.id),sum(t.singles*ptd.orderQuantity) from BomAccount t,PlanPickDetail  ptd " +
                    " where ptd.code='"+code+"' and t.bigItem.id=ptd.bigItem.id and t.outType='单点' and t.item.enabled=true and t.rArea.id = " + shAreaId +
                    " and t.cArea.id="+outAreaId +
                    " and t.bp_type='"+bp_type+"'" +
                    " group by t.zPoint.id,t.item.id";
        }
        Query query = entityManager.createQuery(hql);
        List<Object[]> resultList = query.getResultList();
        return  resultList;
    }

    public List<Object[]> bomAccountAreas(String code, Long shAreaId,String bp_type,String type) {
        String hql =null;
        if(type.equals("0")){
            hql = "select t.cArea.id,t.bp_type from BomAccount t,PlanPickDetail  ptd " +
                    " where ptd.code='"+code+"' and t.bigItem.id=ptd.bigItem.id and t.outType='单点' and t.item.enabled=true and t.rArea.id = " + shAreaId +
                    " group by t.cArea.id,t.bp_type " +
                    " order by t.cArea.id";
        }else{
            hql = "select t.cArea.id,max(t.id),sum(t.singles*ptd.orderQuantity) from BomAccount t,PlanPickDetail  ptd " +
                    " where ptd.code='"+code+"' and t.bigItem.id=ptd.bigItem.id and t.outType='单点' and t.item.enabled=true and t.rArea.id = " + shAreaId +
                    " and t.bp_type='"+bp_type+"'" +
                    " group by t.cArea.id,t.zPoint.id,t.item.id";
        }
        Query query = entityManager.createQuery(hql);
        List<Object[]> resultList = query.getResultList();
        return  resultList;
    }

    @Override
    public List<BomAccount> queryBomAccountList(BomAccount bomAccount) {
        String hql = "from BomAccount t where 1=1 and t.item.enabled=true";
        if (bomAccount.getBigItem() != null){
            hql += " and t.bigItem.id = " + bomAccount.getBigItem().getId();
        }else if (bomAccount.getStationType() != null){
            hql += " and t.stationType = '" + bomAccount.getStationType() + "'";
        }
        Query query = entityManager.createQuery(hql);
        List<BomAccount> resultList = query.getResultList();
        return resultList;
    }

    @Override
    public void updateBatch(Long areaId) {
        String sql="update base_bom_account bom set bom.pc_qty=0 where bom.r_area_id="+areaId;
        Query query=entityManager.createNativeQuery(sql);
        query.executeUpdate();
    }


    public  List<TcOutList> queryTckwList(String  tckw,String bigItemCode){
        List<BomAccount> bs = bomAccountRepository.queryTckwBom(tckw,bigItemCode);
        List<TcOutList> list =new ArrayList<>();
        for(BomAccount ba:bs){
            TcOutList tc=new TcOutList();
            tc.setItem_code(ba.getItem().getCode());
            tc.setItem_name(ba.getItem().getName());
            tc.setBig_item_code(ba.getBigItem().getCode());
            tc.setBig_item_name(ba.getBigItem().getName());
            tc.setBonded(ba.getBonded());
            tc.setOrder_qty(ba.getSingles());
            tc.setPoint_code(ba.getZPoint().getCode());
            list.add(tc);
        }
        return list;
    }

    @Transactional
    public void tcCount(String  tckw,String bigItemCode){
//        List<BomAccount> bs = bomAccountRepository.queryTchws(tckw);
        List<BomAccount> bs = bomAccountRepository.queryTckwBom(tckw,bigItemCode);
        if (bs.size() <= 0) {
            throw new BadRequestException(tckw + "未查到对应BOM工位清单");
        }
        this.bomInvDeal(bs,1d,BizStatus.TC_OUT,tckw,null,null,"台车配料出库");
    }

    //根据BOM进行库存扣减
    public void bomInvDeal(List<BomAccount> bs,double orderQty,String logType,String billCode,Long refObjId,String xpp,String description){
        Area tcArea=areaRepository.getByCode(BaseStatus.DEFAULT_TC_AREA);
        int i=0;
        for(BomAccount ba:bs){
            log.info(i+++"====join........"+ba.getId());
            String stockCode=ba.getBigItem().getCode();

            if(ba.getZPoint()==null){
                if(logType.equals(BizStatus.TC_XX)) {
                    ba.setDescription(ba.getItem().getCode() + "维护制造库位");
                }else{
                    throw new BadRequestException(ba.getItem().getCode() + "维护制造库位");
                }
            }
            if(!ba.getZPoint().getType().equals(BaseStatus.ZZKW)){
                if(logType.equals(BizStatus.TC_XX)) {
                    ba.setDescription(ba.getZPoint().getCode()+"库位类型错误");
                }else{
                    throw new BadRequestException(ba.getZPoint().getCode()+"库位类型错误");
                }
            }
            double qty=orderQty*ba.getSingles();
            //扣除线边库存
            List<Inventory> inventorys=inventoryService.getXbHyInv(ba.getItem(),ba.getRArea(),ba.getZPoint(),"耗用",null);
            if(inventorys.size()>0){
                int num=0;
                for(Inventory inv:inventorys){
                    if(qty<=0){
                        break;
                    }
                    num++;
                    if(num<inventorys.size()&&inv.getQuantity()<=0){
                        continue;
                    }
                    double srcQty=inv.getQuantity();
                    double hyQty=0;
                    if(inv.getQuantity()>qty||num==inventorys.size()){
                        hyQty=qty;
                        qty-=hyQty;
                    }else{
                        hyQty=inv.getQuantity();
                        qty-=hyQty;
                    }
                    inv.setQuantity(inv.getQuantity()-hyQty);
                    inventoryService.update(inv);
                    InventoryLog log= inventoryLogService.storeInventoryLog(logType,BizStatus.REDUCE,billCode,ba.getRArea(),inv.getItemKey(),
                            inv.getPoint(),inv.getPoint(),null,null,srcQty,hyQty,stockCode,null,ba.getBigItem().getCode(),
                            refObjId,inv.getId(),description);
                }
            }else{
                ItemKey itemKey=itemKeyService.getItemKey(ba.getItem(),null,ba.getBonded());
                Inventory inventory = new Inventory();
                inventory.setItemKey(itemKey);
                inventory.setPoint(ba.getZPoint());
                inventory.setPointCode(ba.getZPoint().getCode());
//                inventory.setZzkw(ba.getZPoint());
                inventory.setDept(ba.getRArea().getDept());
                inventory.setArea(ba.getRArea());
                inventory.setQuantity(-qty);
                inventoryService.create(inventory);
                InventoryLog log=inventoryLogService.storeInventoryLog(logType,BizStatus.REDUCE,billCode,ba.getRArea(),inventory.getItemKey(),
                        inventory.getPoint(),inventory.getPoint(),null,null,0d,qty,null,null,ba.getBigItem().getCode(),
                        refObjId,inventory.getId(),description);
            }
            ItemKey dstItemKey = itemKeyService.getItemKey(ba.getItem(), ba.getKyQz(),ba.getBonded());
            //台车出库，创建台车库位
            if(logType.equals(BizStatus.TC_OUT)) {
                Point dstPoint=pointRepository.findByCode(ba.getTckw());
                if (dstPoint==null){
                    dstPoint=new Point();
                    dstPoint.setDept(ba.getZPoint().getDept());
                    dstPoint.setCode(ba.getTckw());
                    dstPoint.setName(ba.getTckw());
                    dstPoint.setType(BaseStatus.HCKW);
                    dstPoint.setArea(tcArea);
                    pointRepository.save(dstPoint);
                }else{
                    dstPoint.setArea(tcArea);
                    dstPoint.setType(BaseStatus.HCKW);
                    pointRepository.save(dstPoint);
                }

                Inventory dstInventory = inventoryService.getInventory(dstItemKey, tcArea, dstPoint, dstPoint, dstPoint.getDept(), BizStatus.TC_RK, stockCode);
                double srcQty = dstInventory.getQuantity();
                dstInventory.setQuantity(dstInventory.getQuantity() + orderQty*ba.getSingles());
                inventoryService.update(dstInventory);
                InventoryLog inventoryLog = inventoryLogService.storeInventoryLog(BizStatus.TC_RK, BizStatus.ADD, billCode,  tcArea, dstItemKey, dstInventory.getPoint(), dstInventory.getPoint(), null, null, srcQty, orderQty*ba.getSingles(), stockCode, null,
                        ba.getBigItem().getCode(), ba.getId(), dstInventory.getId(), "台车配料入库");
                inventoryLogService.update(inventoryLog);
            }
        }
    }


    //根据BOM进行库存扣减
    @Override
    @Transactional
    public void bomTcInvDeal(List<BomAccount> bs,double orderQty,String billCode,Long refObjId){
        Area tcArea=areaRepository.getByCode(BaseStatus.DEFAULT_TC_AREA);
        for(BomAccount ba:bs){
            String stockCode=ba.getBigItem().getCode();
            double qty=orderQty*ba.getSingles();
            String tckw=ba.getTckw();
            if(tckw==null){
                tckw="空台车";
            }
            Point tcPoint=pointRepository.findByCode(tckw);
            if (tcPoint==null){
                tcPoint=new Point();
                tcPoint.setDept(ba.getZPoint().getDept());
                tcPoint.setCode(tckw);
                tcPoint.setName(tckw);
                tcPoint.setType(BaseStatus.HCKW);
                tcPoint.setArea(tcArea);
                pointRepository.save(tcPoint);
            }else{
                tcPoint.setArea(tcArea);
                tcPoint.setType(BaseStatus.HCKW);
                pointRepository.save(tcPoint);
            }
            //扣除台车库存
            List<Inventory> inventorys=inventoryService.getXbHyInv(ba.getItem(),tcArea,tcPoint,"台车下线",stockCode);
            if(inventorys.size()>0){
                int num=0;
                for(Inventory inv:inventorys){
                    if(qty<=0){
                        break;
                    }
                    num++;
                    if(num<inventorys.size()&&inv.getQuantity()<=0){
                        continue;
                    }
                    double srcQty=inv.getQuantity();
                    double hyQty=0;
                    if(inv.getQuantity()>qty||num==inventorys.size()){
                        hyQty=qty;
                        qty-=hyQty;
                    }else{
                        hyQty=inv.getQuantity();
                        qty-=hyQty;
                    }
                    inv.setQuantity(inv.getQuantity()-hyQty);
                    inventoryService.update(inv);
                    inventoryLogService.storeInventoryLog(BizStatus.TC_XX,BizStatus.REDUCE,billCode,tcArea,inv.getItemKey(),
                            inv.getPoint(),inv.getPoint(),null,null,srcQty,hyQty,stockCode,null,ba.getBigItem().getCode(),
                            refObjId,inv.getId(),ba.getRArea().getName()+"台车BOM下线");

                }
            }else{
                ItemKey itemKey = itemKeyService.getItemKey(ba.getItem(), ba.getKyQz(),ba.getBonded());
                Inventory inventory = inventoryService.getInventory(itemKey, tcArea, tcPoint, tcPoint, tcPoint.getDept(), BizStatus.TC_XX, stockCode);
                inventory.setQuantity(-qty);
                inventoryService.create(inventory);
                inventoryLogService.storeInventoryLog(BizStatus.TC_XX,BizStatus.REDUCE,billCode,tcArea,inventory.getItemKey(),
                        inventory.getPoint(),inventory.getPoint(),null,null,0d,qty,null,null,ba.getBigItem().getCode(),
                        refObjId,inventory.getId(),ba.getRArea().getName()+"台车BOM下线");
            }
        }
    }
}