1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515 |
- <?php
- /**
- * 商品基础数据模型
- * Created by PhpStorm.
- * User: XiaoMing
- * Date: 2019/10/30
- * Time: 14:26
- */
- namespace JinDouYun\Model\GoodsManage;
- use JinDouYun\Cache\PageCache;
- use JinDouYun\Controller\Common\Logger;
- use JinDouYun\Dao\GoodsManage\DSku;
- use JinDouYun\Dao\Shop\DShop;
- use JinDouYun\Model\Merchant\MMerchant;
- use JinDouYun\Model\Purchase\MSupplier;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use Jindouyun\Cache\RoleAclCache;
- use JinDouYun\Cache\SpecCache;
- use JinDouYun\Cache\GoodsBasicCache;
- use JinDouYun\Cache\GoodsBasicRelevant;
- use JinDouYun\Cache\OverviewCache;
- use JinDouYun\Dao\GoodsManage\DGoodsBrand;
- use JinDouYun\Dao\GoodsManage\DGoodsBasic;
- use JinDouYun\Dao\GoodsCategory\DGoodsCategory;
- use JinDouYun\Model\Goods\BasicAndSkuCache;
- use JinDouYun\Model\Stock\MInventory;
- use JinDouYun\Model\MBaseModel;
- use JinDouYun\Model\Shop\MShop;
- use JinDouYun\Model\GoodsCategory\MGoodsCategory;
- use Util\Common\ChineseCharacter;
- class MGoodsBasic extends MBaseModel
- {
- /**
- * BasicAndSkuCache
- */
- use BasicAndSkuCache;
- /**
- * @var DGoodsBasic
- */
- private $objDGoodsBasic;
- /**
- * @var MSku
- */
- private $objMSku;
- /**
- * @var int
- */
- private $onlineUserId;
- /**
- * @var
- */
- private $onlineEnterpriseId;
- /**
- * @var GoodsBasicCache
- */
- private $objGoodsBasicCache;
- /**
- * @var OverviewCache
- */
- private $objOverviewCache;
- /**
- * @var SpecCache
- */
- private $objSpecCache;
- /**
- * @var MInventory
- */
- private $objMInventory;
- /**
- * @var GoodsBasicRelevant
- */
- private $objGoodsBasicRelevant;
- /**
- * @var DGoodsCategory
- */
- private $objDGoodsCategory;
- /**
- * @var DGoodsBrand
- */
- private $objDGoodsBrand;
- /**
- * @var DShop
- */
- private $objDShop;
- /**
- * @var DSku
- */
- private $objDSku;
- /**
- * MGoodsBasic constructor.
- * @param $onlineUserId
- * @param $onlineEnterpriseId
- */
- public function __construct($onlineUserId, $onlineEnterpriseId)
- {
- $this->onlineUserId = $onlineUserId;
- $this->onlineEnterpriseId = $onlineEnterpriseId;
- parent::__construct($this->onlineEnterpriseId, $this->onlineUserId);
- $this->objMSku = new MSku($this->onlineUserId, $this->onlineEnterpriseId);
- $this->objDGoodsBasic = new DGoodsBasic('default');
- $this->objOverviewCache = new OverviewCache();
- $this->objDGoodsBasic->setTable($this->objDGoodsBasic->get_Table() . '_' . $onlineEnterpriseId);
- $this->objDGoodsBasic->setSearchIndex('goods_basic_search')->setType('goodsBasic');
- $this->objGoodsBasicCache = new GoodsBasicCache($this->onlineEnterpriseId);
- $this->objGoodsBasicRelevant = new GoodsBasicRelevant($this->onlineEnterpriseId);
- $this->objSpecCache = new SpecCache($this->onlineEnterpriseId);
- $this->objMInventory = new MInventory($this->onlineEnterpriseId, $this->onlineUserId);
- $this->objDGoodsCategory = new DGoodsCategory();
- $this->objDGoodsCategory->setTable('qianniao_goods_category_'.$this->onlineEnterpriseId);
- $this->objDGoodsBrand = new DGoodsBrand();
- $this->objDGoodsBrand->setTable('qianniao_goods_brand_'.$this->onlineEnterpriseId);
- $this->objDShop = new DShop();
- $this->objDShop->setTable('qianniao_shop_1');
- $this->objDSku = new DSku();
- $this->objDSku->setTable('qianniao_sku_'.$this->onlineEnterpriseId);
- }
- /**
- * @param $id
- * @return string
- */
- public function getGoodsBasicSubTableName()
- {
- return $this->objDGoodsBasic->get_Table();
- }
- /**
- * specType => 2
- * 添加多规格基础商品资料
- * @param $params
- * @return ResultWrapper
- */
- public function addMultipleSpecGoodsBasic($params)
- {
- $unitData = $params['unitData'];
- $specMultiple = $params['specMultiple'];
- unset($params['unitData']);
- unset($params['specMultiple']);
- $this->objDGoodsBasic->beginTransaction();
- $params['addUserId'] = $this->onlineUserId;//创建人id
- $objRoleAclCache = new RoleAclCache();
- $staffId = $objRoleAclCache->getStaffIdAndUserCenterId($this->onlineEnterpriseId, $this->onlineUserId);
- $params['salesManId'] = empty($staffId) ? 0 : $staffId;
- //商品名称转换搜索条件
- $objChineseCharacter = new ChineseCharacter();
- $params['condition'] = $objChineseCharacter->getInitials(trim($params['title']));
- $dbResult = $this->objDGoodsBasic->insert($params);
- $goodsBasicId = $dbResult;
- if ($dbResult === false) {
- $this->objDGoodsBasic->rollBack();
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- if (!empty($unitData)) {
- $insert = [];
- foreach ($unitData as $unit) {
- $specGroupIds = [];
- $hash = '';
- foreach ($specMultiple as $spec) {
- $specGroupIds = array_column($spec['specGroup'], 'specValueId');
- $hash = md5(implode('|', $specGroupIds));
- $insert[] = [
- 'goodsId' => $dbResult,
- 'specData' => json_encode($spec['specGroup']),
- 'specImage' => json_encode($spec['specImage']),
- 'specType' => $params['specType'],
- 'unitId' => $unit['unitId'],
- 'isMaster' => $unit['isMaster'],
- 'unitName' => $unit['unitName'],
- 'conversion' => isset($unit['conversion']) ? $unit['conversion'] : 0,
- 'specGroupHash' => $hash,
- 'barCode' => isset($spec['barCode'][$unit['unitId']]) ? $spec['barCode'][$unit['unitId']] : '',
- 'isDefault' => isset($spec['isDefault'][$unit['unitId']]) ? $spec['isDefault'][$unit['unitId']] : StatusCode::$delete,
- 'weight' => isset($spec['weight'][$unit['unitId']]) ? $spec['weight'][$unit['unitId']] : 0,
- 'isNew' => StatusCode::$standard,
- ];
- }
- }
- $result = $this->objMSku->addSku($insert, true);
- if (!$result->isSuccess()) {
- $this->objDGoodsBasic->rollBack();
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- unset($insert);
- }
- //缓存商品分类下基础商品数量
- $categoryIds = explode(',', $params['categoryPath']);
- foreach ($categoryIds as $v) {
- $this->objGoodsBasicCache->categoryKeyScoreIncr($v);
- }
- $this->objGoodsBasicCache->brandKeyScoreIncr($params['brandId']);
- //缓存商品数据
- $this->isUpdateBasicCache = true;
- $this->objectId = $goodsBasicId;
- $this->objDGoodsBasic->commit();
- //self::updateEsData($dbResult);
- return ResultWrapper::success($dbResult);
- }
- /**
- * specType => 1
- * 添加单规格商品基础数据
- * @param $params
- * @return ResultWrapper
- * @throws \Exception
- */
- public function addGoodsBasic($params)
- {
- $unitData = $params['unitData'];
- unset($params['unitData']);
- $beginStatus = $this->objDGoodsBasic->beginTransaction();
- $params['addUserId'] = $this->onlineUserId;//创建人id
- $objRoleAclCache = new RoleAclCache();
- $staffId = $objRoleAclCache->getStaffIdAndUserCenterId($this->onlineEnterpriseId, $this->onlineUserId);
- $params['salesManId'] = empty($staffId) ? 0 : $staffId;
- //商品名称转换搜索条件
- $objChineseCharacter = new ChineseCharacter();
- $params['condition'] = $objChineseCharacter->getInitials(trim($params['title']));
- $dbResult = $this->objDGoodsBasic->insert($params);
- $goodsBasicId = $dbResult;
- if ($dbResult === false) {
- $this->objDGoodsBasic->rollBack();
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- if (!empty($unitData)) {
- $insert = [];
- foreach ($unitData as $key => $unit) {
- $insert[] = [
- 'unitName' => $unit['unitName'],
- 'unitId' => $unit['unitId'],//单位ID
- 'goodsId' => $dbResult,
- 'isMaster' => $unit['isMaster'],
- 'conversion' => isset($unit['conversion']) ? $unit['conversion'] : 0,
- 'specType' => StatusCode::$specType['single'],//无属性
- 'barCode' => isset($unit['barCode']) ? $unit['barCode'] : '',
- 'isDefault' => isset($unit['isDefault']) ? $unit['isDefault'] : StatusCode::$delete,
- 'weight' => isset($unit['weight']) ? $unit['weight'] : '',
- 'isNew' => StatusCode::$standard,
- ];
- }
- $result = $this->objMSku->addSku($insert, true);
- if (!$result->isSuccess()) {
- $this->objDGoodsBasic->rollBack();
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- }
- //缓存商品分类下基础商品数量
- $categoryIds = explode(',', $params['categoryPath']);
- foreach ($categoryIds as $v) {
- $this->objGoodsBasicCache->categoryKeyScoreIncr($v);
- }
- $this->objGoodsBasicCache->brandKeyScoreIncr($params['brandId']);
- //缓存商品数据
- //缓存商品数据
- $this->isUpdateBasicCache = true;
- $this->objectId = $goodsBasicId;
- $beginStatus && $this->objDGoodsBasic->commit();
- //self::updateEsData($dbResult);
- return ResultWrapper::success($dbResult);
- }
- private function createEsDocumentId($goodsBasicId)
- {
- return 'EnterpriseId_' . $this->onlineEnterpriseId . '_GoodsBasicId_' . $goodsBasicId;
- }
- /**
- * 更新ES
- * @param $goodsBasicId
- */
- public function updateEsData($goodsBasicId)
- {
- //每次查询一遍,避免商品在数据库存在,在ES中不存在的情况
- $goodsBasic = $this->objDGoodsBasic->get_by('id', $goodsBasicId);
- if ($goodsBasic !== false && !empty($goodsBasic)) {
- $goodsBasic['goodsCode'] = createCode(StatusCode::$code['goodsBasic']['prefix'], $goodsBasicId, StatusCode::$code['goodsBasic']['length']);//商品编码
- $goodsBasic['enterpriseId'] = $this->onlineEnterpriseId;
- $goodsBasic['images'] = json_decode($goodsBasic['images'], true);
- //处理sku数据
- $skuInfo = $this->objMSku->getSkuInfo(['goodsId' => [$goodsBasicId]]);
- if ($skuInfo->isSuccess()) {
- $skuData = $skuInfo->getData();
- $goodsBasic['skuData'] = array_shift($skuData);
- }
- /*
- $_id = self::createEsDocumentId($goodsBasicId);
- $result = $this->objDGoodsBasic->addUpSearchIndexDocument($goodsBasic, $_id);
- if (isset($result['_shards']) && isset($result['_shards']['successful']) && $result['_shards']['successful'] == 1) {
- //echo "es操作成功";die;
- } else {
- file_put_contents('/www/wwwroot/logs/api.junhailan.com/elasticsearch.log', date('Y-m-d H:i:s') . '操作商品资料es错误,错误原因' . var_export($result, true) . PHP_EOL, FILE_APPEND);
- }*/
- }
- }
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2020/8/22
- * Time: 4:29 下午
- * @param array $data
- * @param string $source
- * @return ResultWrapper
- */
- private function getExportDataBySelectParams($data,string $source)
- {
- if (empty($data)){
- return ResultWrapper::success([]);
- }
- switch ($source){
- case 'db':
- $result = self::getGoodsBasicBySelectParams($data);
- break;
- case 'es':
- $allIds = [];
- foreach ($data as $value){
- $allIds[] = $value['id'];
- }
- $result = self::getGoodsBasicBySelectParams(['id' => $allIds]);
- break;
- }
- if (!$result->isSuccess()){
- return ResultWrapper::fail($result->getData(),$result->getErrorCode());
- }
- return ResultWrapper::success($result->getData());
- }
- /**
- * Doc: (des="获取export查询方法返回的数据集合")
- * User: XMing
- * Date: 2020/8/22
- * Time: 11:42 上午
- * @param $object
- * @return array
- */
- private static function getGeneratorData($object)
- {
- $arrayList = [];
- foreach ($object as $value){
- $arrayList[] = $value;
- }
- return $arrayList;
- }
- /**
- * Doc: (des="基础商品资料导出")
- * User: XMing
- * Date: 2020/8/22
- * Time: 5:54 下午
- * @param array $data
- * @param string $source
- * @return ResultWrapper
- */
- private function exportGoodsBasic($data,string $source)
- {
- $listResult = self::getExportDataBySelectParams($data,$source);
- if (!$listResult->isSuccess()){
- return ResultWrapper::fail($listResult->getData(),$listResult->getErrorCode());
- }
- $list = $listResult->getData();
- if (empty($list)){
- return ResultWrapper::success([]);
- }
- $formatListsResult = self::formatExportList($list);
- if (!$formatListsResult->isSuccess()){
- return ResultWrapper::fail($formatListsResult->getData(),$formatListsResult->getErrorCode());
- }
- $formatLists = $formatListsResult->getData();
- self::exportCsv($formatLists);
- }
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2020/8/24
- * Time: 4:01 下午
- * @param array $result
- */
- private function exportCsv(array $result)
- {
- //导出到本地
- header("Content-type:application/vnd.ms-excel");
- header("Content-Disposition:filename=GoodsBasicCsv.csv");
- header('Cache-Control: max-age=0');
- $fp = fopen('php://output', 'a');
- $head = ['ID', '商品编码', '商品名称', '商品分类', '商品品牌','发布来源','状态', '保质期(天)', '创建时间',
- '商品卖点', '货架编码', '禁止销售店铺', '关键词', '计量单位', '规格', '主单位(主/辅)', '主辅换算比率', '商品条码'];//定义标题
- foreach ($head as $i => $v) {
- $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8'); //将中文标题转换编码,否则乱码
- }
- fputcsv($fp, $head);
- $limit = 10000;
- $num = 0;//计数器
- $rows = [];
- foreach ($result as $v) {
- $num++;
- if($num == $limit){
- ob_flush(); //释放内存
- flush();
- }
- foreach ($v['specMultiple'] as $index => $item){
- self::buildExportFinalData($rows,$v,$item,$index);
- foreach ($rows as $kk => $vv) {
- $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码
- }
- fputcsv($fp, $rs);
- $rows = [];
- }
- }
- exit;
- }
- /**
- * Doc: (des="映射导出的数据")
- * User: XMing
- * Date: 2020/8/24
- * Time: 4:20 下午
- * @param $rows
- * @param array $v
- * @param array $item
- * @param int $index
- */
- private function buildExportFinalData(&$rows,array $v,array $item,int $index)
- {
- $rows['id'] = '';
- $rows['goodsCode'] = '';
- $rows['goodsName'] = '';
- $rows['categoryName'] = '';
- $rows['brandName'] = '';
- $rows['sourceMsg'] ='';
- $rows['enableStatusMsg'] = '';
- $rows['expireTime'] = '';
- $rows['createTime'] = '';
- $rows['describe'] = '';
- $rows['storageCode'] = '';
- $rows['noSalesShopNames'] = '';
- $rows['tag'] = '';
- if ($index == 0){
- $rows['id'] = $v['id'];
- $rows['goodsCode'] = $v['goodsCode'];
- $rows['goodsName'] = $v['title'];
- $rows['categoryName'] = $v['categoryName'];
- $rows['brandName'] = $v['brandName'];
- $rows['sourceMsg'] =$v['sourceMsg'];
- $rows['enableStatusMsg'] = $v['enableStatusMsg'];
- $rows['expireTime'] = $v['expireTime'];
- $rows['createTime'] = $v['createTime'];
- $rows['describe'] = $v['describe'];
- $rows['storageCode'] = $v['storage'];
- $rows['noSalesShopNames'] = $v['noSalesShopNames'];
- $rows['tag'] = $v['tag'];
- }
- $rows['unitName'] = $item['unitName'];
- $rows['specGroup'] = $item['specGroup'];
- $rows['skuMsg'] = $item['skuMsg'];
- $rows['conversion'] = $item['conversion'];
- $rows['barCode'] = $item['barCode'];
- }
- /**
- * Doc: (des="格式化导出数据")
- * User: XMing
- * Date: 2020/8/22
- * Time: 6:12 下午
- * @param array $data
- * @return ResultWrapper
- */
- private function formatExportList(array $data)
- {
- if (empty($data)){
- return ResultWrapper::success([]);
- }
- $allCategoryIds = [];
- $allBrandIds = [];
- $allNoSalesShopIds = [];
- $allBasicIds = [];
- $merchantIds = [];
- $shopIds = [];
- $supplierIds = [];
- foreach ($data as $value){
- $allBasicIds[] = $value['id'];
- $allCategoryIds[] = $value['categoryId'];
- $merchantIds[] = $value['merchantId'];
- if (!empty($value['brandId'])){
- $allBrandIds[] = $value['brandId'];
- }
- if (!empty($value['noSalesShop'])){
- $array = explode(',',$value['noSalesShop']);
- $allNoSalesShopIds = array_merge($allNoSalesShopIds,$array);
- }
- if ($value['noSalesShop']) {
- $shopIds = array_merge($shopIds, explode(',', $value['noSalesShop']));
- }
- !empty($value['storeId']) && $shopIds[] = $value['storeId'];
- !empty($value['supplierId']) && $supplierIds[] = $value['supplierId'];
- }
- $allCategoryIds = array_values(array_unique($allCategoryIds));
- $allBrandIds = array_values(array_unique($allBrandIds));
- $allNoSalesShopIds = array_values(array_unique($allNoSalesShopIds));
- $merchantIds = array_values(array_unique($merchantIds));
- //查询分类
- $categoryMap = [];
- if (!empty($allCategoryIds)){
- $categoryLists = $this->objDGoodsCategory->exportSelect(['id' => $allCategoryIds],'id,title');
- if ($categoryLists === false){
- return ResultWrapper::fail($this->objDGoodsCategory->error(),ErrorCode::$dberror);
- }
- $categoryLists = self::getGeneratorData($categoryLists);
- foreach ($categoryLists as $value){
- $categoryMap[$value['id']] = $value['title'];
- }
- unset($value);
- unset($categoryLists);
- }
- //查询品牌
- $brandMap = [];
- if (!empty($allBrandIds)){
- $brandLists = $this->objDGoodsBrand->exportSelect(['id' => $allBrandIds],'id,title');
- if ($brandLists === false){
- return ResultWrapper::fail($this->objDGoodsBrand->error(),ErrorCode::$dberror);
- }
- $brandLists = self::getGeneratorData($brandLists);
- foreach ($brandLists as $value){
- $brandMap[$value['id']] = $value['title'];
- }
- unset($value);
- unset($brandLists);
- }
- //查询店铺
- $shopMap = [];
- if (!empty($allNoSalesShopIds)){
- $shopLists = $this->objDShop->exportSelect(['id'=>$allNoSalesShopIds],'id,name');
- if ($shopLists === false){
- return ResultWrapper::fail($this->objDShop->error(),ErrorCode::$dberror);
- }
- $shopLists = self::getGeneratorData($shopLists);
- foreach ($shopLists as $value){
- $shopMap[$value['id']] = $value['name'];
- }
- unset($value);
- unset($shopLists);
- }
- //查询sku
- $skuMap = [];
- if (!empty($allBasicIds)){
- $skuLists = $this->objDSku->exportSelect(['goodsId' => $allBasicIds,'deleteStatus' => StatusCode::$standard],'id,goodsId,conversion,unitName,isMaster,specData,barCode');
- if ($skuLists === false){
- return ResultWrapper::fail($this->objDSku->error(),ErrorCode::$dberror);
- }
- $skuLists = self::getGeneratorData($skuLists);
- foreach ($skuLists as &$value){
- $value['skuMsg'] = $value['isMaster'] == StatusCode::$standard ? '主' : '辅';
- $value['specGroup'] = empty($value['specData']) ? '无' : self::createSpecName(json_decode($value['specData'],true));
- $skuMap[$value['goodsId']][] = $value;
- }
- unset($value);
- unset($skuLists);
- }
- //查询发布来源(商户)
- if(!empty($merchantIds)){
- $objMMerchant = new MMerchant($this->onlineEnterpriseId, $this->onlineUserId);
- $modelResult = $objMMerchant->selectMerchantData(['id' => $merchantIds, 'deleteStatus' => StatusCode::$standard]);
- if(!$modelResult->isSuccess()){
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- $merchantResult = $modelResult->getData();
- unset($modelResult);
- $merchantData =[];
- foreach($merchantResult as $value){
- $merchantData[$value['id']] = $value;
- }
- }
-
- //商铺
- $objMShop = new MShop($this->onlineEnterpriseId, $this->onlineUserId);
- $shops = $objMShop->getShopName(array_values(array_unique($shopIds)));
-
-
- //供应商
- $objMSupplier = new MSupplier($this->onlineUserId,$this->onlineEnterpriseId);
- $supplierResult = $objMSupplier->getSupplierNameByIds($supplierIds);
- $supplierMap = [];
- if ($supplierResult->isSuccess()){
- $supplierMap = MSupplier::supplierMap($supplierResult->getData());
- }
-
- foreach ($data as $key => &$value){
- $value['categoryName'] = isset($categoryMap[$value['categoryId']]) ? $categoryMap[$value['categoryId']] : '无';
- $value['brandName'] = isset($brandMap[$value['brandId']]) ? $brandMap[$value['brandId']] : '无';
- if (empty($value['noSalesShop'])){
- $value['noSalesShopNames'] = '';
- }else{
- $noSalesShopStr = '';
- $noSalesShopArr = explode(',',$value['noSalesShop']);
- foreach ($noSalesShopArr as $shopId){
- !empty($noSalesShopStr) && $noSalesShopStr .= ',';
- $noSalesShopStr .= isset($shopMap[$shopId]) ? $shopMap[$shopId] : '无';
- }
- $value['noSalesShopNames'] = $noSalesShopStr;
- }
- $value['specMultiple'] = isset($skuMap[$value['id']]) ? $skuMap[$value['id']] : [];
- $value['goodsCode'] = createCode(StatusCode::$code['goodsBasic']['prefix'], $value['id'], StatusCode::$code['goodsBasic']['length']);;
-
- $data[$key]['sourceMsg'] = '平台';
- if (isset($value['storeId']) && !empty($value['storeId'])){
- $data[$key]['sourceMsg'] = getArrayItem($shops,$value['storeId'],'');
- }
- if (isset($value['supplierId']) && !empty($value['supplierId'])){
- $row = getArrayItem($supplierMap,$value['supplierId'],[]);
- if (!empty($row)){
- $data[$key]['sourceMsg'] = getArrayItem($row,'title','');
- }
- }
- $data[$key]['merchantName'] = '';
- if(isset($value['merchantId']) && !empty($value['merchantId']) && isset($merchantData[$value['merchantId']])){
- $data[$key]['sourceMsg'] = $merchantData[$value['merchantId']]['name'];
- $data[$key]['merchantName'] = $merchantData[$value['merchantId']]['name'];
- }
-
- $value['enableStatusMsg'] = $value['enableStatus'] == StatusCode::$standard ? '启用' : '禁用';
- $value['createTime'] = date('Y-m-d H:i:s',$value['createTime']);
- $value['storage'] = '无';
- if (!empty($value['extends'])){
- $value['storage'] = empty($value['extends']) ? '' : json_decode($value['extends'],true)['storage'];
- }
- }
- return ResultWrapper::success($data);
- }
- /**
- * Doc: (des="生成规格名字")
- * User: XMing
- * Date: 2020/8/22
- * Time: 2:47 下午
- * @param array $specGroup
- * @return string
- */
- private static function createSpecName(array $specGroup)
- {
- $str = '';
- foreach ($specGroup as $value){
- empty($str) && $str .= ' ';
- if (!empty($value['specName'])){
- $str .= $value['specName'].':';
- }
- if (!empty($value['specValueName'])){
- $str .= $value['specValueName'];
- }
- }
- return empty($str) ? '无' : $str;
- }
- /**
- * Doc: (des="导出查询数据")
- * User: XMing
- * Date: 2020/8/22
- * Time: 4:58 下午
- * @param array $selectParams
- * @return ResultWrapper
- */
- private function getGoodsBasicBySelectParams(array $selectParams)
- {
- $goodsBasicLists = $this->objDGoodsBasic->exportSelect($selectParams,'*','createTime DESC');
- if ($goodsBasicLists === false){
- return ResultWrapper::fail($this->objDGoodsBasic->error(),ErrorCode::$dberror);
- }
- $goodsBasicLists = self::getGeneratorData($goodsBasicLists);
- return ResultWrapper::success($goodsBasicLists);
- }
- /**
- * 获取全部商品基础数据列表
- * @param $selectParams
- * @param boolean $is_export
- * @return ResultWrapper
- * @throws \Exception
- */
- public function getAllGoodsBasic($selectParams,$is_export = false)
- {
- $limit = $selectParams['limit'];
- unset($selectParams['limit']);
- $offset = $selectParams['offset'];
- unset($selectParams['offset']);
- // $selectParams = parent::getSalesManQueryParams($selectParams, 'salesManId');
- $selectParams['deleteStatus'] = StatusCode::$standard;
- if ($is_export === true) self::exportGoodsBasic($selectParams,'db');
- $dbResult = $this->objDGoodsBasic->select($selectParams, '*', 'id desc', $limit, $offset);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- $total = $this->objDGoodsBasic->count($selectParams);
- $format = self::formatJson($dbResult);
- $format = self::formatCategory($format);
- foreach ($format as &$formatData) {
- $formatData['unitData'] = [];
- $formatData['specGroup'] = [];
- $formatData['specMultiple'] = [];
- $formatData = self::formatSingleSkuMapping($formatData);
- $formatData = self::formatMultipleSkuMapping($formatData);
- }
- $return = [
- 'data' => $format,
- 'total' => ($total) ? intval($total) : 0,
- ];
- return ResultWrapper::success($return);
- }
- public function fixEsData($selectParams)
- {
- $limit = $selectParams['limit'];
- unset($selectParams['limit']);
- $offset = $selectParams['offset'];
- unset($selectParams['offset']);
- //$selectParams['deleteStatus'] = StatusCode::$standard;
- $dbResult = $this->objDGoodsBasic->select($selectParams, 'id', null, $limit, $offset);
- foreach ($dbResult as $goods) {
- self::updateEsData($goods['id']);
- }
- print_r(count($dbResult) . '条数据');
- }
- /**
- * 获取商铺下允许销售的商品
- * @param $selectParams
- * @return ResultWrapper
- * @throws \Exception
- */
- public function getGoodsBasicOfShopId($selectParams)
- {
- $limit = $selectParams['limit'];
- $offset = $selectParams['offset'];
- /*
- $where = 'deleteStatus = ' . StatusCode::$standard;
- $where .= ' AND !FIND_IN_SET(' . $selectParams['shopId'] . ',noSalesShop)';
- if (isset($selectParams['title'])) {
- $where .= ' AND title LIKE "%' . $selectParams['title'] . '%"';
- }
- $dbResult = $this->objDGoodsBasic->select($where, '*', 'createTime desc', $limit, $offset);*/
- //查询已经发布的基础物料id
- $sql = 'SELECT `basicGoodsId` from qianniao_goods_' . $this->onlineEnterpriseId . ' WHERE `shopId` = ' . $selectParams['shopId'] . ' AND `deleteStatus` = '.StatusCode::$standard;
- $result = $this->objDGoodsBasic->query($sql);
- if ($result === false){
- return ResultWrapper::fail($this->objDGoodsBasic->error(),ErrorCode::$dberror);
- }
- unset($sql);
- $havingIds = [];
- foreach ($result as $item) {
- $havingIds[] = $item['basicGoodsId'];
- }
- $sql = 'SELECT `id` from qianniao_goods_basic_'.$this->onlineEnterpriseId.' WHERE FIND_IN_SET(' . $selectParams['shopId'] . ',`noSalesShop`)';
- $result = $this->objDGoodsBasic->query($sql);
- if ($result === false){
- return ResultWrapper::fail($this->objDGoodsBasic->error(),ErrorCode::$dberror);
- }
- unset($sql);
- $noSaleIds = [];
- foreach ($result as $item){
- $noSaleIds[] = $item['id'];
- }
- $notIds = array_merge($havingIds,$noSaleIds);
- $sql = 'SELECT * FROM qianniao_goods_basic_' . $this->onlineEnterpriseId . ' WHERE `enableStatus` = ' . StatusCode::$standard . ' AND `deleteStatus` = ' . StatusCode::$standard;
- if (!empty($notIds)){
- $notIds = trim(implode(',',$notIds),',');
- $sql .= ' AND id NOT IN ('. $notIds .')';
- }
- $sql .= ' AND level = '.StatusCode::$standard;
- if (isset($selectParams['title'])) {
- $sql .= ' AND title LIKE "%' . $selectParams['title'] . '%" ';
- }
- //筛选分类
- if (isset($selectParams['categoryId']) && !empty($selectParams['categoryId'])){
- $sql .= ' AND find_in_set('.$selectParams['categoryId'].',categoryPath) ';
- }
- $sql .= ' order by createTime desc limit ' . $offset . ',' . $limit;
- $dbResult = $this->objDGoodsBasic->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- $sql = 'select count(id) as total from qianniao_goods_basic_' . $this->onlineEnterpriseId . ' where id not in(select basicGoodsId from qianniao_goods_' . $this->onlineEnterpriseId . ' where shopid = ' . $selectParams['shopId'] . ' AND deleteStatus = '.StatusCode::$standard.' ) and !FIND_IN_SET(' . $selectParams['shopId'] . ',noSalesShop) and enableStatus = ' . StatusCode::$standard . ' and deleteStatus =' . StatusCode::$standard;
- $sql .= ' AND level = '.StatusCode::$standard;
- if (isset($selectParams['title'])) {
- $sql .= ' AND title LIKE "%' . $selectParams['title'] . '%"';
- }
- //筛选分类
- if (isset($selectParams['categoryId']) && !empty($selectParams['categoryId'])){
- $sql .= ' AND find_in_set('.$selectParams['categoryId'].',categoryPath) ';
- }
- $total = $this->objDGoodsBasic->query($sql);
- if ($total === false) {
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- $total = $total[0]['total'];
- $format = self::formatJson($dbResult);
- $format = self::formatCategory($format);
- foreach ($format as &$detail) {
- $detail['unitData'] = [];
- $detail = self::formatSingleSkuMapping($detail);
- }
- $return = [
- 'data' => $format,
- 'total' => ($total) ? intval($total) : 0,
- ];
- return ResultWrapper::success($return);
- }
- /**
- * 获取指定基础商品详情
- * @param $id
- * @param $isAddGoods
- * @return ResultWrapper
- * @throws \Exception
- */
- public function getGoodsBasicInfoById($id, $isAddGoods)
- {
- $dbResult = $this->objDGoodsBasic->get_by('id', $id);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- $formatData = self::formatJson($dbResult);
- $formatData = self::formatCategory($formatData);
- $formatData = array_shift($formatData);
- $formatData = self::formatSingleSkuMapping($formatData);
- $formatData = self::formatMultipleSkuMapping($formatData, $isAddGoods);
- $formatData = self::formatMultipleSkuIndex($formatData);
- $formatData = self::getSkuInventorySpec($formatData);
- return ResultWrapper::success($formatData);
- }
- /**
- * @param $data
- * @return mixed
- */
- private function getSkuInventorySpec($data)
- {
- //print_r(array_column($data['specMultiple'],'id'));die;
- $InventoryResult = $this->objMInventory->getInventoryBySkuIds(array_column($data['specMultiple'], 'id'));
- $inventoryData = [];
- if (!$InventoryResult->isSuccess()) {
- file_put_contents('/www/wwwroot/logs/api.junhailan.com/MGoodsBasic_error.log', date('Y-m-d H:i:s') . '错误信息|sql错误 :' . $InventoryResult->getData() . PHP_EOL, FILE_APPEND);
- } else {
- $inventoryData = $InventoryResult->getData();
- }
- foreach ($data['specMultiple'] as &$row) {
- if (isset($inventoryData[$row['id']])) {
- $row['isEditSpec'] = StatusCode::$delete;//采购过,不能编辑
- } else {
- $row['isEditSpec'] = StatusCode::$standard;
- }
- $row['inventory'] = isset($inventoryData[$row['id']]) ? $inventoryData[$row['id']] : 0;
- }
- //查询物料库存
- $materielResult = $this->objMInventory->getInventoryByMaterielIds([$data['id']]);
- $materielMap = [];
- if (!$materielResult->isSuccess()) {
- file_put_contents('/www/wwwroot/logs/api.junhailan.com/MGoodsBasic_error.log', date('Y-m-d H:i:s') . '错误信息|sql错误 :' . var_export($materielResult->getData()) . PHP_EOL, FILE_APPEND);
- } else {
- $materielMap = $materielResult->getData();
- }
- if (!isset($materielMap[$data['id']])) {
- //从来没有采购过
- $data['isEditSpec'] = StatusCode::$standard;
- } else {
- //采购过
- $data['isEditSpec'] = StatusCode::$delete;
- }
- return $data;
- }
- /**
- *
- */
- public static function formatMultipleSkuIndex($details)
- {
- //生成规格唯一索引
- $specMultiple = $details['specMultiple'];
- if (empty($specMultiple)) {
- return $details;
- }
- foreach ($specMultiple as &$spec) {
- $index = $details['id'] . ':' . $spec['unitId'];//商品ID
- //$subName = '';
- if (!empty($spec['specGroup'])) {
- foreach ($spec['specGroup'] as $row) {
- $index .= empty($index) ? $row['specValueId'] : ':' . $row['specValueId'];
- //$subName .= empty($subName) ? $row['specIdName'] . ':' . $row['specValueIdName'] : '-' . $row['specIdName'] . ':' . $row['specValueIdName'];
- //$subName .= empty($subName) ? $row['specValueIdName'] : ':' . $row['specValueIdName'];
- }
- }
- $spec['index'] = $index;
- $spec['indexGroup'] = explode(':', $index);
- //$spec['subName'] = $subName;
- }
- $details['specMultiple'] = $specMultiple;
- return $details;
- }
- /**
- * 将相同的辅单位合并在一起(兼容之前数据所以使用md5判断)
- * @param $data
- * @return array
- */
- public function branchMerge($data)
- {
- if (empty($data)) {
- return $data;
- }
- $hash = [];
- $sku = [];
- foreach ($data as $key => $unit) {
- $thisHash = md5($unit['conversion'] . $unit['unitName'] . $unit['unitId']);
- $hash[$thisHash]['skuIds'][] = $unit['id'];
- $hash[$thisHash]['sku'] = $unit;
- }
- $mapping = [];
- foreach ($hash as &$value) {
- $value['sku']['skuIds'] = $value['skuIds'];
- $mapping[] = $value['sku'];
- }
- return $mapping;
- }
- /**
- * 单位
- * @param $data
- * @return mixed
- * @throws \Exception
- */
- public function formatSingleSkuMapping($data)
- {
- if(isset($data['basicGoodsId'])){
- $id = $data['basicGoodsId'];
- }else{
- $id = $data['id'];
- }
- //计量单位
- $selectParams = [
- 'goodsId' => $data['id'],
- ];
- if (isset($data['basicGoodsId'])){
- $id = $data['basicGoodsId'];
- }else{
- $id = $data['id'];
- }
- $objMSku = new MSku($this->onlineUserId, $this->onlineEnterpriseId);
- $sql = "SELECT id,barCode,unitId,unitName,conversion,isMaster,barCode,specType,isDefault,`weight`,isNew,specGroupHash FROM qianniao_sku_" . $this->onlineEnterpriseId . " WHERE goodsId=" . $id . " AND deleteStatus=" . StatusCode::$standard . " ORDER BY isMaster DESC";
- $skuResult = $objMSku->getQuery($sql);
- $skuData = [];
- if ($skuResult->isSuccess()) {
- $skuData = $skuResult->getData();
- $haveMaster = 0;
- $newSkuData = [];
- $noMasterUnidIds = [];
- foreach ($skuData as $key => &$value) {
- if ($data['specType'] == StatusCode::$specType['multiple']) {
- $value['barCode'] = '';
- }
- if ($value['isMaster'] == StatusCode::$standard && $haveMaster == 0) {
- $newSkuData[] = $value;
- $haveMaster = 1;
- }
- if ($value['isMaster'] == StatusCode::$delete && !in_array($value['unitId'], $noMasterUnidIds)) {
- $newSkuData[] = $value;
- $noMasterUnidIds[] = $value['unitId'];
- }
- }
- unset($skuData);
- $skuData = $newSkuData;
- }
- $data['unitData'] = $skuData;
- return $data;
- }
- /**
- * @param $data
- * @param $isAddGoods
- * @return mixed
- * @throws \Exception
- */
- public function formatMultipleSkuMapping($data, $isAddGoods = 4)
- {
- switch ($isAddGoods) {
- case StatusCode::$standard:
- $sql = "SELECT id,unitId,unitName,conversion,isDefault,isMaster,specGroupHash,specImage,specData,barCode,`weight`,isNew FROM qianniao_sku_" . $this->onlineEnterpriseId . " WHERE goodsId=" . $data['id'] . " AND deleteStatus=" . StatusCode::$standard;
- break;
- case StatusCode::$delete:
- $sql = "SELECT id,unitId,unitName,conversion,isDefault,isMaster,specGroupHash,specImage,specData,barCode,`weight`,isNew FROM qianniao_sku_" . $this->onlineEnterpriseId . " WHERE goodsId=" . $data['id'] . " AND deleteStatus=" . StatusCode::$standard . " AND isMaster=" . StatusCode::$standard . " GROUP BY specGroupHash";
- break;
- }
- //$sql = "SELECT id,unitId,unitName,conversion,isMaster,specGroupHash,specImage,specData FROM qianniao_sku_" . $this->onlineEnterpriseId . " WHERE goodsId=" . $data['id'] . " AND deleteStatus=" . StatusCode::$standard . " AND isMaster=" . StatusCode::$standard . " GROUP BY specGroupHash";
- $skuResult = $this->objMSku->getQuery($sql);
- $skuData = [];
- if ($skuResult->isSuccess()) {
- $skuData = $skuResult->getData();
- }
- $specMultiple = [];
- foreach ($skuData as $spec) {
- $specGroup = json_decode($spec['specData'], true);
- $specMultiple[] = [
- 'id' => $spec['id'],
- 'unitId' => $spec['unitId'],
- 'unitName' => $spec['unitName'],
- 'isMaster' => $spec['isMaster'],
- 'conversion' => $spec['conversion'],
- 'specGroup' => $specGroup,
- 'specGroupHash' => $spec['specGroupHash'],
- 'specImage' => json_decode($spec['specImage'], true),
- 'barCode' => $spec['barCode'],
- 'isDefault' => $spec['isDefault'],
- 'weight' => $spec['weight'],
- 'isNew' => $spec['isNew']
- ];
- }
- $data['specMultiple'] = $specMultiple;
- if ($isAddGoods == StatusCode::$delete) $data = self::formatBarCode($data);
- return $data;
- }
- /**
- *
- */
- public function formatBarCode(array $data)
- {
- if (empty($data)) {
- return $data;
- }
- if ($data['specType'] == StatusCode::$specType['single']) {
- return $data;
- }
- $skuResult = $this->objMSku->getSkuByParams(
- [
- 'specGroupHash' => array_column($data['specMultiple'], 'specGroupHash'),
- 'goodsId' => $data['id'],
- 'deleteStatus' => StatusCode::$standard
- ]
- );
- if (!$skuResult->isSuccess()) {
- file_put_contents('/www/wwwroot/logs/api.junhailan.com/MGoodsBasic_error.log', date('Y-m-d H:i:s') . '错误原因|sql:' . $skuResult->getData() . PHP_EOL, FILE_APPEND);
- return $data;
- }
- $skuData = $skuResult->getData();
- $map = [];
- $isDefaultMap = [];
- $weightMap = [];
- foreach ($skuData as $item) {
- $map[$item['specGroupHash']][$item['unitId']] = $item['barCode'];
- $isDefaultMap[$item['specGroupHash']][$item['unitId']] = $item['isDefault'];
- $weightMap[$item['specGroupHash']][$item['unitId']] = $item['weight'];
- }
- foreach ($data['specMultiple'] as &$spec) {
- $spec['barCode'] = isset($map[$spec['specGroupHash']]) ? $map[$spec['specGroupHash']] : (object)[];
- $spec['isDefault'] = isset($isDefaultMap[$spec['specGroupHash']]) ? $isDefaultMap[$spec['specGroupHash']] : (object)[];
- $spec['weight'] = isset($weightMap[$spec['specGroupHash']]) ? $weightMap[$spec['specGroupHash']] : (object)[];
- }
- return $data;
- }
- /**
- * specType => 1
- * 编辑单规格基础商品数据
- * @param $params
- * @return ResultWrapper
- * @throws \Exception
- */
- public function editGoodsBasic($params)
- {
- if (empty($params['id'])) {
- return ResultWrapper::fail('没有指定要修改的商品数据id', ErrorCode::$paramError);
- }
- ;
- $id = $params['id'];
- unset($params['id']);
- $unitData = $params['unitData'];
- $delUnitIds = $params['delUnitIds'];
- $delSpecSkuIds = $params['delSpecSkuIds'];
- unset($params['delSpecSkuIds']);//属性删除
- unset($params['delUnitIds']);
- unset($params['unitData']);
- unset($params['merchantId']);
- $beginStatus = $this->objDGoodsBasic->beginTransaction();
- $objMSku = new MSku($this->onlineUserId, $this->onlineEnterpriseId);
- $dbResult = $objMSku->editSku($unitData, $id, $delUnitIds, $delSpecSkuIds);
- if (!$dbResult->isSuccess()) {
- $this->objDGoodsBasic->rollBack();
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- $dbResult = $this->objDGoodsBasic->update($params, ['id' => $id]);
- if ($dbResult === false) {
- $this->objDGoodsBasic->rollBack();
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- //缓存商品名称
- $this->isUpdateBasicCache = true;
- $this->objectId = $id;
- $beginStatus && $this->objDGoodsBasic->commit();
- //self::updateEsData($id);
- $this->objMSku->delSkuCache($id);
- // 每编辑一件商品需要清除一下小程序商品首页的缓存;
- $objPageCache = new PageCache();
- $objPageCache->delPage();
- return ResultWrapper::success($dbResult);
- }
- /**
- * specType => 2
- * 编辑多规格基础商品资料
- * @param $params
- * @return ResultWrapper
- * @throws \Exception
- */
- public function editMultipleGoodsBasic($params)
- {
- if (empty($params['id'])) {
- return ResultWrapper::fail('没有指定要修改的商品数据id', ErrorCode::$paramError);
- }
- $id = $params['id'];//商品id
- $delUnitIds = $params['delUnitIds'];//辅单位
- $delSpecSkuIds = $params['delSpecSkuIds'];//属性
- $specMultiple = $params['specMultiple'];
- $unitData = $params['unitData'];
- unset($params['unitData']);
- unset($params['specMultiple']);
- unset($params['delUnitIds']);
- unset($params['delSpecSkuIds']);
- unset($params['id']);
- //编辑sku
- $objMSku = new MSku($this->onlineUserId, $this->onlineEnterpriseId);
- $dbResult = $objMSku->editMultipleSku($specMultiple, $id, $delUnitIds, $delSpecSkuIds, $unitData);
- if (!$dbResult->isSuccess()) {
- $this->objDGoodsBasic->rollBack();
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- //编辑基础资料
- $dbResult = $this->objDGoodsBasic->update($params, ['id' => $id]);
- if ($dbResult === false) {
- $this->objDGoodsBasic->rollBack();
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- //缓存商品名称
- $this->isUpdateBasicCache = true;
- $this->objectId = $id;
- $this->objDGoodsBasic->commit();
- //self::updateEsData($id);
- $this->objMSku->delSkuCache($id);
-
- // 每编辑一件商品需要清除一下小程序商品首页的缓存;
- $objPageCache = new PageCache();
- $objPageCache->delPage();
- return ResultWrapper::success($dbResult);
- }
- /**
- * 更新基础商品的启用/禁用
- * @param $params
- * @return ResultWrapper
- * @throws \Exception
- */
- public function updateEnableStatus($params)
- {
- if (!empty($params['deleteStatus'])){
- $is_status['deleteStatus'] = $params['deleteStatus'];
- }else{
- $is_status['enableStatus'] = $params['enableStatus'];
- }
- $dbResult = $this->objDGoodsBasic->update($is_status, ['id' => $params['id']]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- /*if (is_array($params['id'])) {
- foreach ($params['id'] as $id) {
- self::updateEsData($id);
- }
- }*/
- return ResultWrapper::success($dbResult);
- }
- /**
- * 删除指定商品基础资料
- * @param $id
- * @return ResultWrapper
- * @throws \Exception
- */
- public function delGoodsBasic($id)
- {
- $detail = $this->objDGoodsBasic->get($id, 'categoryPath,brandId');
- if (empty($detail)) {
- return ResultWrapper::fail('没有找到指定数据', ErrorCode::$dberror);
- }
- $dbResult = $this->objDGoodsBasic->update(['deleteStatus' => StatusCode::$delete], $id);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- $categoryIds = explode(',', $detail['categoryPath']);
- foreach ($categoryIds as $v) {
- $this->objGoodsBasicCache->categoryKeyScoreDecr($v);
- }
- $this->objGoodsBasicCache->brandKeyScoreDecr($detail['brandId']);
- /*
- $_id = self::createEsDocumentId($id);
- $this->objDGoodsBasic->esupdateTypeFieldVaule(['deleteStatus' => StatusCode::$delete], $_id); */
- return ResultWrapper::success($dbResult);
- }
- /**
- * Doc: (des="发布为平台商品")
- * User: XMing
- * Date: 2020/12/7
- * Time: 12:02 下午
- * @param int $id
- * @return ResultWrapper
- */
- public function pushPlatform(int $id): ResultWrapper
- {
- $dbResult = $this->objDGoodsBasic->update(['level' => StatusCode::$standard,'updateTime' => time()], $id);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- /*
- $_id = self::createEsDocumentId($id);
- $this->objDGoodsBasic->esupdateTypeFieldVaule(['level' => StatusCode::$standard,'updateTime' => time()], $_id);*/
- return ResultWrapper::success(true);
- }
- /**
- * 格式化Json数据
- * @param $data
- * @return mixed
- * @throws \Exception
- */
- private function formatJson($data)
- {
- if (isset($data['id'])) {
- $data['images'] = empty($data['images']) ? '' : json_decode($data['images'], true);
- $data['code'] = createCode(StatusCode::$code['goodsBasic']['prefix'], $data['id'], StatusCode::$code['goodsBasic']['length']);
- $data['description'] = empty($data['description']) ? '' : htmlspecialchars_decode($data['description'], ENT_QUOTES);
- $data['specGroup'] = empty($data['specGroup']) ? [] : json_decode($data['specGroup'], true);
- $extends = empty($data['extends']) ? [] : json_decode($data['extends'], true);
- $data['storage'] = isset($extends['storage']) ? $extends['storage'] : '';
- return $data;
- }
- foreach ($data as &$v) {
- $v['code'] = createCode(StatusCode::$code['goodsBasic']['prefix'], $v['id'], StatusCode::$code['goodsBasic']['length']);
- $v['images'] = empty($v['images']) ? '' : json_decode($v['images'], true);
- $v['description'] = empty($v['description']) ? '' : htmlspecialchars_decode($v['description'], ENT_QUOTES);
- $v['specGroup'] = empty($v['specGroup']) ? [] : json_decode($v['specGroup'], true);
- $extends = empty($v['extends']) ? [] : json_decode($v['extends'], true);
- $v['storage'] = isset($extends['storage']) ? $extends['storage'] : '';
- }
- return $data;
- }
- /**
- * 获取品牌下的商品数量
- * @param $where
- * @param $enterpriseId
- * @return ResultWrapper
- * @throws \Exception
- */
- public function getGoodsBasicTotalByBrandId($where)
- {
- $tableName = self::getGoodsBasicSubTableName();
- $sql = "SELECT brandId,count(id) num FROM " . $tableName . " where brandId IN (" . implode(',', $where['brandId']) . ') and deleteStatus=' . $where['deleteStatus'] . ' group by brandId';
- $dbResult = $this->objDGoodsBasic->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 获取分类下的商品数量
- * @param $where
- * @param $enterpriseId
- * @return ResultWrapper
- * @throws \Exception
- */
- public function getGoodsBasicTotalByCategoryId($where)
- {
- $tableName = self::getGoodsBasicSubTableName();
- $sql = "SELECT categoryId,count(id) num FROM " . $tableName . " where categoryId IN (" . implode(',', $where['categoryId']) . ') and deleteStatus=' . $where['deleteStatus'] . ' group by brandId';
- $dbResult = $this->objDGoodsBasic->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * Doc: (des="格式化物料列表")
- * User: XMing
- * Date: 2021/3/4
- * Time: 11:39 上午
- * @param array $data
- * @return ResultWrapper
- * @throws \Exception
- */
- public function formatMaterielLists(array $data): ResultWrapper
- {
- if (empty($data)){
- return ResultWrapper::success($data);
- }
- $materielIds = [];
- $categoryIds = [];
- $brandIds = [];
- $supplierIds = [];
- $shopIds = [];
- foreach ($data as $key => $value) {
- if (!in_array($value['id'],$materielIds)){
- $materielIds[] = $value['id'];
- }
- if (!in_array($value['categoryId'],$categoryIds) && !empty($value['categoryId'])){
- $categoryIds[] = $value['categoryId'];
- }
- if (!in_array($value['assistCategoryId'],$categoryIds) && !empty($value['assistCategoryId'])){
- $categoryIds[] = $value['assistCategoryId'];
- }
- if (!in_array($value['brandId'],$brandIds) && !empty($value['brandId'])){
- $brandIds[] = $value['brandId'];
- }
- if (!empty($value['noSalesShop'])){
- $noSalesShopArray = explode(',', $value['noSalesShop']);
- $shopIds = array_merge($shopIds,$noSalesShopArray);
- }
- if (!empty($value['storeId']) && !in_array($value['storeId'],$shopIds)){
- $shopIds[] = $value['storeId'];
- }
- if (!empty($value['supplierId']) && !in_array($value['supplierId'],$supplierIds)){
- $supplierIds[] = $value['supplierId'];
- }
- }
- $categoryMap = [];
- $objMGoodsCategory = new MGoodsCategory($this->onlineUserId, $this->onlineEnterpriseId);
- if (!empty($categoryIds)){
- $categoryResult = $objMGoodsCategory->getCategoryTitleByIds($categoryIds);
- if (!$categoryResult->isSuccess()){
- return ResultWrapper::fail($categoryResult->getData(),$categoryResult->getErrorCode());
- }
- $categoryData = $categoryResult->getData();
- $categoryMap = self::arrayToArrayMapping($categoryData);
- if ($categoryMap === false){
- return ResultWrapper::fail('ArrayToMap映射错误',ErrorCode::$paramError);
- }
- }
- $brandMap = [];
- if (!empty($brandIds)){
- $objMGoodsBrand = new MGoodsBrand($this->onlineUserId, $this->onlineEnterpriseId);
- $brandResult = $objMGoodsBrand->getBrandTitleByBrandId($brandIds);
- if (!$brandResult->isSuccess()){
- return ResultWrapper::fail($brandResult->getData(),$brandResult->getErrorCode());
- }
- $brandData = $brandResult->getData();
- $brandMap = self::arrayToArrayMapping($brandData);
- if ($brandMap === false){
- return ResultWrapper::fail('ArrayToMap映射错误',ErrorCode::$paramError);
- }
- }
- //商铺
- $shopMap = [];
- if (!empty($shopIds)){
- $objMShop = new MShop($this->onlineEnterpriseId, $this->onlineUserId);
- $shopResult = $objMShop->getShopByIds($shopIds);
- if (!$shopResult->isSuccess()){
- return ResultWrapper::fail($shopResult->getData(),$shopResult->getErrorCode());
- }
- $shopData = $shopResult->getData();
- $shopMap = self::arrayToArrayMapping($shopData);
- if ($shopMap === false){
- return ResultWrapper::fail('ArrayToMap映射错误',ErrorCode::$paramError);
- }
- }
- $supplierMap = [];
- if (!empty($supplierIds)){
- $objMSupplier = new MSupplier($this->onlineUserId,$this->onlineEnterpriseId);
- $supplierResult = $objMSupplier->getSupplierNameByIds($supplierIds);
- if (!$supplierResult->isSuccess()){
- return ResultWrapper::fail($supplierResult->getData(),$supplierResult->getErrorCode());
- }
- $supplierData = $supplierResult->getData();
- $supplierMap = self::arrayToArrayMapping($supplierData);
- if ($supplierMap === false){
- return ResultWrapper::fail('ArrayToMap映射错误',ErrorCode::$paramError);
- }
- }
- $specTotalMapResult = self::getSpecTotal($data);
- if (!$specTotalMapResult->isSuccess()){
- return ResultWrapper::fail($specTotalMapResult->getData(),$specTotalMapResult->getErrorCode());
- }
- $specTotalMap = $specTotalMapResult->getData();
- $masterUnitNameResult = $this->objMSku->getMasterUnitNameByMaterielIds($materielIds);
- if (!$masterUnitNameResult->isSuccess()){
- return ResultWrapper::fail($masterUnitNameResult->getData(),$masterUnitNameResult->getErrorCode());
- }
- $masterUnitNameMap = $masterUnitNameResult->getData();
- foreach ($data as &$item){
- $item['assistCategoryPath'] = json_decode($value['assistCategoryPath'], true);
- $item['assistCategoryTitle'] = isset($categoryMap[$item['assistCategoryId']]) ? $categoryMap[$item['assistCategoryId']]['title'] : '';
- $item['categoryTitle'] = isset($categoryMap[$item['categoryId']]) ? $categoryMap[$item['categoryId']]['title'] : '';
- $item['brandTitle'] = isset($brandMap[$item['brandId']]['title']) ? $brandMap[$item['brandId']]['title'] : '';
- $item['specTotal'] = isset($specTotalMap[$item['id']]) ? $specTotalMap[$item['id']] : 0;
- $noSalesShopArray = explode(',', $item['noSalesShop']);
- $noSalesShopNames = [];
- foreach ($noSalesShopArray as $shopId) {
- if ($shopId) {
- $noSalesShopNames[] = isset($shopMap[$shopId]) ? $shopMap[$shopId]['name'] : '';
- }
- }
- $item['noSalesShopNames'] = trim(implode(',', $noSalesShopNames),',');
- $item['sourceMsg'] = '平台';
- if (isset($item['storeId']) && !empty($item['storeId'])){
- $item['sourceMsg'] = isset($shopMap[$item['storeId']]) ? $shopMap[$item['storeId']]['name'] : '';
- }
- if (isset($item['supplierId']) && !empty($item['supplierId'])){
- $row = getArrayItem($supplierMap,$item['supplierId'],[]);
- if (!empty($row)){
- $item['sourceMsg'] = getArrayItem($row,'title','');
- }
- }
- $item['code'] = createCode(StatusCode::$code['goodsBasic']['prefix'], $item['id'], StatusCode::$code['goodsBasic']['length']);//当前状态
- $extends = empty($item['extends']) ? [] : json_decode($item['extends'],true);
- $item['storage']= isset($extends['storage']) ? $extends['storage'] : '';
- $item['unitNameMaster'] = getArrayItem($masterUnitNameMap,$item['id'],'');
- }
- unset($item);
- return ResultWrapper::success($data);
- }
- /**
- * 查询基础商品对应规格数量
- * @param $data
- * @return array
- */
- public function getSpecTotal(array $data): ResultWrapper
- {
- $goodsIds = array_values(array_unique(array_column($data, 'id')));
- $goodsIds = trim(implode(',', $goodsIds), ',');
- if (empty($goodsIds)){
- return ResultWrapper::success([]);
- }
- $sql = 'SELECT COUNT(id) as total,goodsId FROM qianniao_sku_' . $this->onlineEnterpriseId . ' WHERE deleteStatus = ' . StatusCode::$standard . ' AND goodsId IN(' . $goodsIds . ') GROUP BY goodsId;';
- $dbResult = $this->objMSku->getQuery($sql);
- if (!$dbResult->isSuccess()) {
- Logger::logs(E_USER_ERROR,'sql error',__CLASS__,__LINE__,$dbResult->getData());
- }
- $allSpec = $dbResult->getData();
- $mapping = [];
- foreach ($allSpec as $spec) {
- $mapping[$spec['goodsId']] = $spec['total'];
- }
- return ResultWrapper::success($mapping);
- }
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2021/3/4
- * Time: 12:03 下午
- * @param array $array
- * @param string $index
- * @return array|false
- */
- public static function arrayToArrayMapping(array $array,string $index = 'id')
- {
- if (empty($array)){
- return [];
- }
- $map = [];
- foreach ($array as $key => $value){
- if (!isset($value['id'])){
- return false;
- }
- $map[$value[$index]] = $value;
- }
- return $map;
- }
- /**
- * 格式化数据
- * @param $data
- * @param $enterpriseId
- * @return mixed
- * @throws \Exception
- */
- public function formatCategory($data)
- {
- if (empty($data)) {
- return $data;
- }
- if (isset($data['categoryId'])) {
- $dimension = $data;
- unset($data);
- $data[] = $dimension;
- }
- $allBrandIds = [];
- $allCategoryIds = [];
- //$noSalesShopIds = [];
- $allGoodsBasicIds = [];
- $shopIds = [];
- $supplierIds = [];
- $merchantIds = [];
- foreach ($data as $key => $value) {
- $allGoodsBasicIds[] = $value['id'];
- $allCategoryIds[] = $value['categoryId'];
- $allCategoryIds[] = $value['assistCategoryId'];
- $allBrandIds[] = $value['brandId'];
- !empty($value['merchantId']) && $merchantIds[] = $value['merchantId'];
- if ($value['noSalesShop']) {
- $shopIds = array_merge($shopIds, explode(',', $value['noSalesShop']));
- }
- !empty($value['storeId']) && $shopIds[] = $value['storeId'];
- !empty($value['supplierId']) && $supplierIds[] = $value['supplierId'];
- }
- $allCategoryData = [];
- $allBrandData = [];
- //分类
- $objMGoodsCategory = new MGoodsCategory($this->onlineUserId, $this->onlineEnterpriseId);
- $categoryResult = $objMGoodsCategory->getCategoryTitleByIds(array_values(array_filter($allCategoryIds)));
- if ($categoryResult->isSuccess()) {
- $categoryData = $categoryResult->getData();
- foreach ($categoryData as $key => $value) {
- $allCategoryData[$value['id']] = $value;
- }
- }
- //品牌
- $objMGoodsBrand = new MGoodsBrand($this->onlineUserId, $this->onlineEnterpriseId);
- $brandResult = $objMGoodsBrand->getBrandTitleByBrandId(array_values(array_filter($allBrandIds)));
- if ($brandResult->isSuccess()) {
- $brandData = $brandResult->getData();
- //创建映射关系
- foreach ($brandData as $key => $value) {
- $allBrandData[$value['id']] = $value;
- }
- }
- //商铺
- $objMShop = new MShop($this->onlineEnterpriseId, $this->onlineUserId);
- $shops = $objMShop->getShopName(array_values(array_unique($shopIds)));
- //供应商
- $objMSupplier = new MSupplier($this->onlineUserId,$this->onlineEnterpriseId);
- $supplierResult = $objMSupplier->getSupplierNameByIds($supplierIds);
- $supplierMap = [];
- if ($supplierResult->isSuccess()){
- $supplierMap = MSupplier::supplierMap($supplierResult->getData());
- }
- //商户
- if(!empty($merchantIds)){
- $objMMerchant = new MMerchant($this->onlineEnterpriseId, $this->onlineUserId);
- $modelResult = $objMMerchant->selectMerchantData(['id' => $merchantIds, 'deleteStatus' => StatusCode::$standard]);
- if(!$modelResult->isSuccess()){
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- $merchantResult = $modelResult->getData();
- unset($modelResult);
- $merchantData = [];
- foreach($merchantResult as $value){
- $merchantData[$value['id']] = $value;
- }
- }
- foreach ($data as $key => &$value) {
- $data[$key]['categoryTitle'] = isset($allCategoryData[$value['categoryId']]['title']) ? $allCategoryData[$value['categoryId']]['title'] : '';
- $data[$key]['assistCategoryTitle'] = isset($allCategoryData[$value['assistCategoryId']]['title']) ? $allCategoryData[$value['assistCategoryId']]['title'] : '';
- $data[$key]['assistCategoryPath'] = json_decode($value['assistCategoryPath'], true);
- $data[$key]['brandTitle'] = isset($allBrandData[$value['brandId']]['title']) ? $allBrandData[$value['brandId']]['title'] : '';
- $noSalesShop = explode(',', $value['noSalesShop']);
- $noSalesShopNames = [];
- foreach ($noSalesShop as $v) {
- if ($v) {
- $noSalesShopNames[] = isset($shops[$v]) ? $shops[$v]['name'] : '';
- }
- }
- $data[$key]['noSalesShopNames'] = implode(',', $noSalesShopNames);
- //$data[$key]['masterUnit'] = isset($skuData[$value['id']]['masterUnit']) ? $skuData[$value['id']]['masterUnit'] : '';
- //$data[$key]['branchUnit'] = isset($skuData[$value['id']]['branchUnit']) ? $skuData[$value['id']]['branchUnit'] : '';
- $data[$key]['sourceMsg'] = '平台';
- if (isset($value['storeId']) && !empty($value['storeId'])){
- $data[$key]['sourceMsg'] = getArrayItem($shops,$value['storeId'],'');
- }
- if (isset($value['supplierId']) && !empty($value['supplierId'])){
- $row = getArrayItem($supplierMap,$value['supplierId'],[]);
- if (!empty($row)){
- $data[$key]['sourceMsg'] = getArrayItem($row,'title','');
- }
- }
- $data[$key]['merchantName'] = '';
- if(isset($value['merchantId']) && !empty($value['merchantId']) && isset($merchantData[$value['merchantId']])){
- $data[$key]['sourceMsg'] = $merchantData[$value['merchantId']]['name'];
- $data[$key]['merchantName'] = $merchantData[$value['merchantId']]['name'];
- }
- }
- return $data;
- }
- /**
- * 获取商品名称
- * @param $goodsBasicIds
- * @param string $fields
- * @return array
- */
- public function getGoodsBasicNames($goodsBasicIds, $fields = "*"): ResultWrapper
- {
- if (!$goodsBasicIds) {
- return ResultWrapper::success([]);
- }
- $dbResult = $this->objDGoodsBasic->select($goodsBasicIds, $fields);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDGoodsBasic->error(),ErrorCode::$dberror);
- }
- foreach ($dbResult as $goodsBasic) {
- $return[$goodsBasic['id']] = $goodsBasic;
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 统计分类下的商品数量
- * @param $id
- * @return ResultWrapper
- */
- public function getBasicTotalByIds($id)
- {
- $tableName = $this->objDGoodsBasic->get_Table();
- $status = StatusCode::$standard;
- $sql = "select categoryId,count(id) num from {$tableName} where deleteStatus={$status} AND FIND_IN_SET({$id},categoryPath)";
- $dbResult = $this->objDGoodsBasic->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- $dbResult = empty($dbResult) ? '' : $dbResult[0];
- return ResultWrapper::success($dbResult);
- }
- /**
- * 搜索条件 ES
- * @param $selectParams
- * @param boolean $is_export
- * @return array
- */
- public function setSearchWhere($selectParams = [],bool $is_export = false)
- {
- $defaultDSL = [];
- if ($is_export === false) {
- if (isset($selectParams['offset']) && $selectParams['offset']) {
- $defaultDSL['from'] = $selectParams['offset'];
- }
- $defaultDSL['size'] = 2000;
- if (isset($selectParams['limit']) && $selectParams['limit']) {
- $defaultDSL['size'] = $selectParams['limit'];
- }
- }
- $defaultDSL['sort'] = [
- 'createTime' => [
- 'order' => 'desc'
- ],
- ];
- $dsl = [];
- $dsl['query']['bool']['must'][] = [
- 'bool' => [
- 'must' => [
- 'term' => ['enterpriseId' => $this->onlineEnterpriseId],
- ],
- ]
- ];
- //商品名称/编码/条码
- if (isset($selectParams['keyword'])) {
- if (!empty($selectParams['keyword'])) {
- $dsl['query']['bool']['must'][] = [
- 'bool'=>[
- 'must' => [
- 'multi_match' => [
- 'fields' => ['title', 'code', 'barCode', 'goodsCode'],
- 'query' => $selectParams['keyword'],
- //'fuzziness' => 'AUTO',
- "type" => "best_fields",
- "tie_breaker" => 0.3,
- "minimum_should_match" => "100%"
- ]
- ]
- ],
- ];
- }
- }
- //销售状态 goods_basic表
- if (isset($selectParams['enableStatus'])) {
- if (!empty($selectParams['enableStatus'])) {
- $dsl['query']['bool']['filter'][] =
- ['term' => ['enableStatus' => $selectParams['enableStatus']]];
- }
- }
- if (isset($selectParams['supplierId']) && !empty($selectParams['supplierId'])){
- $dsl['query']['bool']['filter'][] =
- ['term' => ['supplierId' => $selectParams['supplierId']]];
- }
- if (isset($selectParams['level']) && !empty($selectParams['level'])){
- $dsl['query']['bool']['filter'][] =
- ['term' => ['level' => $selectParams['level']]];
- }
- //删除状态
- if (isset($selectParams['deleteStatus'])) {
- $dsl['query']['bool']['filter'][] =
- ['term' => ['deleteStatus' => $selectParams['deleteStatus']]];
- }
- //商品分类
- if (isset($selectParams['categoryId'])) {
- if (!empty($selectParams['categoryId'])) {
- $dsl['query']['bool']['filter'][] =
- ['term' => ['categoryId' => $selectParams['categoryId']]];
- }
- }
- if (isset($selectParams['categoryPath'])) {
- if (!empty($selectParams['categoryPath'])) {
- $dsl['query']['bool']['must'][] = [
- 'bool' => [
- 'should' => [
- [
- 'wildcard' => ['categoryPath' => $selectParams['categoryPath'].',*'],
- ],
- [
- 'wildcard' => ['categoryPath' => $selectParams['categoryPath']],
- ],
- ],
- ]
- ];
- }
- }
- //商品品牌
- if (isset($selectParams['brandId'])) {
- if (!empty($selectParams['brandId'])) {
- $dsl['query']['bool']['filter'][] =
- ['term' => ['brandId' => $selectParams['brandId']]];
- }
- }
- /*多选
- * if (!empty($selectParams['brandIds'])) {
- $dsl['query']['bool']['must'][] = [
- 'terms' => [
- 'brandId' => $selectParams['brandIds']
- ]
- ];
- }*/
- //销售员id
- parent::getAccessSalesManIds();
- if (!empty(parent::$salesManIds) && empty($selectParams['salesManId'])) {
- $dsl['query']['bool']['filter'][] = [
- 'terms' => ['salesManId' => parent::$salesManIds]
- ];
- }
- //基本资料ids
- if (isset($selectParams['ids'])) {
- if (!empty($selectParams['ids'])) {
- $dsl['query']['bool']['must'][] = [
- 'terms' => [
- 'id' => array_values($selectParams['ids'])
- ]
- ];
- }
- }
- //导出
- $dsl = array_merge($defaultDSL,$dsl);
- return $dsl;
- }
- /**
- * 搜索符合条件的GoodsBasic
- * @param array $selectParams
- * @return ResultWrapper
- */
- public function searchGoodsBasics($selectParams = [])
- {
- $dsl = $this->setSearchWhere($selectParams);
- $result = $this->objDGoodsBasic->getSearchQueryDsl($dsl);
- if (isset($result['status']) && $result['status'] == 400) {
- file_put_contents('/www/wwwroot/logs/api.junhailan.com/elasticsearch.log', date('Y-m-d H:i:s') . '获取基本资料失败,错误原因' . var_export($result, true) . PHP_EOL, FILE_APPEND);
- if ($result['error']['reason'] == 'all shards failed') {
- return ResultWrapper::success([]);
- }
- return ResultWrapper::fail('基本资料id获取失败' . $result['error']['reason'], ErrorCode::$apiNotResult);
- }
- if (!isset($result['hits']) || $result['hits']['total'] == 0) {
- return ResultWrapper::success([]);
- }
- $total = $result['hits']['total'];
- $dbResult = $result['hits']['hits'];
- $goodsBasicIds = [];
- foreach ($dbResult as $key => &$value) {
- $goodsBasicIds[] = $value['_source'];
- }
- return ResultWrapper::success($goodsBasicIds);
- }
- public function buildSqlBySelectParams(array $selectParams): array
- {
- $countField = ' COUNT(id) as total ';
- $fields = ' * ';
- $tableName = $this->objDGoodsBasic->get_Table();
- $where = '';
- if (isset($selectParams['enableStatus']) && !empty($selectParams['enableStatus'])){
- $where .= ' AND `enableStatus` = '.$selectParams['enableStatus'];
- }
- if (isset($selectParams['brandId']) && !empty($selectParams['brandId'])){
- $where .= ' AND `brandId` = '.$selectParams['brandId'];
- }
- if (isset($selectParams['supplierId']) && !empty($selectParams['supplierId'])){
- $where .= ' AND `supplierId` = '.$selectParams['supplierId'];
- }
- if (isset($selectParams['level']) && !empty($selectParams['level'])){
- $where .= ' AND `level` = '.$selectParams['level'];
- }
- if (isset($selectParams['categoryId']) && !empty($selectParams['categoryId'])){
- $where .= ' AND `categoryId` = '.$selectParams['categoryId'].' or find_in_set('.$selectParams['categoryId'].',assistCategoryId) ';
- }
- if (isset($selectParams['merchantId']) && !empty($selectParams['merchantId'])){
- $where .= ' AND `merchantId` = '.$selectParams['merchantId'];
- }
- //商品关键字
- if (isset($selectParams['keyword']) && !empty($selectParams['keyword'])){
- $where .= ' and title like "%'.$selectParams['keyword'].'%"';
- }
- //分类路径
- if (isset($selectParams['categoryPath']) && !empty($selectParams['categoryPath'])){
- if (is_string($selectParams['categoryPath']) || is_int($selectParams['categoryPath'])){
- $selectParams['categoryPath'] = explode(',',$selectParams['categoryPath']);
- }
- $endPathId = array_pop($selectParams['categoryPath']);
- if (!empty($endPathId)){
- $where .= ' and find_in_set('.$endPathId.',categoryPath) or find_in_set('.$endPathId.',assistCategoryId) ';
- }
- }
- //排序
- if (isset($selectParams['orderBy']) && !empty($selectParams['orderBy'])){
- $where .= ' order by '.$selectParams['orderBy'];
- }else{
- $where .= ' order by createTime DESC ';
- }
- $countSql = 'SELECT '.$countField.' FROM '.$tableName.' WHERE `deleteStatus` ='.StatusCode::$standard.$where;
- //分页
- if (isset($selectParams['offset']) && isset($selectParams['limit']) && !empty($selectParams['limit'])){
- $where .= ' limit '.$selectParams['offset'].','.$selectParams['limit'];
- }
- $sql = 'SELECT '.$fields.' FROM '.$tableName.' WHERE `deleteStatus` ='.StatusCode::$standard.$where;
- return [
- 'sql' => $sql,
- 'countSql' => $countSql
- ];
- }
- /**
- * 搜索
- * @param array $selectParams 过滤条件
- * @param boolean $is_export
- * @return ResultWrapper
- * @throws \Exception
- */
- public function search($selectParams,bool $is_export = false)
- {
- $buildSql =self::buildSqlBySelectParams($selectParams);
- $lists = $this->objDGoodsBasic->query($buildSql['sql']);
- if ($lists === false){
- return ResultWrapper::fail($this->objDGoodsBasic->error(),ErrorCode::$dberror);
- }
- $count = $this->objDGoodsBasic->query($buildSql['countSql']);
- if ($count === false){
- return ResultWrapper::fail($this->objDGoodsBasic->error(),ErrorCode::$dberror);
- }
- if (empty($lists)){
- return ResultWrapper::success([
- 'data' => [],
- 'total' => 0
- ]);
- }
- if ($is_export === true){
- self::exportGoodsBasic($lists,'db');
- die;
- }
- $format = self::formatJson($lists);
- $format = self::formatCategory($format);
- foreach ($format as &$formatData) {
- $formatData['unitData'] = [];
- $formatData['specGroup'] = [];
- $formatData['specMultiple'] = [];
- $formatData = self::formatSingleSkuMapping($formatData);
- $formatData = self::formatMultipleSkuMapping($formatData);
- }
- $return = [
- 'data' => $format,
- 'total' => isset($count[0]['total']) ? $count[0]['total'] : 0,
- ];
- return ResultWrapper::success($return);
- /*$dsl = $this->setSearchWhere($selectParams);
- $result = $this->objDGoodsBasic->getSearchQueryDsl($dsl);
- if (isset($result['status']) && $result['status'] == 400) {
- return ResultWrapper::fail('获取数据失败' . $result['error']['reason'], ErrorCode::$apiNotResult);
- }
- if (!isset($result['hits']) || $result['hits']['total'] == 0) {
- return ResultWrapper::success([
- 'data' => [],
- 'total' => 0
- ]);
- }
- $total = $result['hits']['total'];
- $dbResult = $result['hits']['hits'];
- $list = [];
- foreach ($dbResult as $key => &$value) {
- $data = [];
- $data = $value['_source'];
- //$data['id'] = $value['_id'];
- $list[] = $data;
- }
- if ($is_export === true) self::exportGoodsBasic($list,'es');
- foreach ($list as &$v) {
- $v['masterUnit'] = isset($v['skuData']['masterUnit']) ? $v['skuData']['masterUnit'] : [];
- $v['branchUnit'] = isset($v['skuData']['branchUnit']) ? $v['skuData']['branchUnit'] : [];
- $v['code'] = createCode(StatusCode::$code['goodsBasic']['prefix'], $v['id'], StatusCode::$code['goodsBasic']['length']);
- $v['description'] = empty($v['description']) ? '' : htmlspecialchars_decode($v['description'], ENT_QUOTES);
- }
- $return = [
- 'data' => self::formatCategory($list),
- 'total' => ($total) ? intval($total) : 0,
- ];
- return ResultWrapper::success($return);*/
- }
- /**
- * 根据商品ids数组获取商品信息
- * @param $whereIds
- * @return array|ResultWrapper
- */
- public function getGoodsDataByGoodsIds($whereIds)
- {
- $GoodsBasic = $this->objDGoodsBasic->select($whereIds, 'title,id,images,expireTime,specType');
- if ($GoodsBasic === false) {
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- if (empty($GoodsBasic)) {
- return ResultWrapper::fail('商品基础资料为空', ErrorCode::$paramError);
- }
- $returnData = array_column($GoodsBasic, null, 'id');
- return ResultWrapper::success($returnData);
- }
- /**
- * 根据ids数组获取分类信息
- * @param $ids
- * @return ResultWrapper
- * @throws \Exception
- */
- public function getCategoryByIds($ids)
- {
- if (empty($ids)) return ResultWrapper::success($ids);
- $objDGoodsCategory = new DGoodsCategory('default');
- $objDGoodsCategory->setTable($objDGoodsCategory->get_Table() . '_' . $this->onlineEnterpriseId);
- $sql = "select b.id,c.id categoryId,c.title from " . $this->objDGoodsBasic->get_Table() . " b LEFT JOIN " . $objDGoodsCategory->get_Table() . " c on b.categoryId = c.id where b.id in(" . implode(',', $ids) . ")";
- $dbResult = $this->objDGoodsBasic->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- $categoryNames = [];
- foreach ($dbResult as $value) {
- $categoryNames[$value['id']] = $value;
- }
- return ResultWrapper::success($categoryNames);
- }
- /**
- * 根据ids数组获取品牌
- * @param $ids
- * @return ResultWrapper
- * @throws \Exception
- */
- public function getBrandByIds($ids)
- {
- if (empty($ids)) return ResultWrapper::success($ids);
- $objDGoodsBrand = new DGoodsBrand('default');
- $objDGoodsBrand->setTable($objDGoodsBrand->get_Table() . '_' . $this->onlineEnterpriseId);
- $sql = "select b.id,c.id brandId,c.title from " . $this->objDGoodsBasic->get_Table() . " b LEFT JOIN " . $objDGoodsBrand->get_Table() . " c on b.brandId = c.id where b.id in(" . implode(',', $ids) . ")";
- $dbResult = $this->objDGoodsBasic->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- $brandNames = [];
- foreach ($dbResult as $value) {
- $brandNames[$value['id']] = $value;
- }
- return ResultWrapper::success($brandNames);
- }
- /**
- * 更新基础商品分类
- * @param $params
- * @return ResultWrapper
- */
- public function updateCategory($params)
- {
- $dbResult = $this->objDGoodsBasic->select($params['id'], 'categoryPath');
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- //todo(减原来分类下的商品数量)
- foreach ($dbResult as $value) {
- $allCategoryIds = explode(',', $value['categoryPath']);
- foreach ($allCategoryIds as $categoryId) {
- $this->objGoodsBasicCache->categoryKeyScoreDecr($categoryId);
- }
- }
- unset($allCategoryIds);
- //更新商品分类
- $dbResult = $this->objDGoodsBasic->update(['categoryId' => $params['categoryId'], 'categoryPath' => $params['categoryPath']], $params['id']);
- if ($dbResult == false) {
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- //todo(增加现在分类下的商品数量)
- $allCategoryIds = explode(',', $params['categoryPath']);
- foreach ($allCategoryIds as $categoryId) {
- $this->objGoodsBasicCache->categoryKeyScoreIncr($categoryId);
- }
- //修改ES中的数据
- /*
- foreach ($params['id'] as $id) {
- $_id = self::createEsDocumentId($id);
- $this->objDGoodsBasic->esupdateTypeFieldVaule(['categoryId' => $params['categoryId'], 'categoryPath' => $params['categoryPath']], $_id);
- }
- usleep(500000);//500ms */
- return ResultWrapper::success($dbResult);
- }
- /**
- * 批量设置不可销售店铺
- * @param $params
- * @return ResultWrapper
- */
- public function setNoSalesShop($params)
- {
- $dbResult = $this->objDGoodsBasic->update(['noSalesShop' => $params['noSalesShop']], $params['id']);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- //修改ES中的数据
- foreach ($params['id'] as $id) {
- $_id = self::createEsDocumentId($id);
- $this->objDGoodsBasic->esupdateTypeFieldVaule(['noSalesShop' => $params['noSalesShop']], $_id);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 批量设置品牌
- * @param $params
- * @return ResultWrapper
- */
- public function setBrand($params)
- {
- $dbResult = $this->objDGoodsBasic->update(['brandId' => $params['brandId']], $params['id']);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- //修改ES中的数据
- foreach ($params['id'] as $id) {
- $_id = self::createEsDocumentId($id);
- $this->objDGoodsBasic->esupdateTypeFieldVaule(['brandId' => $params['brandId']], $_id);
- }
- return ResultWrapper::success($dbResult);
- }
- public function todayGoodsRanking($ranking = 1, $shopId = null)
- {
- if ($ranking == 1) {
- $result = $this->objOverviewCache->getRanking($this->onlineEnterpriseId, 'goodsRanking', $shopId);
- } else {
- $result = $this->objOverviewCache->getSalesMoneyRanking($this->onlineEnterpriseId, 'goodsRanking', $shopId);
- }
- if (empty($result)) {
- return [];
- }
- $total = array_sum($result);
- //拼接goodsBasicId
- $goodsBasicIds = [];
- $i = 1;
- foreach ($result as $goodsBasicId => $v) {
- $goodsBasicIds[] = $goodsBasicId;
- $i++;
- if ($i >= 8) {
- break;
- }
- }
- $goodsBasicResult = self::getGoodsBasicNames($goodsBasicIds);
- if (!$goodsBasicResult->isSuccess()){
- return [];
- }
- $goodsBasicResult = $goodsBasicResult->getData();
- $goodsBasicNameData = [];
- foreach ($goodsBasicResult as $goodsBasicData) {
- $goodsBasicNameData[$goodsBasicData['id']] = $goodsBasicData['title'];
- }
- //拼接返回数据
- $return = [];
- foreach ($result as $goodsBasicId => $v) {
- $i = 1;
- $rank = [];
- $rank['id'] = $i;
- $rank['goodsName'] = isset($goodsBasicNameData[$goodsBasicId]) ? $goodsBasicNameData[$goodsBasicId] : '';
- $rank['numberOrMoney'] = $v;
- $rank['percent'] = $v ? (float)bcmul(bcdiv($v, $total), 100, 2) : 0;
- $return[] = $rank;
- $i++;
- if ($i >= 8) {
- break;
- }
- }
- return $return;
- }
- public function getBasicInfoByIds($params, $field = "*")
- {
- $dbResult = $this->objDGoodsBasic->select($params, $field);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 查
- * @param $id
- * @return array|bool
- */
- public function getBasicById($id)
- {
- $dbResult = $this->objDGoodsBasic->get(['id' => $id]);
- if($dbResult === false){
- return [];
- }
- if(isset($dbResult['images']) && !empty($dbResult['images'])){
- $dbResult['images'] = json_decode($dbResult['images'], true);
- }else{
- $dbResult['images'] = [];
- }
- return $dbResult;
- }
- /**
- * 基础商品资料按照sku列表显示
- * @param array $selectParams
- * @return ResultWrapper
- */
- public function getAllGoodsBasicBySku(array $selectParams)
- {
- $fields ='b.isEq,s.id as skuId,s.unitName,s.unitId,s.specType,s.conversion,s.specData as specGroup,s.isMaster as isMaster,b.title,b.images as images,b.id,c.title as categoryName,c.id as categoryId';
- $sql = 'SELECT '.$fields.' FROM qianniao_sku_'.$this->onlineEnterpriseId.' as s
- LEFT JOIN qianniao_goods_basic_'.$this->onlineEnterpriseId.' as b
- ON b.id = s.goodsId
- LEFT JOIN qianniao_goods_category_'.$this->onlineEnterpriseId.' as c
- ON c.id = b.categoryId
- WHERE s.deleteStatus = '.StatusCode::$standard.'
- AND b.deleteStatus= '.StatusCode::$standard.'';
- //是否显示全部单位主/辅
- if (isset($selectParams['isRevealSku']) && $selectParams['isRevealSku'] == StatusCode::$standard){
- $sql .= ' AND s.isMaster = '.$selectParams['isRevealSku'];
- }
- if (isset($selectParams['materielId']) && !empty($selectParams['materielId'])){
- $sql .= ' AND s.goodsId = '.$selectParams['materielId'];
- }
- //筛选分类
- if (isset($selectParams['categoryId']) && !empty($selectParams['categoryId'])){
- $sql .= ' and find_in_set('.$selectParams['categoryId'].',b.categoryPath) ';
- }
- if (isset($selectParams['keyword']) && !empty($selectParams['keyword'])){
- $keyword = '%'.$selectParams['keyword'].'%';
- $sql .= ' AND (b.title LIKE "'.$keyword.'" OR b.condition LIKE "'.$keyword.'")';
- }
- if (isset($selectParams['shopId']) && !empty($selectParams['shopId'])){
- // $sql .= ' AND b.storeId = '.$selectParams['shopId'];
- $sql .= ' AND b.storeId in ('.$selectParams['shopId'].',0)';
- }
-
- if( isset($selectParams['merchantId']) ){
- $sql .= ' AND b.merchantId = '.$selectParams['merchantId'];
- } else {
- if (isset($selectParams['ifMerchant']) ){
- if($selectParams['ifMerchant'] == StatusCode::$delete){
- $sql .= ' AND b.merchantId = 0';
- } else {
- $sql .= ' AND b.merchantId <> 0';
- }
- }
- }
-
- $total = count((array)$this->objDGoodsBasic->query($sql));
- $sql .= ' ORDER BY b.createTime DESC LIMIT '.$selectParams['offset'].','.$selectParams['limit'].'';
- $dbResult = $this->objDGoodsBasic->query($sql);
- if ($dbResult === false){
- return ResultWrapper::fail($this->objDGoodsBasic->error(),ErrorCode::$dberror);
- }
- $return = [
- 'data' => self::format($dbResult),
- 'total' => $total,
- ];
- return ResultWrapper::success($return);
- }
- /**
- * @param $data
- * @return mixed
- */
- public static function format($data)
- {
- if (empty($data)){
- return $data;
- }
- foreach ($data as &$value){
- $value['specGroup'] = empty($value['specGroup']) ? [] : json_decode($value['specGroup'],true);
- $value['images'] = empty($value['images']) ? [] : json_decode($value['images'],true);
- $value['conversion'] = empty($value['conversion']) ? 0 : $value['conversion'];
- $value['code'] = createCode(StatusCode::$code['goodsBasic']['prefix'], $value['id'], StatusCode::$code['goodsBasic']['length']);
- }
- return $data;
- }
- /**
- * @After
- */
- public function __destruct()
- {
- // TODO: Implement __destruct() method.
- self::cacheBasic();
- }
- /**
- * Doc: (des="根据基础商品资料id获取商品数据,及分类名称,品牌名称")
- * User: XMing
- * Date: 2020/7/9
- * Time: 7:01 下午
- * @param array $ids
- * @return ResultWrapper
- */
- public function getBasicGoodsFieldByIds(array $ids)
- {
- $mapping = [];
- $notFoundCacheIds = [];
- foreach ($ids as $id){
- $result = $this->objGoodsBasicRelevant->getNameByBasicId($id,true);
- if ($result===false || empty($result)){
- //缓存中没有
- $notFoundCacheIds[] = $id;
- continue;
- }
- $mapping[$id] = $result;
- }
- if (!empty($notFoundCacheIds)){
- //查询数据库
- $fields = "c.title as categoryName,b.*,d.title as brandName";
- $idStr = implode(',',$notFoundCacheIds);
- $sql = 'select '.$fields.' from qianniao_goods_basic_'.$this->onlineEnterpriseId.' as b
- left join qianniao_goods_category_'.$this->onlineEnterpriseId.' as c
- on b.categoryId = c.id
- left join qianniao_goods_brand_'.$this->onlineEnterpriseId.' as d
- on d.id = b.brandId
- where b.id in('.$idStr.')';
- $basicResult = $this->objDGoodsBasic->query($sql);
- if ($basicResult === false){
- Logger::logs(E_USER_ERROR,'查询出错',__CLASS__,__LINE__,$this->objDGoodsBasic->error());
- return ResultWrapper::fail($this->objDGoodsBasic->error(),ErrorCode::$dberror);
- }
- foreach ($basicResult as $item){
- $this->objGoodsBasicRelevant->cacheBasicIdRelationName($item,$item['id']);
- $item['images'] = json_decode($item['images'], true);
- $mapping[$item['id']] = $item;
- }
- }
- return ResultWrapper::success($mapping);
- }
- /**
- * Doc: (des="根据供应商获取商品数量")
- * User: XMing
- * Date: 2020/12/26
- * Time: 4:54 下午
- * @param $supplierId
- * @return ResultWrapper
- */
- public function getTotalBySupplierId($supplierId): ResultWrapper
- {
- $selectParams = [
- 'supplierId' => $supplierId,
- 'deleteStatus' => StatusCode::$standard,
- ];
- $total = $this->objDGoodsBasic->count($selectParams);
- if ($total === false){
- return ResultWrapper::fail($this->objDGoodsBasic->error(),ErrorCode::$dberror);
- }
- return ResultWrapper::success($total);
- }
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2020/12/19
- * Time: 9:41 上午
- * @param $ids
- * @return ResultWrapper
- */
- public function getMapsByIds($ids): ResultWrapper
- {
- if (empty($ids)){
- return ResultWrapper::success([]);
- }
- $lists = $this->objDGoodsBasic->select(['id' => $ids],'id,images,title');
- if ($lists === false){
- return ResultWrapper::fail($this->objDGoodsBasic->error(),ErrorCode::$dberror);
- }
- $map = [];
- foreach ($lists as $list){
- $list['code'] = createCode(StatusCode::$code['goodsBasic']['prefix'], $list['id'], StatusCode::$code['goodsBasic']['length']);
- $list['images'] = !empty($list['images']) ? json_decode($list['images'],true) : [];
- $map[$list['id']] = $list;
- }
- return ResultWrapper::success($map);
- }
- /**
- * 根据商品名称关键字查询商品ids
- */
- public function getBasicGoodsIdsByGoodsTitleKeyword($keyword)
- {
- $sql = 'select id from qianniao_goods_basic_'.$this->onlineEnterpriseId.' where title like "%'.$keyword.'%" and deleteStatus = '.StatusCode::$standard;
- $dbResult = $this->objDGoodsBasic->query($sql);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success(array_pop($dbResult));
- }
- }
|