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_warehouse;
import cn.ibizlab.businesscentral.core.odoo_stock.filter.Stock_warehouseSearchContext;
import cn.ibizlab.businesscentral.core.odoo_stock.service.IStock_warehouseService;

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_warehouseMapper;
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_warehouseServiceImpl")
public class Stock_warehouseServiceImpl extends EBSServiceImpl<Stock_warehouseMapper, Stock_warehouse> implements IStock_warehouseService {

    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_product.service.IProduct_replenishService productReplenishService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_purchase.service.IPurchase_reportService purchaseReportService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_sale.service.ISale_orderService saleOrderService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_sale.service.ISale_reportService saleReportService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_stock.service.IStock_location_routeService stockLocationRouteService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_stock.service.IStock_moveService stockMoveService;
    @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_ruleService stockRuleService;
    @Autowired
    @Lazy
    protected cn.ibizlab.businesscentral.core.odoo_stock.service.IStock_warehouse_orderpointService stockWarehouseOrderpointService;
    @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_stock.service.IStock_locationService stockLocationService;

    protected int batchSize = 500;

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

    private boolean messageinfo = false ;

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

    @Override
    @Transactional
    public boolean create(Stock_warehouse 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_warehouse> list) {
        list.forEach(item->fillParentData(item));
        this.saveBatch(list,batchSize);
    }

