package cn.ibizlab.businesscentral.core.odoo_stock.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.math.BigInteger;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import cn.ibizlab.businesscentral.util.errors.BadRequestAlertException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.context.annotation.Lazy;
import cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_move;
import cn.ibizlab.businesscentral.core.odoo_stock.filter.Stock_moveSearchContext;
import cn.ibizlab.businesscentral.core.odoo_stock.service.IStock_moveService;

import cn.ibizlab.businesscentral.util.helper.CachedBeanCopier;
import cn.ibizlab.businesscentral.util.helper.DEFieldCacheMap;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ibizlab.businesscentral.core.util.helper.EBSServiceImpl;
import cn.ibizlab.businesscentral.core.odoo_stock.mapper.Stock_moveMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.alibaba.fastjson.JSONObject;

import org.springframework.util.StringUtils;

/**
 * 实体[库存移动] 服务对象接口实现
 */
@Slf4j
@Service("Stock_moveServiceImpl")
public class Stock_moveServiceImpl extends EBSServiceImpl<Stock_moveMapper, Stock_move> implements IStock_moveService {

    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_account.service.IAccount_moveService accountMoveService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_mrp.service.IMrp_product_produce_lineService mrpProductProduceLineService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_purchase.service.IPurchase_requisition_lineService purchaseRequisitionLineService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_repair.service.IRepair_lineService repairLineService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_repair.service.IRepair_orderService repairOrderService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_stock.service.IStock_move_lineService stockMoveLineService;

    protected cn.ibizlab.businesscentral.core.odoo_stock.service.IStock_moveService stockMoveService = this;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_stock.service.IStock_return_picking_lineService stockReturnPickingLineService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_stock.service.IStock_scrapService stockScrapService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_mrp.service.IMrp_bom_lineService mrpBomLineService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_mrp.service.IMrp_productionService mrpProductionService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_mrp.service.IMrp_routing_workcenterService mrpRoutingWorkcenterService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_mrp.service.IMrp_unbuildService mrpUnbuildService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_mrp.service.IMrp_workorderService mrpWorkorderService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_product.service.IProduct_packagingService productPackagingService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_product.service.IProduct_productService productProductService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_purchase.service.IPurchase_order_lineService purchaseOrderLineService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_base.service.IRes_companyService resCompanyService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_base.service.IRes_partnerService resPartnerService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_base.service.IRes_usersService resUsersService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_sale.service.ISale_order_lineService saleOrderLineService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_stock.service.IStock_inventoryService stockInventoryService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_stock.service.IStock_locationService stockLocationService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_stock.service.IStock_package_levelService stockPackageLevelService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_stock.service.IStock_picking_typeService stockPickingTypeService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_stock.service.IStock_pickingService stockPickingService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_stock.service.IStock_ruleService stockRuleService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_stock.service.IStock_warehouseService stockWarehouseService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_uom.service.IUom_uomService uomUomService;

    protected int batchSize = 500;

    public String getIrModel(){
        return "stock.move" ;
    }

    private boolean messageinfo = false ;

    public void setMessageInfo(boolean messageinfo){
        this.messageinfo = messageinfo ;
    }

    @Override
    @Transactional
    public boolean create(Stock_move et) {
        boolean mail_create_nosubscribe = et.get("mail_create_nosubscribe") != null;
        boolean mail_create_nolog = et.get("mail_create_nolog") != null;
        boolean mail_notrack = et.get("mail_notrack") != null;
        fillParentData(et);
        if(!this.retBool(this.baseMapper.insert(et)))
            return false;
        CachedBeanCopier.copy(get(et.getId()),et);

        if (messageinfo && !mail_create_nosubscribe) {
            cn.ibizlab.businesscentral.util.security.SpringContextHolder.getBean(cn.ibizlab.businesscentral.core.extensions.service.Mail_followersExService.class).add_default_followers(this,et);
        }

        if (messageinfo && !mail_create_nolog) {
            cn.ibizlab.businesscentral.util.security.SpringContextHolder.getBean(cn.ibizlab.businesscentral.core.extensions.service.Mail_tracking_valueExService.class).add_default_create_message(this,et);
        }

        if (messageinfo && !mail_notrack) {

        }

        return true;
    }

