123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931 |
- <?php
- /**
- * 规格
- * Created by PhpStorm.
- * User: XiaoMing
- * Date: 2019/11/18
- * Time: 18:28
- */
- namespace JinDouYun\Model\GoodsManage;
- use JinDouYun\Cache\SkuCache;
- use JinDouYun\Controller\Common\Logger;
- use JinDouYun\Model\Goods\BasicAndSkuCache;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Core\StatusCode;
- use JinDouYun\Cache\GoodsBasicRelevant;
- use JinDouYun\Dao\GoodsManage\DSku;
- /**
- * Class MSku
- * @package JinDouYun\Model\GoodsManage
- */
- class MSku
- {
- use BasicAndSkuCache;
- /**
- * @var DSku
- */
- private $objDSku;
- /**
- * @var
- */
- private $onlineUserId;
- /**
- * @var int
- */
- private $onlineEnterpriseId;
- /**
- * @var GoodsBasicRelevant
- */
- private $objGoodsBasicRelevantCache;
- /**
- * @var SkuCache
- */
- private $objSkuCache;
- /**
- * MSku constructor.
- * @param $onlineUserId
- * @param $onlineEnterpriseId
- * @throws \Exception
- */
- public function __construct($onlineUserId, $onlineEnterpriseId)
- {
- $this->onlineUserId = $onlineUserId;
- $this->onlineEnterpriseId = $onlineEnterpriseId;
- $this->objDSku = new DSku('default');
- $this->objDSku->setTable($this->objDSku->get_Table() . '_' . $onlineEnterpriseId);
- $this->objSkuCache = new SkuCache($this->onlineEnterpriseId);
- $this->objGoodsBasicRelevantCache = new GoodsBasicRelevant($this->onlineEnterpriseId);
- }
- /**
- * 增加规格
- * @param $params
- * @param bool $multiple
- * @return ResultWrapper
- */
- public function addSku($params, $multiple = false)
- {
- $last = $this->objDSku->select([], 'id', 'id DESC', 1);//获取最后一条数据的id
- $dbResult = $this->objDSku->insert($params, $multiple);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
- }
- //写缓存
- self::updateCacheSku($last);
- $this->isUpdateSku = true;
- $this->updateSku = (array) $dbResult;
- return ResultWrapper::success($dbResult);
- }
- /**
- * 带有属性的比那集
- * @param $specMultiple
- * @param $goodsId
- * @param $delUnitIds
- * @param $delSpecSkuIds
- * @param $unitData
- * @return ResultWrapper
- */
- public function editMultipleSku($specMultiple, $goodsId, $delUnitIds, $delSpecSkuIds, $unitData)
- {
- $dbResult = self::commonMerge($specMultiple, $unitData, ['goodsId' => $goodsId], $goodsId);
- if (!$dbResult->isSuccess()) {
- return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
- }
- $data = $dbResult->getData();
- if (empty($data)) {
- $insert = self::commonSpec($specMultiple, $unitData, $goodsId);//组装数据
- if (!empty($insert)) {
- $dbResult = $this->objDSku->insert($insert, true);
- }
- $this->updateSku = array_merge($this->updateSku,$dbResult);
- unset($insert);
- }
- if (!empty($data['insert'])) {
- $dbResult = $this->objDSku->insert($data['insert'], true);
- $this->updateSku = array_merge($this->updateSku,$dbResult);
- }
- if (!empty($data['update'])) {
- foreach ($data['update'] as &$spec) {
- $this->updateSku = array_merge($this->updateSku,[$spec['id']]);
- unset($spec['hash']);
- $dbResult = $this->objDSku->update($spec, ['id' => $spec['id']]);
- }
- }
- if (!empty($data['delete'])) {
- $dbResult = $this->objDSku->update(
- [
- 'deleteStatus' => StatusCode::$delete
- ],
- [
- 'id' => array_column($data['delete'], 'id')
- ]);
- }
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
- }
- $this->isUpdateSku = true;
- return ResultWrapper::success($dbResult);
- }
- /**
- * 组合spec
- * @param $specMultiple
- * @param $unitData
- * @param $goodsId
- * @return array
- */
- public function commonSpec($specMultiple, $unitData, $goodsId)
- {
- $isNew = StatusCode::$standard;
- $isNewResult = self::getIsNewByGoodsId($goodsId);
- if ($isNewResult->isSuccess()){
- $isNew = $isNewResult->getData();
- }
- //属性新增操作
- $insert = [];
- foreach ($unitData as $unit) {
- foreach ($specMultiple as $spec) {
- if (!isset($spec['specGroupHash'])) {
- $specGroupIds = array_column($spec['specGroup'], 'specValueId');
- $hash = md5(implode('|', $specGroupIds));
- //属性新增操作
- $insert[] = [
- 'goodsId' => $goodsId,
- 'specData' => json_encode($spec['specGroup']),
- 'specImage' => json_encode($spec['specImage']),
- 'specType' => StatusCode::$specType['multiple'],
- 'unitId' => $unit['unitId'],
- 'isMaster' => $unit['isMaster'],
- 'unitName' => $unit['unitName'],
- 'conversion' => $unit['conversion'],
- 'specGroupHash' => $hash,
- 'isNew' => $isNew
- ];
- }
- }
- }
- return $insert;
- }
- /**
- * @param $specMultiple
- * @param $unitData
- * @param $selectParams
- * @param $goodsId
- * @return ResultWrapper
- */
- public function commonMerge($specMultiple, $unitData, $selectParams, $goodsId)
- {
- // 判断sku是否新版的规则标识
- $isNewResult = self::getIsNewByGoodsId($goodsId);
- if (!$isNewResult->isSuccess()){
- return ResultWrapper::fail($isNewResult->getData(),$isNewResult->getErrorCode());
- }
- $isNew = $isNewResult->getData();
- // 查询当前商品的所有sku数据
- $dbResult = self::getSpecQuery($selectParams);
- if (!$dbResult->isSuccess()) {
- return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
- }
- $oldData = $dbResult->getData();
- if (empty($oldData)) {
- return ResultWrapper::success([]);
- }
- $mapping = [];
- $allHash = [];
- foreach ($oldData as &$value) {
- $hash = md5($value['goodsId'] . $value['unitId'] . $value['isMaster'] . $value['specGroupHash']);
- $value['hash'] = $hash;
- $mapping[$value['hash']] = $value;
- $allHash[] = $value['hash'];
- }
- //比对数据库就数据和新数据
- $nowData = [];
- foreach ($unitData as $unit) {
- foreach ($specMultiple as $spec) {
- //生成hash值
- $specGroupIds = array_column($spec['specGroup'], 'specValueId');
- $specGroupHash = md5(implode('|', $specGroupIds));//属性hash值
- $hash = md5($goodsId . $unit['unitId'] . $unit['isMaster'] . $specGroupHash);//对单行数据生成hash值
- $nowData[] = [
- 'goodsId' => $goodsId,
- 'specData' => json_encode($spec['specGroup']),
- 'specImage' => json_encode($spec['specImage']),
- 'specType' => StatusCode::$specType['multiple'],
- 'unitId' => $unit['unitId'],
- 'isMaster' => $unit['isMaster'],
- 'unitName' => $unit['unitName'],
- 'conversion' => $unit['conversion'],
- 'specGroupHash' => $specGroupHash,
- 'hash' => $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' => $isNew
- ];//新的数据
- }
- }
- $insert = [];
- $update = [];
- $delete = [];
- foreach ($nowData as &$spec) {
- if (in_array($spec['hash'], $allHash)) {
- $spec['id'] = $mapping[$spec['hash']]['id'];
- $update[] = $spec;
- unset($mapping[$spec['hash']]);
- } else {
- unset($spec['hash']);
- $insert[] = $spec;
- }
- }
- $delete = $mapping;
- return ResultWrapper::success([
- 'insert' => $insert,
- 'update' => $update,
- 'delete' => array_values($delete),
- ]);
- }
- public function getSpecQuery($selectParams)
- {
- $selectParams['deleteStatus'] = StatusCode::$standard;
- $dbResult = $this->objDSku->select($selectParams);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 修改sku
- * @param $unitData
- * @param $goodsId
- * @param $delUnitIds
- * @return ResultWrapper
- */
- public function editSku($unitData, $goodsId, $delUnitIds, $delSpecSkuIds)
- {
- $isNewResult = self::getIsNewByGoodsId($goodsId);
- if (!$isNewResult->isSuccess()){
- return ResultWrapper::fail($isNewResult->getData(),ErrorCode::$dberror);
- }
- $isNew = $isNewResult->getData();
- $objGoodsBasicRelevant = new GoodsBasicRelevant($this->onlineEnterpriseId);
- //有删除的
- if (!empty($delUnitIds)) {
- $dbResult = $this->objDSku->update(
- ['deleteStatus' => StatusCode::$delete, 'updateTime' => time()],
- [
- 'unitId' => $delUnitIds,
- 'goodsId' => $goodsId,
- ]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
- }
- }
- // 删除带属性的sku
- if(!empty($delSpecSkuIds)){
- $dbResult = $this->objDSku->update(['deleteStatus' => StatusCode::$delete, 'updateTime' => time()], ['specGroupHash'=>$delSpecSkuIds]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
- }
- }
- //有更新
- if (!empty($unitData)) {
- $insert = [];
- foreach ($unitData as $unit) {
- if (!isset($unit['id'])) {
- $insert[] = [
- 'conversion' => $unit['conversion'],
- 'unitName' => $unit['unitName'],
- 'isMaster' => $unit['isMaster'],
- 'unitId' => $unit['unitId'],
- 'specType' => StatusCode::$specType['single'],//无属性
- 'goodsId' => $goodsId,
- 'barCode' => isset($unit['barCode']) ? $unit['barCode'] : '',
- 'isDefault' => isset($unit['isDefault']) ? $unit['isDefault'] : StatusCode::$delete,
- 'weight' => isset($unit['weight']) ? $unit['weight'] : 0,
- 'isNew' => $isNew,
- ];
- } else {
- $dbResult = $this->objDSku->update([
- 'conversion' => $unit['conversion'],
- 'unitName' => $unit['unitName'],
- 'isMaster' => $unit['isMaster'],
- 'unitId' => $unit['unitId'],
- 'barCode' => isset($unit['barCode']) ? $unit['barCode'] : '',
- 'isDefault' => isset($unit['isDefault']) ? $unit['isDefault'] : StatusCode::$delete,
- 'weight' => isset($unit['weight']) ? $unit['weight'] : 0,
- 'isNew' => $isNew
- ], $unit['id']);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
- }
- $this->updateSku = array_merge($this->updateSku,[$unit['id']]);
- $objGoodsBasicRelevant->cacheSkuIdRelationName($unit['unitName'], $unit['id']);
- }
- }
- if (!empty($insert)) {
- $last = $this->objDSku->select([], 'id', 'id DESC', 1);//获取最后一条数据的id
- $dbResult = $this->objDSku->insert($insert, true);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
- }
- $this->updateSku = array_merge($this->updateSku,$dbResult);
- if (isset($last)) {
- self::updateCacheSku($last);//写缓存
- }
- }
- }
- $this->isUpdateSku = true;
- return ResultWrapper::success(true);
- }
- /**
- * Doc: (des="获取最新单位的isNew")
- * User: XMing
- * Date: 2020/10/23
- * Time: 11:46 上午
- */
- public function getIsNewByGoodsId($goodsId): ResultWrapper
- {
- $result = $this->objDSku->get(['goodsId' => $goodsId,'isMaster' => StatusCode::$standard],'isNew');
- if ($result === false){
- return ResultWrapper::fail($this->objDSku->error(),ErrorCode::$dberror);
- }
- return ResultWrapper::success(isset($result['isNew']) ? $result['isNew'] : StatusCode::$standard);
- }
- /**
- * 批量设置sku
- * @param $params
- * @return ResultWrapper
- */
- public function setSku($params)
- {
- if (!is_array($params['goodsId'])) return ResultWrapper::fail('参数格式错误', ErrorCode::$paramError);
- $last = $this->objDSku->select([], 'id', 'id DESC', 1);//获取最后一条数据的id
- if (is_array($params['goodsId'])) {
- $skuData = [];
- foreach ($params['goodsId'] as $key => $val) {
- foreach ($params['branchData'] as $sku) {
- $skuData[] = [
- 'id' => '',
- 'goodsId' => $val,
- 'conversion' => $sku['conversion'],
- 'unitName' => $sku['unitName'],
- 'unitId' => $sku['unitId'],
- 'isMaster' => $sku['isMaster'],
- ];
- }
- }
- $dbResult = $this->objDSku->replace($skuData, true);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
- }
- self::updateCacheSku($last);
- return ResultWrapper::success($dbResult);
- }
- }
- /**
- * @param $sql
- * @return ResultWrapper
- */
- public function getQuery($sql)
- {
- $dbResult = $this->objDSku->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 根据goodsId获取sku信息 根据主单位辅单位区分
- * @param $params
- * @param string $fields
- * @param bool $group
- * @return ResultWrapper
- */
- public function getSkuInfo($params, $fields = 'id,goodsId,conversion,unitName,unitId,isMaster', $group = true)
- {
- $params['deleteStatus'] = StatusCode::$standard;
- $dbResult = $this->objDSku->select($params, $fields);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
- }
- if ($group) {
- foreach ($dbResult as $key => &$val) {
- $isMaster = $val['isMaster'];
- $goodsId = $val['goodsId'];
- //unset($val['isMaster']);
- unset($val['goodsId']);
- if ($isMaster == StatusCode::$standard) {
- $data[$goodsId]['masterUnit'] = $val;
- } else {
- $data[$goodsId]['branchUnit'][] = $val;
- }
- }
- } else {
- $data = $dbResult;
- }
- return ResultWrapper::success(isset($data) ? $data : []);
- }
- /**
- * 根据ids数组获取name
- * @param $ids
- * @param bool $type
- * @return array|ResultWrapper
- */
- public function getNameByIds($ids, $type = false)
- {
- $dbResult = $this->objDSku->select($ids, '*');
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
- }
- if (empty($dbResult)) {
- return ResultWrapper::fail('skuIds错误', ErrorCode::$dberror);
- }
- $returnData = [];
- foreach ($dbResult as $key => $value) {
- if ($type) {
- $returnData[$value['id']] = $value;
- } else {
- $returnData[$value['id']]['id'] = $value['id'];
- $returnData[$value['id']]['unitName'] = $value['unitName'];
- }
- }
- return ResultWrapper::success($returnData);
- }
- /**
- * 根据ids数组获取name
- * @param $GoodsIds
- * @param bool $isMaster
- * @return array|ResultWrapper
- */
- public function getSkuDataByGoodsIds($GoodsIds, $isMaster = false)
- {
- $skuData = [];
- $falseResult = [];
- foreach ($GoodsIds as $value){
- $cacheResult = $this->objSkuCache->getGoodsBasicSku($value);
- if($cacheResult) {
- $skuData[$value] = $cacheResult;
- }else{
- $falseResult[] = $value;
- }
- }
- if(empty($falseResult)){
- if($isMaster){
- $return = [];
- foreach($skuData as $goodsId => $sku){
- foreach($sku as $skuId => $v){
- if($v['isMaster'] == StatusCode::$standard){
- $return[$goodsId][$skuId] = $v;
- }
- }
- }
- return ResultWrapper::success($return);
- }
- return ResultWrapper::success($skuData);
- }
- $where = ['goodsId' => $falseResult];
- $where['deleteStatus'] = StatusCode::$standard;
- $dbResult = $this->objDSku->select($where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
- }
- $data = $dbResult;
- unset($dbResult);
- $format = [];
- foreach($data as $value){
- $format[$value['goodsId']][$value['id']] = $value;
- }
- foreach($format as $goodsId => $value){
- $this->objSkuCache->addGoodsBasicSku($goodsId,$value);
- $skuData[$goodsId] = $value;
- }
- if($isMaster){
- $return = [];
- foreach($skuData as $goodsId => $sku){
- foreach($sku as $skuId => $v){
- if($v['isMaster'] == StatusCode::$standard){
- $return[$goodsId][$skuId] = $v;
- }
- }
- }
- return ResultWrapper::success($return);
- }
- return ResultWrapper::success($skuData);
- }
- /**
- * @param $skuIds
- * @return array
- */
- public function getSku($skuIds)
- {
- $return = [];
- if (!$skuIds) {
- return $return;
- }
- $dbResult = $this->objDSku->select($skuIds, '*');
- if ($dbResult === false) {
- return $return;
- }
- foreach ($dbResult as $sku) {
- $return[$sku['id']] = $sku;
- }
- return $return;
- }
- /**
- * 获取单个sku数据
- * @param $where
- * @return array
- */
- public function getSkuData($where = [])
- {
- $dbResult = $this->objDSku->get($where);
- if ($dbResult === false) {
- return [];
- }
- return $dbResult;
- }
- /**
- * @param $selectParams
- * @return ResultWrapper
- */
- public function getMaterSkuId($selectParams)
- {
- $dbResult = $this->objDSku->select($selectParams, 'id,goodsId');
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
- }
- $mapping = [];
- foreach ($dbResult as $val) {
- $mapping[$val['goodsId']] = $val['id'];
- }
- return ResultWrapper::success($mapping);
- }
- /**
- * todo(这个缓存方法废弃了,但是好像很多地方用,暂时没删除)
- * @param $last
- * @param string $action
- * @return bool
- */
- public function updateCacheSku($last)
- {
- $lastId = isset($last[0]['id']) ? $last[0]['id'] : 0;
- $objGoodsBasicRelevant = new GoodsBasicRelevant($this->onlineEnterpriseId);
- $dbResult = $this->objDSku->select("id > {$lastId}", 'id,unitName');
- if ($dbResult == false) {
- return false;
- }
- foreach ($dbResult as $key => $val) {
- $objGoodsBasicRelevant->cacheSkuIdRelationName($val['unitName'], $val['id']);
- }
- return true;
- }
- /**
- * @param $selectParams
- * @return ResultWrapper
- */
- public function getSkuByGoodsBasicId($selectParams)
- {
- $selectParams['deleteStatus'] = StatusCode::$standard;
- $dbResult = $this->objDSku->select($selectParams, 'id,unitName,specData,unitId,specImage,conversion,isMaster,specType');
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 获取sku数据
- * @param $skuIds
- * @return ResultWrapper
- * @throws \Exception
- */
- public function getSkuDataBySkuIds($skuIds)
- {
- if (empty($skuIds)) return ResultWrapper::success([]);
- $skuData = [];
- $falseResult = [];
- if(!is_array($skuIds)){
- $skuIds = [$skuIds];
- }
- foreach ($skuIds as $value){
- $cacheResult = $this->objSkuCache->getSku($value,true);
- if($cacheResult && isset($cacheResult['specType'])) {
- $skuData[] = $cacheResult;
- }else{
- $falseResult[] = $value;
- }
- }
- if(!empty($falseResult)){
- $where = ['id' => $falseResult];
- $dbResult = $this->objDSku->select($where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
- }
- if(!empty($dbResult)){
- foreach ($dbResult as $value){
- $skuData[] = $value;
- $cacheMap = self::cacheMappersSku($value);
- $this->objSkuCache->delSku($value['id']);
- $cacheResult = $this->objSkuCache->cacheSku($value['id'], $cacheMap);
- if(!$cacheResult->isSuccess()){
- return ResultWrapper::fail($cacheResult->getData(), $cacheResult->getErrorCode());
- }
- }
- }
- }
- $returnData = [];
- foreach ($skuData as $value) {
- //单规格
- $returnData[$value['id']]['unitId'] = $value['unitId'];
- $returnData[$value['id']]['unitName'] = $value['unitName'];
- $returnData[$value['id']]['barCode'] = $value['barCode'];
- $returnData[$value['id']]['isMaster'] = $value['isMaster'];
- $returnData[$value['id']]['skuName'] = '';
- $returnData[$value['id']]['conversion'] = $value['conversion'];
- $returnData[$value['id']]['specData'] = [];
- if ($value['specType'] == StatusCode::$specType['multiple']) {
- //多规格
- $returnData[$value['id']]['skuName'] = '';
- $specData = json_decode($value['specData'], true);
- foreach ($specData as $vv) {
- $returnData[$value['id']]['specData'][$vv['specId']]['specName'] = $vv['specName'];
- $returnData[$value['id']]['specData'][$vv['specId']]['specValue'][$vv['specValueId']] = $vv['specValueName'];
- $returnData[$value['id']]['skuName'] = empty($returnData[$value['id']]['skuName']) ? $vv['specValueName'] : $returnData[$value['id']]['skuName'].';'. $vv['specValueName'];
- }
- }
- }
- return ResultWrapper::success($returnData);
- }
- /**
- * 通过skuIds获取规格名
- * @param $skuIds
- * @return ResultWrapper
- */
- public function getSpecNameBySkuId($skuIds)
- {
- if (empty($skuIds)){
- return ResultWrapper::success([]);
- }
- $dbResult = $this->objDSku->select(['id' => $skuIds]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
- }
- if (empty($dbResult)) {
- return ResultWrapper::success([]);
- }
- $mapping = [];
- foreach ($dbResult as $row) {
- $unitName = $row['unitName'];
- $specData = [];
- $specName = '';
- if ($row['specType'] == StatusCode::$specType['multiple']) {
- $specData = json_decode($row['specData'], true);
- $arrSpecName = array_column($specData, 'specValueName');
- $specName = rtrim(implode('_', $arrSpecName), '_');
- }
- if (isset($specName) && empty($specName)) {
- $unitName .= ' ' . $specName;
- }
- $mapping[$row['id']] = [
- 'specGroup' => empty($specData) ? [] : $specData,//属性组
- 'unitName' => $row['unitName'],//单位名称
- 'nameFields' => $unitName,//拼接好的名字
- 'isMaster' => $row['isMaster'],//是否是主单位
- 'conversion' => $row['conversion'],//换算比率
- 'specImages' => empty($row['specImage']) ? [] : json_decode($row['specImage'], true),
- 'barCode' => empty($row['barCode']) ? '' : $row['barCode'],
- 'weight' => empty($row['weight']) ? 0 : $row['weight'],
- 'isNew' => $row['isNew']
- ];
- }
- return ResultWrapper::success($mapping);
- }
- /**
- * 传入skuIds 获取sku对应主单位skuId及换算比例
- * @param $skuIds
- * @return ResultWrapper
- */
- public function getConversion($skuIds)
- {
- if (empty($skuIds)){
- return ResultWrapper::success([]);
- }
- $dbResult = $this->objDSku->select(['id' => $skuIds], 'id,isMaster,unitName,goodsId,specType,conversion,specGroupHash,isNew');
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
- }
- if (empty($dbResult)) {
- return ResultWrapper::success([]);
- }
- $mapping = [];
- foreach ($dbResult as $item) {
- if ($item['isMaster'] == StatusCode::$standard) {
- $mapping[$item['id']] = [
- 'masterSkuId' => $item['id'],
- 'conversion' => 0,
- 'isMaster' => StatusCode::$standard,
- 'deleteStatus' => StatusCode::$standard,
- 'masterUnitName' => $item['unitName'],
- 'conversionStr' => $item['unitName'].'(主)',
- 'isNew' => $item['isNew']
- ];
- } else {
- //获取当前属性主单位skuID
- $masterSku = $this->objDSku->get(
- [
- 'isMaster' => StatusCode::$standard,
- 'goodsId' => $item['goodsId'],
- 'specGroupHash' => $item['specGroupHash'],
- 'deleteStatus' => StatusCode::$standard
- ],'id,unitName');
- $mapping[$item['id']] = [
- 'masterSkuId' => $masterSku['id'],
- 'conversion' => $item['conversion'],
- 'isMaster' => StatusCode::$delete,
- 'isNew' => $item['isNew'],
- 'conversionStr' => '1'.$item['unitName'].'='.floatval($item['conversion']).$masterSku['unitName'],
- 'masterUnitName' => $masterSku['unitName'],
- ];
- }
- }
- return ResultWrapper::success($mapping);
- }
- /**
- * Doc: (des="删除基础资料下sku缓存")
- * User: XMing
- * Date: 2020/10/21
- * Time: 7:42 下午
- * @param $goodsBasicId
- * @return ResultWrapper
- */
- public function delSkuCache($goodsBasicId): ResultWrapper
- {
- $result = $this->objSkuCache->delGoodsBasicSku($goodsBasicId);
- return ResultWrapper::success($result);
- }
- /**
- * @param array $select
- * @return ResultWrapper
- */
- public function getSkuByParams(array $select)
- {
- $dbResult = $this->objDSku->select($select);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2020/12/19
- * Time: 9:51 上午
- * @param $ids
- * @return ResultWrapper
- */
- public function getSkuMapByIds($ids): ResultWrapper
- {
- if (empty($ids)){
- return ResultWrapper::success([]);
- }
- $lists = $this->objDSku->select(['id' => $ids],'id,unitName,specData as specGroup');
- if ($lists === false) {
- return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
- }
- $map = [];
- foreach ($lists as $list){
- $list['specGroup'] = !empty($list['specGroup']) ? json_decode($list['specGroup'],true) : [];
- $map[$list['id']] = $list;
- }
- return ResultWrapper::success($map);
- }
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2021/3/4
- * Time: 4:29 下午
- * @param array $ids
- * @return ResultWrapper
- */
- public function getMasterUnitNameByMaterielIds(array $ids): ResultWrapper
- {
- if (empty($ids)){
- return ResultWrapper::success([]);
- }
- $lists = $this->objDSku->select([
- 'goodsId' => $ids,
- 'deleteStatus' => StatusCode::$standard,
- 'isMaster' => StatusCode::$standard
- ],'unitName,goodsId');
- if ($lists === false){
- Logger::logs(E_USER_ERROR,'sql error',__CLASS__,__LINE__,$this->objDSku->error());
- return ResultWrapper::fail($this->objDSku->error(),ErrorCode::$dberror);
- }
- $map = [];
- foreach ($lists as $value){
- if (!isset($map[$value['goodsId']]) || empty($map[$value['goodsId']])){
- $map[$value['goodsId']] = $value['unitName'];
- }
- }
- return ResultWrapper::success($map);
- }
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2021/3/13
- * Time: 4:14 下午
- * @param int $id
- * @return ResultWrapper
- */
- public function getMasterById(int $id): ResultWrapper
- {
- $result = $this->objDSku->get(['goodsId' => $id,'deleteStatus' => StatusCode::$standard,'isMaster' => StatusCode::$standard]);
- if ($result === false){
- return ResultWrapper::fail($this->objDSku->error(),ErrorCode::$dberror);
- }
- return ResultWrapper::success($result);
- }
- /**
- * @After
- */
- public function __destruct()
- {
- // TODO: Implement __destruct() method.
- self::cacheSku();
- }
- }
|