    @Override
    @Transactional
    public boolean update(Stock_warehouse et) {
        Stock_warehouse old = new Stock_warehouse() ;
        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_warehouse> 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_warehouse get(Long key) {
        Stock_warehouse et = getById(key);
        if(et==null){
            et=new Stock_warehouse();
            et.setId(key);
        }
        else{
        }
        return et;
    }





    /**
     * 为当前实体填充父数据（外键值文本、外键值附加数据）
     * @param et
     */
    private void fillParentData(Stock_warehouse et){
        //实体关系[DER1N_STOCK_WAREHOUSE__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_WAREHOUSE__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_WAREHOUSE__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_WAREHOUSE__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_WAREHOUSE__STOCK_LOCATION_ROUTE__CROSSDOCK_ROUTE_ID]
        if(!ObjectUtils.isEmpty(et.getCrossdockRouteId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location_route odooCrossdockRoute=et.getOdooCrossdockRoute();
            if(ObjectUtils.isEmpty(odooCrossdockRoute)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location_route majorEntity=stockLocationRouteService.get(et.getCrossdockRouteId());
                et.setOdooCrossdockRoute(majorEntity);
                odooCrossdockRoute=majorEntity;
            }
            et.setCrossdockRouteIdText(odooCrossdockRoute.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_LOCATION_ROUTE__DELIVERY_ROUTE_ID]
        if(!ObjectUtils.isEmpty(et.getDeliveryRouteId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location_route odooDeliveryRoute=et.getOdooDeliveryRoute();
            if(ObjectUtils.isEmpty(odooDeliveryRoute)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location_route majorEntity=stockLocationRouteService.get(et.getDeliveryRouteId());
                et.setOdooDeliveryRoute(majorEntity);
                odooDeliveryRoute=majorEntity;
            }
            et.setDeliveryRouteIdText(odooDeliveryRoute.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_LOCATION_ROUTE__PBM_ROUTE_ID]
        if(!ObjectUtils.isEmpty(et.getPbmRouteId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location_route odooPbmRoute=et.getOdooPbmRoute();
            if(ObjectUtils.isEmpty(odooPbmRoute)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location_route majorEntity=stockLocationRouteService.get(et.getPbmRouteId());
                et.setOdooPbmRoute(majorEntity);
                odooPbmRoute=majorEntity;
            }
            et.setPbmRouteIdText(odooPbmRoute.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_LOCATION_ROUTE__RECEPTION_ROUTE_ID]
        if(!ObjectUtils.isEmpty(et.getReceptionRouteId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location_route odooReceptionRoute=et.getOdooReceptionRoute();
            if(ObjectUtils.isEmpty(odooReceptionRoute)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location_route majorEntity=stockLocationRouteService.get(et.getReceptionRouteId());
                et.setOdooReceptionRoute(majorEntity);
                odooReceptionRoute=majorEntity;
            }
            et.setReceptionRouteIdText(odooReceptionRoute.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_LOCATION__LOT_STOCK_ID]
        if(!ObjectUtils.isEmpty(et.getLotStockId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location odooLotStock=et.getOdooLotStock();
            if(ObjectUtils.isEmpty(odooLotStock)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location majorEntity=stockLocationService.get(et.getLotStockId());
                et.setOdooLotStock(majorEntity);
                odooLotStock=majorEntity;
            }
            et.setLotStockIdText(odooLotStock.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_LOCATION__PBM_LOC_ID]
        if(!ObjectUtils.isEmpty(et.getPbmLocId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location odooPbmLoc=et.getOdooPbmLoc();
            if(ObjectUtils.isEmpty(odooPbmLoc)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location majorEntity=stockLocationService.get(et.getPbmLocId());
                et.setOdooPbmLoc(majorEntity);
                odooPbmLoc=majorEntity;
            }
            et.setPbmLocIdText(odooPbmLoc.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_LOCATION__SAM_LOC_ID]
        if(!ObjectUtils.isEmpty(et.getSamLocId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location odooSamLoc=et.getOdooSamLoc();
            if(ObjectUtils.isEmpty(odooSamLoc)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location majorEntity=stockLocationService.get(et.getSamLocId());
                et.setOdooSamLoc(majorEntity);
                odooSamLoc=majorEntity;
            }
            et.setSamLocIdText(odooSamLoc.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_LOCATION__VIEW_LOCATION_ID]
        if(!ObjectUtils.isEmpty(et.getViewLocationId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location odooViewLocation=et.getOdooViewLocation();
            if(ObjectUtils.isEmpty(odooViewLocation)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location majorEntity=stockLocationService.get(et.getViewLocationId());
                et.setOdooViewLocation(majorEntity);
                odooViewLocation=majorEntity;
            }
            et.setViewLocationIdText(odooViewLocation.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_LOCATION__WH_INPUT_STOCK_LOC_ID]
        if(!ObjectUtils.isEmpty(et.getWhInputStockLocId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location odooWhInputStockLoc=et.getOdooWhInputStockLoc();
            if(ObjectUtils.isEmpty(odooWhInputStockLoc)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location majorEntity=stockLocationService.get(et.getWhInputStockLocId());
                et.setOdooWhInputStockLoc(majorEntity);
                odooWhInputStockLoc=majorEntity;
            }
            et.setWhInputStockLocIdText(odooWhInputStockLoc.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_LOCATION__WH_OUTPUT_STOCK_LOC_ID]
        if(!ObjectUtils.isEmpty(et.getWhOutputStockLocId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location odooWhOutputStockLoc=et.getOdooWhOutputStockLoc();
            if(ObjectUtils.isEmpty(odooWhOutputStockLoc)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location majorEntity=stockLocationService.get(et.getWhOutputStockLocId());
                et.setOdooWhOutputStockLoc(majorEntity);
                odooWhOutputStockLoc=majorEntity;
            }
            et.setWhOutputStockLocIdText(odooWhOutputStockLoc.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_LOCATION__WH_PACK_STOCK_LOC_ID]
        if(!ObjectUtils.isEmpty(et.getWhPackStockLocId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location odooWhPackStockLoc=et.getOdooWhPackStockLoc();
            if(ObjectUtils.isEmpty(odooWhPackStockLoc)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location majorEntity=stockLocationService.get(et.getWhPackStockLocId());
                et.setOdooWhPackStockLoc(majorEntity);
                odooWhPackStockLoc=majorEntity;
            }
            et.setWhPackStockLocIdText(odooWhPackStockLoc.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_LOCATION__WH_QC_STOCK_LOC_ID]
        if(!ObjectUtils.isEmpty(et.getWhQcStockLocId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location odooWhQcStockLoc=et.getOdooWhQcStockLoc();
            if(ObjectUtils.isEmpty(odooWhQcStockLoc)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_location majorEntity=stockLocationService.get(et.getWhQcStockLocId());
                et.setOdooWhQcStockLoc(majorEntity);
                odooWhQcStockLoc=majorEntity;
            }
            et.setWhQcStockLocIdText(odooWhQcStockLoc.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_PICKING_TYPE__INT_TYPE_ID]
        if(!ObjectUtils.isEmpty(et.getIntTypeId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_picking_type odooIntType=et.getOdooIntType();
            if(ObjectUtils.isEmpty(odooIntType)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_picking_type majorEntity=stockPickingTypeService.get(et.getIntTypeId());
                et.setOdooIntType(majorEntity);
                odooIntType=majorEntity;
            }
            et.setIntTypeIdText(odooIntType.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_PICKING_TYPE__IN_TYPE_ID]
        if(!ObjectUtils.isEmpty(et.getInTypeId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_picking_type odooInType=et.getOdooInType();
            if(ObjectUtils.isEmpty(odooInType)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_picking_type majorEntity=stockPickingTypeService.get(et.getInTypeId());
                et.setOdooInType(majorEntity);
                odooInType=majorEntity;
            }
            et.setInTypeIdText(odooInType.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_PICKING_TYPE__MANU_TYPE_ID]
        if(!ObjectUtils.isEmpty(et.getManuTypeId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_picking_type odooManuType=et.getOdooManuType();
            if(ObjectUtils.isEmpty(odooManuType)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_picking_type majorEntity=stockPickingTypeService.get(et.getManuTypeId());
                et.setOdooManuType(majorEntity);
                odooManuType=majorEntity;
            }
            et.setManuTypeIdText(odooManuType.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_PICKING_TYPE__OUT_TYPE_ID]
        if(!ObjectUtils.isEmpty(et.getOutTypeId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_picking_type odooOutType=et.getOdooOutType();
            if(ObjectUtils.isEmpty(odooOutType)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_picking_type majorEntity=stockPickingTypeService.get(et.getOutTypeId());
                et.setOdooOutType(majorEntity);
                odooOutType=majorEntity;
            }
            et.setOutTypeIdText(odooOutType.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_PICKING_TYPE__PACK_TYPE_ID]
        if(!ObjectUtils.isEmpty(et.getPackTypeId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_picking_type odooPackType=et.getOdooPackType();
            if(ObjectUtils.isEmpty(odooPackType)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_picking_type majorEntity=stockPickingTypeService.get(et.getPackTypeId());
                et.setOdooPackType(majorEntity);
                odooPackType=majorEntity;
            }
            et.setPackTypeIdText(odooPackType.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_PICKING_TYPE__PBM_TYPE_ID]
        if(!ObjectUtils.isEmpty(et.getPbmTypeId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_picking_type odooPbmType=et.getOdooPbmType();
            if(ObjectUtils.isEmpty(odooPbmType)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_picking_type majorEntity=stockPickingTypeService.get(et.getPbmTypeId());
                et.setOdooPbmType(majorEntity);
                odooPbmType=majorEntity;
            }
            et.setPbmTypeIdText(odooPbmType.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_PICKING_TYPE__PICK_TYPE_ID]
        if(!ObjectUtils.isEmpty(et.getPickTypeId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_picking_type odooPickType=et.getOdooPickType();
            if(ObjectUtils.isEmpty(odooPickType)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_picking_type majorEntity=stockPickingTypeService.get(et.getPickTypeId());
                et.setOdooPickType(majorEntity);
                odooPickType=majorEntity;
            }
            et.setPickTypeIdText(odooPickType.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_PICKING_TYPE__SAM_TYPE_ID]
        if(!ObjectUtils.isEmpty(et.getSamTypeId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_picking_type odooSamType=et.getOdooSamType();
            if(ObjectUtils.isEmpty(odooSamType)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_picking_type majorEntity=stockPickingTypeService.get(et.getSamTypeId());
                et.setOdooSamType(majorEntity);
                odooSamType=majorEntity;
            }
            et.setSamTypeIdText(odooSamType.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_RULE__BUY_PULL_ID]
        if(!ObjectUtils.isEmpty(et.getBuyPullId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_rule odooBuyPull=et.getOdooBuyPull();
            if(ObjectUtils.isEmpty(odooBuyPull)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_rule majorEntity=stockRuleService.get(et.getBuyPullId());
                et.setOdooBuyPull(majorEntity);
                odooBuyPull=majorEntity;
            }
            et.setBuyPullIdText(odooBuyPull.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_RULE__MANUFACTURE_PULL_ID]
        if(!ObjectUtils.isEmpty(et.getManufacturePullId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_rule odooManufacturePull=et.getOdooManufacturePull();
            if(ObjectUtils.isEmpty(odooManufacturePull)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_rule majorEntity=stockRuleService.get(et.getManufacturePullId());
                et.setOdooManufacturePull(majorEntity);
                odooManufacturePull=majorEntity;
            }
            et.setManufacturePullIdText(odooManufacturePull.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_RULE__MTO_PULL_ID]
        if(!ObjectUtils.isEmpty(et.getMtoPullId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_rule odooMtoPull=et.getOdooMtoPull();
            if(ObjectUtils.isEmpty(odooMtoPull)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_rule majorEntity=stockRuleService.get(et.getMtoPullId());
                et.setOdooMtoPull(majorEntity);
                odooMtoPull=majorEntity;
            }
            et.setMtoPullIdText(odooMtoPull.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_RULE__PBM_MTO_PULL_ID]
        if(!ObjectUtils.isEmpty(et.getPbmMtoPullId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_rule odooPbmMtoPull=et.getOdooPbmMtoPull();
            if(ObjectUtils.isEmpty(odooPbmMtoPull)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_rule majorEntity=stockRuleService.get(et.getPbmMtoPullId());
                et.setOdooPbmMtoPull(majorEntity);
                odooPbmMtoPull=majorEntity;
            }
            et.setPbmMtoPullIdText(odooPbmMtoPull.getName());
        }
        //实体关系[DER1N_STOCK_WAREHOUSE__STOCK_RULE__SAM_RULE_ID]
        if(!ObjectUtils.isEmpty(et.getSamRuleId())){
            cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_rule odooSamRule=et.getOdooSamRule();
            if(ObjectUtils.isEmpty(odooSamRule)){
                cn.ibizlab.businesscentral.core.odoo_stock.domain.Stock_rule majorEntity=stockRuleService.get(et.getSamRuleId());
                et.setOdooSamRule(majorEntity);
                odooSamRule=majorEntity;
            }
            et.setSamRuleIdText(odooSamRule.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_warehouse> getStockWarehouseByIds(List<String> ids) {
         return this.listByIds(ids);
    }

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



}