    @Override
    @Transactional
    public void createBatch(List<Stock_move> list) {
        list.forEach(item->fillParentData(item));
        this.saveBatch(list,batchSize);
    }

    @Override
    @Transactional
    public boolean update(Stock_move et) {
        Stock_move old = new Stock_move() ;
        CachedBeanCopier.copy(get(et.getId()), old);
        boolean mail_notrack = et.get("mail_notrack") != null;
        fillParentData(et);
         if(!update(et,(Wrapper) et.getUpdateWrapper(true).eq("id",et.getId())))
            return false;
        CachedBeanCopier.copy(get(et.getId()),et);
        if (messageinfo && !mail_notrack) {
            cn.ibizlab.businesscentral.util.security.SpringContextHolder.getBean(cn.ibizlab.businesscentral.core.extensions.service.Mail_tracking_valueExService.class).message_track(this,old,et);
        }
        return true;
    }

    @Override
    @Transactional
    public void updateBatch(List<Stock_move> list) {
        list.forEach(item->fillParentData(item));
        updateBatchById(list,batchSize);
    }

    @Override
    @Transactional
    public boolean remove(Long key) {
        boolean result=removeById(key);
        return result ;
    }

    @Override
    @Transactional
    public void removeBatch(Collection<Long> idList) {
        removeByIds(idList);
    }

    @Override
    @Transactional
    public Stock_move get(Long key) {
        Stock_move et = getById(key);
        if(et==null){
            et=new Stock_move();
            et.setId(key);
        }
        else{
        }
        return et;
    }





    /**
     * 为当前实体填充父数据（外键值文本、外键值附加数据）
     * @param et
     */
    private void fillParentData(Stock_move et){
        //实体关系[DER1N_STOCK_MOVE__MRP_PRODUCTION__CREATED_PRODUCTION_ID]
        if(!ObjectUtils.isEmpty(et.getCreatedProductionId())){
            cn.ibizlab.businesscentral.core.odoo_mrp.domain.Mrp_production odooCreatedProduction=et.getOdooCreatedProduction();
            if(ObjectUtils.isEmpty(odooCreatedProduction)){
                cn.ibizlab.businesscentral.core.odoo_mrp.domain.Mrp_production majorEntity=mrpProductionService.get(et.getCreatedProductionId());
                et.setOdooCreatedProduction(majorEntity);
                odooCreatedProduction=majorEntity;
            }
            et.setCreatedProductionIdText(odooCreatedProduction.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__MRP_PRODUCTION__PRODUCTION_ID]
        if(!ObjectUtils.isEmpty(et.getProductionId())){
            cn.ibizlab.businesscentral.core.odoo_mrp.domain.Mrp_production odooProduction=et.getOdooProduction();
            if(ObjectUtils.isEmpty(odooProduction)){
                cn.ibizlab.businesscentral.core.odoo_mrp.domain.Mrp_production majorEntity=mrpProductionService.get(et.getProductionId());
                et.setOdooProduction(majorEntity);
                odooProduction=majorEntity;
            }
            et.setProductionIdText(odooProduction.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__MRP_PRODUCTION__RAW_MATERIAL_PRODUCTION_ID]
        if(!ObjectUtils.isEmpty(et.getRawMaterialProductionId())){
            cn.ibizlab.businesscentral.core.odoo_mrp.domain.Mrp_production odooRawMaterialProduction=et.getOdooRawMaterialProduction();
            if(ObjectUtils.isEmpty(odooRawMaterialProduction)){
                cn.ibizlab.businesscentral.core.odoo_mrp.domain.Mrp_production majorEntity=mrpProductionService.get(et.getRawMaterialProductionId());
                et.setOdooRawMaterialProduction(majorEntity);
                odooRawMaterialProduction=majorEntity;
            }
            et.setRawMaterialProductionIdText(odooRawMaterialProduction.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__MRP_ROUTING_WORKCENTER__OPERATION_ID]
        if(!ObjectUtils.isEmpty(et.getOperationId())){
            cn.ibizlab.businesscentral.core.odoo_mrp.domain.Mrp_routing_workcenter odooOperation=et.getOdooOperation();
            if(ObjectUtils.isEmpty(odooOperation)){
                cn.ibizlab.businesscentral.core.odoo_mrp.domain.Mrp_routing_workcenter majorEntity=mrpRoutingWorkcenterService.get(et.getOperationId());
                et.setOdooOperation(majorEntity);
                odooOperation=majorEntity;
            }
            et.setOperationIdText(odooOperation.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__MRP_UNBUILD__CONSUME_UNBUILD_ID]
        if(!ObjectUtils.isEmpty(et.getConsumeUnbuildId())){
            cn.ibizlab.businesscentral.core.odoo_mrp.domain.Mrp_unbuild odooConsumeUnbuild=et.getOdooConsumeUnbuild();
            if(ObjectUtils.isEmpty(odooConsumeUnbuild)){
                cn.ibizlab.businesscentral.core.odoo_mrp.domain.Mrp_unbuild majorEntity=mrpUnbuildService.get(et.getConsumeUnbuildId());
                et.setOdooConsumeUnbuild(majorEntity);
                odooConsumeUnbuild=majorEntity;
            }
            et.setConsumeUnbuildIdText(odooConsumeUnbuild.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__MRP_UNBUILD__UNBUILD_ID]
        if(!ObjectUtils.isEmpty(et.getUnbuildId())){
            cn.ibizlab.businesscentral.core.odoo_mrp.domain.Mrp_unbuild odooUnbuild=et.getOdooUnbuild();
            if(ObjectUtils.isEmpty(odooUnbuild)){
                cn.ibizlab.businesscentral.core.odoo_mrp.domain.Mrp_unbuild majorEntity=mrpUnbuildService.get(et.getUnbuildId());
                et.setOdooUnbuild(majorEntity);
                odooUnbuild=majorEntity;
            }
            et.setUnbuildIdText(odooUnbuild.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__MRP_WORKORDER__WORKORDER_ID]
        if(!ObjectUtils.isEmpty(et.getWorkorderId())){
            cn.ibizlab.businesscentral.core.odoo_mrp.domain.Mrp_workorder odooWorkorder=et.getOdooWorkorder();
            if(ObjectUtils.isEmpty(odooWorkorder)){
                cn.ibizlab.businesscentral.core.odoo_mrp.domain.Mrp_workorder majorEntity=mrpWorkorderService.get(et.getWorkorderId());
                et.setOdooWorkorder(majorEntity);
                odooWorkorder=majorEntity;
            }
            et.setWorkorderIdText(odooWorkorder.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__PRODUCT_PACKAGING__PRODUCT_PACKAGING]
        if(!ObjectUtils.isEmpty(et.getProductPackaging())){
            cn.ibizlab.businesscentral.core.odoo_product.domain.Product_packaging odooProductPackging=et.getOdooProductPackging();
            if(ObjectUtils.isEmpty(odooProductPackging)){
                cn.ibizlab.businesscentral.core.odoo_product.domain.Product_packaging majorEntity=productPackagingService.get(et.getProductPackaging());
                et.setOdooProductPackging(majorEntity);
                odooProductPackging=majorEntity;
            }
            et.setProductPackagingText(odooProductPackging.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__PRODUCT_PRODUCT__PRODUCT_ID]
        if(!ObjectUtils.isEmpty(et.getProductId())){
            cn.ibizlab.businesscentral.core.odoo_product.domain.Product_product odooProduct=et.getOdooProduct();
            if(ObjectUtils.isEmpty(odooProduct)){
                cn.ibizlab.businesscentral.core.odoo_product.domain.Product_product majorEntity=productProductService.get(et.getProductId());
                et.setOdooProduct(majorEntity);
                odooProduct=majorEntity;
            }
            et.setProductType(odooProduct.getType());
            et.setHasTracking(odooProduct.getTracking());
            et.setProductIdText(odooProduct.getName());
            et.setProductTmplId(odooProduct.getProductTmplId());
        }
        //实体关系[DER1N_STOCK_MOVE__PURCHASE_ORDER_LINE__CREATED_PURCHASE_LINE_ID]
        if(!ObjectUtils.isEmpty(et.getCreatedPurchaseLineId())){
            cn.ibizlab.businesscentral.core.odoo_purchase.domain.Purchase_order_line odooCreatedPurchaseLine=et.getOdooCreatedPurchaseLine();
            if(ObjectUtils.isEmpty(odooCreatedPurchaseLine)){
                cn.ibizlab.businesscentral.core.odoo_purchase.domain.Purchase_order_line majorEntity=purchaseOrderLineService.get(et.getCreatedPurchaseLineId());
                et.setOdooCreatedPurchaseLine(majorEntity);
                odooCreatedPurchaseLine=majorEntity;
            }
            et.setCreatedPurchaseLineIdText(odooCreatedPurchaseLine.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__PURCHASE_ORDER_LINE__PURCHASE_LINE_ID]
        if(!ObjectUtils.isEmpty(et.getPurchaseLineId())){
            cn.ibizlab.businesscentral.core.odoo_purchase.domain.Purchase_order_line odooPurchaseLine=et.getOdooPurchaseLine();
            if(ObjectUtils.isEmpty(odooPurchaseLine)){
                cn.ibizlab.businesscentral.core.odoo_purchase.domain.Purchase_order_line majorEntity=purchaseOrderLineService.get(et.getPurchaseLineId());
                et.setOdooPurchaseLine(majorEntity);
                odooPurchaseLine=majorEntity;
            }
            et.setPurchaseLineIdText(odooPurchaseLine.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__REPAIR_ORDER__REPAIR_ID]
        if(!ObjectUtils.isEmpty(et.getRepairId())){
            cn.ibizlab.businesscentral.core.odoo_repair.domain.Repair_order odooRepair=et.getOdooRepair();
            if(ObjectUtils.isEmpty(odooRepair)){
                cn.ibizlab.businesscentral.core.odoo_repair.domain.Repair_order majorEntity=repairOrderService.get(et.getRepairId());
                et.setOdooRepair(majorEntity);
                odooRepair=majorEntity;
            }
            et.setRepairIdText(odooRepair.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__RES_COMPANY__COMPANY_ID]
        if(!ObjectUtils.isEmpty(et.getCompanyId())){
            cn.ibizlab.businesscentral.core.odoo_base.domain.Res_company odooCompany=et.getOdooCompany();
            if(ObjectUtils.isEmpty(odooCompany)){
                cn.ibizlab.businesscentral.core.odoo_base.domain.Res_company majorEntity=resCompanyService.get(et.getCompanyId());
                et.setOdooCompany(majorEntity);
                odooCompany=majorEntity;
            }
            et.setCompanyIdText(odooCompany.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__RES_PARTNER__PARTNER_ID]
        if(!ObjectUtils.isEmpty(et.getPartnerId())){
            cn.ibizlab.businesscentral.core.odoo_base.domain.Res_partner odooPartner=et.getOdooPartner();
            if(ObjectUtils.isEmpty(odooPartner)){
                cn.ibizlab.businesscentral.core.odoo_base.domain.Res_partner majorEntity=resPartnerService.get(et.getPartnerId());
                et.setOdooPartner(majorEntity);
                odooPartner=majorEntity;
            }
            et.setPartnerIdText(odooPartner.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__RES_PARTNER__RESTRICT_PARTNER_ID]
        if(!ObjectUtils.isEmpty(et.getRestrictPartnerId())){
            cn.ibizlab.businesscentral.core.odoo_base.domain.Res_partner odooRestrictPartner=et.getOdooRestrictPartner();
            if(ObjectUtils.isEmpty(odooRestrictPartner)){
                cn.ibizlab.businesscentral.core.odoo_base.domain.Res_partner majorEntity=resPartnerService.get(et.getRestrictPartnerId());
                et.setOdooRestrictPartner(majorEntity);
                odooRestrictPartner=majorEntity;
            }
            et.setRestrictPartnerIdText(odooRestrictPartner.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__RES_USERS__CREATE_UID]
        if(!ObjectUtils.isEmpty(et.getCreateUid())){
            cn.ibizlab.businesscentral.core.odoo_base.domain.Res_users odooCreate=et.getOdooCreate();
            if(ObjectUtils.isEmpty(odooCreate)){
                cn.ibizlab.businesscentral.core.odoo_base.domain.Res_users majorEntity=resUsersService.get(et.getCreateUid());
                et.setOdooCreate(majorEntity);
                odooCreate=majorEntity;
            }
            et.setCreateUidText(odooCreate.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__RES_USERS__WRITE_UID]
        if(!ObjectUtils.isEmpty(et.getWriteUid())){
            cn.ibizlab.businesscentral.core.odoo_base.domain.Res_users odooWrite=et.getOdooWrite();
            if(ObjectUtils.isEmpty(odooWrite)){
                cn.ibizlab.businesscentral.core.odoo_base.domain.Res_users majorEntity=resUsersService.get(et.getWriteUid());
                et.setOdooWrite(majorEntity);
                odooWrite=majorEntity;
            }
            et.setWriteUidText(odooWrite.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__SALE_ORDER_LINE__SALE_LINE_ID]
        if(!ObjectUtils.isEmpty(et.getSaleLineId())){
            cn.ibizlab.businesscentral.core.odoo_sale.domain.Sale_order_line odooSaleLine=et.getOdooSaleLine();
            if(ObjectUtils.isEmpty(odooSaleLine)){
                cn.ibizlab.businesscentral.core.odoo_sale.domain.Sale_order_line majorEntity=saleOrderLineService.get(et.getSaleLineId());
                et.setOdooSaleLine(majorEntity);
                odooSaleLine=majorEntity;
            }
            et.setSaleLineIdText(odooSaleLine.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__STOCK_INVENTORY__INVENTORY_ID]
        if(!ObjectUtils.isEmpty(et.getInventoryId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_inventory odooInventory=et.getOdooInventory();
            if(ObjectUtils.isEmpty(odooInventory)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_inventory majorEntity=stockInventoryService.get(et.getInventoryId());
                et.setOdooInventory(majorEntity);
                odooInventory=majorEntity;
            }
            et.setInventoryIdText(odooInventory.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__STOCK_LOCATION__LOCATION_DEST_ID]
        if(!ObjectUtils.isEmpty(et.getLocationDestId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location odooLocationDest=et.getOdooLocationDest();
            if(ObjectUtils.isEmpty(odooLocationDest)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location majorEntity=stockLocationService.get(et.getLocationDestId());
                et.setOdooLocationDest(majorEntity);
                odooLocationDest=majorEntity;
            }
            et.setScrapped(odooLocationDest.getScrapLocation());
            et.setLocationDestIdText(odooLocationDest.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__STOCK_LOCATION__LOCATION_ID]
        if(!ObjectUtils.isEmpty(et.getLocationId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location odooLocation=et.getOdooLocation();
            if(ObjectUtils.isEmpty(odooLocation)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location majorEntity=stockLocationService.get(et.getLocationId());
                et.setOdooLocation(majorEntity);
                odooLocation=majorEntity;
            }
            et.setLocationIdText(odooLocation.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__STOCK_MOVE__ORIGIN_RETURNED_MOVE_ID]
        if(!ObjectUtils.isEmpty(et.getOriginReturnedMoveId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_move odooOriginReturnedMove=et.getOdooOriginReturnedMove();
            if(ObjectUtils.isEmpty(odooOriginReturnedMove)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_move majorEntity=stockMoveService.get(et.getOriginReturnedMoveId());
                et.setOdooOriginReturnedMove(majorEntity);
                odooOriginReturnedMove=majorEntity;
            }
            et.setOriginReturnedMoveIdText(odooOriginReturnedMove.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__STOCK_PICKING_TYPE__PICKING_TYPE_ID]
        if(!ObjectUtils.isEmpty(et.getPickingTypeId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_picking_type odooPickingType=et.getOdooPickingType();
            if(ObjectUtils.isEmpty(odooPickingType)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_picking_type majorEntity=stockPickingTypeService.get(et.getPickingTypeId());
                et.setOdooPickingType(majorEntity);
                odooPickingType=majorEntity;
            }
            et.setShowOperations(odooPickingType.getShowOperations());
            et.setPickingTypeIdText(odooPickingType.getName());
            et.setPickingCode(odooPickingType.getCode());
            et.setPickingTypeEntirePacks(odooPickingType.getShowEntirePacks());
        }
        //实体关系[DER1N_STOCK_MOVE__STOCK_PICKING__PICKING_ID]
        if(!ObjectUtils.isEmpty(et.getPickingId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_picking odooPicking=et.getOdooPicking();
            if(ObjectUtils.isEmpty(odooPicking)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_picking majorEntity=stockPickingService.get(et.getPickingId());
                et.setOdooPicking(majorEntity);
                odooPicking=majorEntity;
            }
            et.setPickingPartnerId(odooPicking.getPartnerId());
            et.setBackorderId(odooPicking.getBackorderId());
            et.setPickingIdText(odooPicking.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__STOCK_RULE__RULE_ID]
        if(!ObjectUtils.isEmpty(et.getRuleId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_rule odooRule=et.getOdooRule();
            if(ObjectUtils.isEmpty(odooRule)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_rule majorEntity=stockRuleService.get(et.getRuleId());
                et.setOdooRule(majorEntity);
                odooRule=majorEntity;
            }
            et.setRuleIdText(odooRule.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__STOCK_WAREHOUSE__WAREHOUSE_ID]
        if(!ObjectUtils.isEmpty(et.getWarehouseId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_warehouse odooWarehouse=et.getOdooWarehouse();
            if(ObjectUtils.isEmpty(odooWarehouse)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_warehouse majorEntity=stockWarehouseService.get(et.getWarehouseId());
                et.setOdooWarehouse(majorEntity);
                odooWarehouse=majorEntity;
            }
            et.setWarehouseIdText(odooWarehouse.getName());
        }
        //实体关系[DER1N_STOCK_MOVE__UOM_UOM__PRODUCT_UOM]
        if(!ObjectUtils.isEmpty(et.getProductUom())){
            cn.ibizlab.businesscentral.core.odoo_uom.domain.Uom_uom odooProductUom=et.getOdooProductUom();
            if(ObjectUtils.isEmpty(odooProductUom)){
                cn.ibizlab.businesscentral.core.odoo_uom.domain.Uom_uom majorEntity=uomUomService.get(et.getProductUom());
                et.setOdooProductUom(majorEntity);
                odooProductUom=majorEntity;
            }
            et.setProductUomText(odooProductUom.getName());
        }
    }




    @Override
    public List<JSONObject> select(String sql, Map param){
        return this.baseMapper.selectBySQL(sql,param);
    }

    @Override
    @Transactional
    public boolean execute(String sql , Map param){
        if (sql == null || sql.isEmpty()) {
            return false;
        }
        if (sql.toLowerCase().trim().startsWith("insert")) {
            return this.baseMapper.insertBySQL(sql,param);
        }
        if (sql.toLowerCase().trim().startsWith("update")) {
            return this.baseMapper.updateBySQL(sql,param);
        }
        if (sql.toLowerCase().trim().startsWith("delete")) {
            return this.baseMapper.deleteBySQL(sql,param);
        }
        log.warn("暂未支持的SQL语法");
        return true;
    }

    @Override
    public List<Stock_move> getStockMoveByIds(List<String> ids) {
         return this.listByIds(ids);
    }

    @Override
    public List<Stock_move> getStockMoveByEntities(List<Stock_move> entities) {
        List ids =new ArrayList();
        for(Stock_move entity : entities){
            Serializable id=entity.getId();
            if(!ObjectUtils.isEmpty(id)){
                ids.add(id);
            }
        }
        if(ids.size()>0)
           return this.listByIds(ids);
        else
           return entities;
    }



}



