MCommissionGoods.Class.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. <?php
  2. namespace JinDouYun\Model\Commission;
  3. use JinDouYun\Dao\Commission\DCommissionGoodsStatistics;
  4. use Mall\Framework\Core\ErrorCode;
  5. use Mall\Framework\Core\ResultWrapper;
  6. use Mall\Framework\Core\StatusCode;
  7. use JinDouYun\Controller\Common\Logger;
  8. use JinDouYun\Dao\Goods\DGoods;
  9. use JinDouYun\Dao\GoodsManage\DSku;
  10. use JinDouYun\Model\Stock\MInventory;
  11. use JinDouYun\Dao\Commission\DCommissionGoods;
  12. use JinDouYun\Dao\Commission\DCommissionGrade;
  13. use JinDouYun\Model\Price\MPrice;
  14. /**
  15. * 分销商品
  16. * Description:
  17. * Class MCommissionGoods
  18. */
  19. class MCommissionGoods
  20. {
  21. /**
  22. * @var DCommissionGoods
  23. */
  24. private $objDCommissionGoods;
  25. /**
  26. * @var integer 当前登陆企业id
  27. */
  28. private $onlineEnterpriseId;
  29. /**
  30. * @var integer 当前登陆用户id
  31. */
  32. private $onlineUserId;
  33. /**
  34. * @var MInventory
  35. */
  36. private $objMInventory;
  37. /**
  38. * @var DGoods
  39. */
  40. private $objDGoods;
  41. /**
  42. * @var DSku
  43. */
  44. private $objDSku;
  45. /**
  46. * @var DCommissionGrade
  47. */
  48. private $objDCommissionGrade;
  49. /**
  50. * @var MPrice
  51. */
  52. private $objMPrice;
  53. /**
  54. * @var DCommissionGoodsStatistics
  55. */
  56. private $objDCommissionGoodsStatistics;
  57. /**
  58. * MCommissionGoods constructor.
  59. * @param $onlineEnterpriseId
  60. * @param $onlineUserId
  61. * @throws \Exception
  62. */
  63. public function __construct($onlineEnterpriseId, $onlineUserId)
  64. {
  65. $this->onlineUserId = $onlineUserId;
  66. $this->onlineEnterpriseId = $onlineEnterpriseId;
  67. $this->objDCommissionGoods = new DCommissionGoods();
  68. $this->objDCommissionGoods->setTable('qianniao_commission_goods_' . $this->onlineEnterpriseId);
  69. $this->objMInventory = new MInventory($this->onlineEnterpriseId, $this->onlineUserId);
  70. $this->objDGoods = new DGoods();
  71. $this->objDGoods->setTable('qianniao_goods_' . $this->onlineEnterpriseId);
  72. $this->objDSku = new DSku();
  73. $this->objDSku->setTable('qianniao_sku_' . $this->onlineEnterpriseId);
  74. $this->objDCommissionGrade = new DCommissionGrade();
  75. $this->objMPrice = new MPrice($this->onlineUserId, $this->onlineEnterpriseId);
  76. $this->objDCommissionGoodsStatistics = new DCommissionGoodsStatistics();
  77. $this->objDCommissionGoodsStatistics->setTable('qianniao_commission_goods_statistics_'.$this->onlineEnterpriseId);
  78. }
  79. public function __destruct()
  80. {
  81. // TODO: Implement __destruct() method.
  82. }
  83. /**
  84. * Doc: (des="分销商品列表")
  85. * User: XMing
  86. * Date: 2020/7/22
  87. * Time: 2:23 下午
  88. * @param array $selectParams
  89. * @return ResultWrapper
  90. */
  91. public function getAll(array $selectParams)
  92. {
  93. $return = [
  94. 'data' => [],
  95. 'total' => 0
  96. ];
  97. $fields = 'g.id,g.isJoinCommission,g.basicGoodsId,b.title,b.images,s.warehouseId,g.enableStatus';
  98. $sql = 'SELECT ' . $fields . ' FROM qianniao_goods_' . $this->onlineEnterpriseId . ' as g
  99. LEFT JOIN qianniao_goods_basic_' . $this->onlineEnterpriseId . ' as b
  100. ON b.id = g.basicGoodsId
  101. LEFT JOIN qianniao_shop_1 as s
  102. ON s.id = g.shopId
  103. WHERE
  104. g.deleteStatus = ' . StatusCode::$standard;
  105. //商品名称
  106. if (isset($selectParams['keyword']) && !empty($selectParams['keyword'])) {
  107. $sql .= ' and b.title like "%' . $selectParams['keyword'] . '%"';
  108. }
  109. //上架/下架
  110. if (isset($selectParams['enableStatus']) && !empty($selectParams['enableStatus'])) {
  111. $sql .= ' and g.enableStatus = ' . $selectParams['enableStatus'];
  112. }
  113. //分类筛选
  114. if (isset($selectParams['categoryId']) && !empty($selectParams['categoryId'])){
  115. $sql .= ' and find_in_set('.$selectParams['categoryId'].',b.categoryPath) ';
  116. }
  117. //分销状态
  118. if (isset($selectParams['isJoinCommission']) && !empty($selectParams['isJoinCommission'])) {
  119. $sql .= ' and g.isJoinCommission = ' . $selectParams['isJoinCommission'];
  120. }
  121. $return['total'] = count((array)$this->objDCommissionGoods->query($sql));
  122. //排序 分页
  123. $sql .= ' order by g.createTime DESC LIMIT ' . $selectParams['offset'] . ',' . $selectParams['limit'];
  124. $dbResult = $this->objDCommissionGoods->query($sql);
  125. if ($dbResult === false) {
  126. return ResultWrapper::fail($this->objDCommissionGoods->error(), ErrorCode::$dberror);
  127. }
  128. if (empty($dbResult)) {
  129. return ResultWrapper::success($return);
  130. }
  131. $formatData = self::formatList((array)$dbResult);
  132. if (!$formatData->isSuccess()) {
  133. return ResultWrapper::fail($formatData->getData(), $formatData->getErrorCode());
  134. }
  135. $return['data'] = $formatData->getData();
  136. return ResultWrapper::success($return);
  137. }
  138. /**
  139. * Doc: (des="格式化分销商品列表数据")
  140. * User: XMing
  141. * Date: 2020/7/22
  142. * Time: 3:14 下午
  143. * @param array $data
  144. * @return ResultWrapper
  145. */
  146. public function formatList(array $data)
  147. {
  148. if (empty($data)) {
  149. return ResultWrapper::success($data);
  150. }
  151. $allGoodsIds = [];
  152. foreach ($data as $goods) {
  153. $allGoodsIds[] = $goods['id'];
  154. }
  155. //获取分销商品的销量和分销额
  156. $commissionGoodsStatistics = $this->objDCommissionGoodsStatistics->select(['goodsId' => $allGoodsIds],'goodsId,salesNum,commission');
  157. if ($commissionGoodsStatistics===false){
  158. return ResultWrapper::fail($this->objDCommissionGoodsStatistics->error(),ErrorCode::$dberror);
  159. }
  160. $commissionGoodsStatisticsMap = [];
  161. foreach ($commissionGoodsStatistics as $value){
  162. $commissionGoodsStatisticsMap[$value['goodsId']] = $value;
  163. }
  164. foreach ($data as &$item) {
  165. !is_array($item['images']) && $item['images'] = json_decode($item['images'], true);
  166. $item['commission'] = isset($commissionGoodsStatisticsMap[$item['id']]) ? $commissionGoodsStatisticsMap[$item['id']]['commission'] : '0.00';//佣金
  167. $item['inventoryTotal'] = 0;//总库存
  168. $item['salesCount'] = isset($commissionGoodsStatisticsMap[$item['id']]) ? $commissionGoodsStatisticsMap[$item['id']]['salesNum'] : 0;//总销量
  169. }
  170. return ResultWrapper::success($data);
  171. }
  172. /**
  173. * Doc: (des="批量更新商品分销状态")
  174. * User: XMing
  175. * Date: 2020/7/22
  176. * Time: 4:45 下午
  177. * @param array $params
  178. * @return ResultWrapper
  179. */
  180. public function updateIsJoin(array $params)
  181. {
  182. $dbResult = $this->objDGoods->update(['isJoinCommission' => $params['isJoinCommission'], 'updateTime' => time()], ['id' => $params['id']]);
  183. if ($dbResult === false) {
  184. return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
  185. }
  186. return ResultWrapper::success('操作成功');
  187. }
  188. /**
  189. * Doc: (des="获取分销商品详情")
  190. * User: XMing
  191. * Date: 2020/7/22
  192. * Time: 4:58 下午
  193. * @param int $id
  194. * @return ResultWrapper
  195. * @throws \Exception
  196. */
  197. public function getInfo(int $id)
  198. {
  199. $fields = 'g.id,g.shopId,b.title,g.isDefine,b.specType,b.images,g.id,g.isJoinCommission,s.warehouseId,g.basicGoodsId,g.retType';
  200. $sql = 'SELECT ' . $fields . ' FROM qianniao_goods_' . $this->onlineEnterpriseId . ' as g
  201. LEFT JOIN qianniao_goods_basic_' . $this->onlineEnterpriseId . ' as b
  202. ON b.id = g.basicGoodsId
  203. LEFT JOIN qianniao_shop_1 as s
  204. ON s.id = g.shopId
  205. WHERE g.id = ' . $id;
  206. $dbResult = $this->objDGoods->query($sql);
  207. if ($dbResult === false) {
  208. return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
  209. }
  210. if (empty($dbResult)) {
  211. return ResultWrapper::success([]);
  212. }
  213. $dbResult = (array)$dbResult;
  214. $goodsInfo = array_shift($dbResult);
  215. $formatData = self::formatDetails($goodsInfo);
  216. if (!$formatData->isSuccess()) {
  217. return ResultWrapper::fail($formatData->getData(), $formatData->getErrorCode());
  218. }
  219. return ResultWrapper::success($formatData->getData());
  220. }
  221. /**
  222. * Doc: (des="格式化分销商品详情数据")
  223. * {"grade_1":{"oneRate" : 1,"twoRate":2 ,"threeRate":3}."grade_2":{"oneRate" : 1,"twoRate":2 ,"threeRate":3}}
  224. * User: XMing
  225. * Date: 2020/7/22
  226. * Time: 5:14 下午
  227. * @param array $data
  228. * @return ResultWrapper
  229. * @throws \Exception
  230. */
  231. public function formatDetails(array $data)
  232. {
  233. if (empty($data)) {
  234. return ResultWrapper::success([]);
  235. }
  236. //获取商品的价格
  237. $priceSelectParam['material'] = [
  238. $data['shopId'] => [
  239. $data['id']
  240. ]
  241. ];
  242. $priceResult = $this->objMPrice->getPrice($priceSelectParam);
  243. if (!$priceResult->isSuccess()) {
  244. return ResultWrapper::fail($priceResult->getData(), $priceResult->getErrorCode());
  245. }
  246. $priceData = $priceResult->getData();
  247. //获取商品分销销量
  248. $CommissionGoodsStatistics = $this->objDCommissionGoodsStatistics->select(['goodsId' => $data['id']],'goodsId,skuId,salesNum');
  249. if ($CommissionGoodsStatistics === false){
  250. return ResultWrapper::fail($this->objDCommissionGoodsStatistics->error(),ErrorCode::$dberror);
  251. }
  252. $CommissionGoodsStatisticsMap = [];
  253. foreach ($CommissionGoodsStatistics as $value){
  254. $CommissionGoodsStatisticsMap[$value['skuId']] = $value;
  255. }
  256. //获取商品的所有sku
  257. $skuResult = $this->objDSku->select(['goodsId' => $data['basicGoodsId'], 'deleteStatus' => StatusCode::$standard]);
  258. if ($skuResult === false) {
  259. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  260. }
  261. $skuIds = [];
  262. foreach ($skuResult as $item){
  263. $skuIds[] = $item['id'];
  264. }
  265. $inventoryResult = $this->objMInventory->getInventoryByShopIdAndSkuIds($data['shopId'],$skuIds);
  266. if (!$inventoryResult->isSuccess()){
  267. return ResultWrapper::fail($inventoryResult->getData(),$inventoryResult->getErrorCode());
  268. }
  269. $inventoryData = $inventoryResult->getData();
  270. $skuMap = [];
  271. foreach ($skuResult as $item) {
  272. $skuMap[$item['goodsId']][] = [
  273. 'skuId' => $item['id'],
  274. 'unitName' => $item['unitName'],
  275. 'specGroupHash' => $item['specGroupHash'],
  276. 'specData' => empty($item['specData']) ? [] : json_decode($item['specData'], true),
  277. 'price' => isset($priceData[$data['shopId']][$data['id']][$item['id']]['salePrice']) ? $priceData[$data['shopId']][$data['id']][$item['id']]['salePrice'] : 0,
  278. 'saleNum' => isset($CommissionGoodsStatisticsMap[$item['id']]) ? $CommissionGoodsStatisticsMap[$item['id']]['salesNum'] : 0,
  279. 'inventory' => isset($inventoryData[$item['id']]['num']) ? $inventoryData[$item['id']]['num'] : 0,
  280. ];
  281. }
  282. //查询商品自定义比率
  283. $commissionData = [];
  284. if ($data['isDefine'] == StatusCode::$standard) {
  285. $commissionGoods = $this->objDCommissionGoods->select(['deleteStatus' => StatusCode::$standard, 'goodsId' => $data['id']]);
  286. if ($commissionGoods === false) {
  287. return ResultWrapper::fail($this->objDCommissionGoods->error(), ErrorCode::$dberror);
  288. }
  289. foreach ($commissionGoods as $item) {
  290. $commissionData[$item['skuId']] = json_decode($item['commissionRule'], true);
  291. }
  292. }
  293. //获取等级佣金比例
  294. $gradeResult = $this->objDCommissionGrade->select(['enterpriseId' => $this->onlineEnterpriseId, 'deleteStatus' => StatusCode::$standard, 'enableStatus' => StatusCode::$standard],'*','grade ASC');
  295. if ($gradeResult === false) {
  296. return ResultWrapper::fail($this->objDCommissionGrade->error(), ErrorCode::$dberror);
  297. }
  298. !is_array($data['images']) && $data['images'] = json_decode($data['images'], true);
  299. $data['specMultiple'] = isset($skuMap[$data['basicGoodsId']]) ? $skuMap[$data['basicGoodsId']] : [];
  300. $default_rule = [];
  301. $commission_rule = [];
  302. foreach ($gradeResult as $key => $row) {
  303. $rule = [];
  304. $default_rule[] = [
  305. 'name' => $row['name'],
  306. 'grade' => $row['grade'],
  307. 'rule' => [
  308. 'oneRate' => $row['oneRate'],
  309. 'twoRate' => $row['twoRate'],
  310. 'threeRate' => $row['threeRate']
  311. ]
  312. ];
  313. $commission_rule[$key] = [
  314. 'name' => $row['name'],
  315. 'grade' => $row['grade']
  316. ];
  317. foreach ($data['specMultiple'] as $datum) {
  318. $rule[] = [
  319. 'skuId' => $datum['skuId'],
  320. 'unitName' => $datum['unitName'],
  321. 'specData' => $datum['specData'],
  322. 'saleNum' => $datum['saleNum'],
  323. 'price' => $datum['price'],
  324. 'inventory' => $datum['inventory'],
  325. 'oneRate' => isset($commissionData[$datum['skuId']][$row['grade']]) ? $commissionData[$datum['skuId']][$row['grade']]['oneRate'] : 0,
  326. 'twoRate' => isset($commissionData[$datum['skuId']][$row['grade']]) ? $commissionData[$datum['skuId']][$row['grade']]['twoRate'] : 0,
  327. 'threeRate' => isset($commissionData[$datum['skuId']][$row['grade']]) ? $commissionData[$datum['skuId']][$row['grade']]['threeRate'] : 0
  328. ];
  329. }
  330. $commission_rule[$key]['rule'] = $rule;
  331. }
  332. $data['default_rule'] = $default_rule;//默认规则
  333. $data['commission_rule'] = $commission_rule;//自定义规则
  334. return ResultWrapper::success($data);
  335. }
  336. /**
  337. * Doc: (des="自定义分销商品")
  338. * User: XMing
  339. * Date: 2020/7/22
  340. * Time: 6:17 下午
  341. * @param array $data
  342. * @return ResultWrapper
  343. * @example {"id":131,"isDefine":5,"isJoinCommission":5,"commission_rule":[{"skuId":1036,"rule":{"0":{"oneRate":1,"twoRate":2,"threeRate":3},"2":{"oneRate":4,"twoRate":5,"threeRate":6},"3":{"oneRate":7,"twoRate":8,"threeRate":9},"4":{"oneRate":10,"twoRate":11,"threeRate":12}}},{"skuId":1037,"rule":{"0":{"oneRate":1,"twoRate":2,"threeRate":3},"2":{"oneRate":4,"twoRate":5,"threeRate":6},"3":{"oneRate":7,"twoRate":8,"threeRate":9},"4":{"oneRate":10,"twoRate":11,"threeRate":12}}},{"skuId":1038,"rule":{"0":{"oneRate":1,"twoRate":2,"threeRate":3},"2":{"oneRate":4,"twoRate":5,"threeRate":6},"3":{"oneRate":7,"twoRate":8,"threeRate":9},"4":{"oneRate":10,"twoRate":11,"threeRate":12}}}]}
  344. */
  345. public function setCommission(array $data)
  346. {
  347. $this->objDCommissionGoods->beginTransaction();
  348. //非单独设置
  349. $dbResult = $this->objDGoods->update([
  350. 'isJoinCommission' => $data['isJoinCommission'],
  351. 'isDefine' => $data['isDefine'],
  352. 'retType' => $data['retType'],
  353. 'updateTime' => time()
  354. ], ['id' => $data['id']]);
  355. if ($dbResult === false) {
  356. $this->objDGoods->rollBack();
  357. return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
  358. }
  359. if (isset($data['isDefine']) && $data['isDefine'] == StatusCode::$standard) {
  360. //单独设置了分销数据
  361. $insert = [];
  362. //[{"skuId":135,"rule":{"grade_1":{"oneRate" : 1,"twoRate":2 ,"threeRate":3}."grade_2":{"oneRate" : 1,"twoRate":2 ,"threeRate":3}}}]
  363. foreach ($data['commission_rule'] as $item) {
  364. $insert[] = [
  365. 'goodsId' => $data['id'],
  366. 'skuId' => $item['skuId'],
  367. 'commissionRule' => json_encode($item['rule'])
  368. ];
  369. }
  370. $commissionGoodsResult = $this->objDCommissionGoods->replace($insert, true);
  371. if ($commissionGoodsResult === false) {
  372. $this->objDCommissionGoods->rollBack();
  373. return ResultWrapper::fail($this->objDCommissionGoods->error(), ErrorCode::$dberror);
  374. }
  375. }
  376. $this->objDCommissionGoods->commit();
  377. return ResultWrapper::success('操作成功');
  378. }
  379. /**
  380. * Doc: (des="格式化分销商品数据")
  381. * User: XMing
  382. * Date: 2020/7/28
  383. * Time: 7:50 下午
  384. * @param array $params
  385. * @param int $customerId
  386. * @throws \Exception
  387. * @return ResultWrapper
  388. */
  389. public function formatGoods(array $params,$customerId = null)
  390. {
  391. $allGoodsIds = [];
  392. $allSkuIds = [];
  393. foreach ($params as $value){
  394. $allGoodsIds[] = $value['goodsId'];
  395. $allSkuIds[] = $value['skuId'];
  396. }
  397. $allGoodsIdStr = implode(',',$allGoodsIds);
  398. $fields = 'g.id,g.basicGoodsId,g.shopId,b.title as goodsName,b.images,b.describe,s.warehouseId,s.name as shopName';
  399. $sql = 'select '.$fields.' from qianniao_goods_'.$this->onlineEnterpriseId.' as g
  400. left join qianniao_goods_basic_'.$this->onlineEnterpriseId.' as b
  401. on b.id = g.basicGoodsId
  402. left join qianniao_shop_1 as s
  403. on g.shopId = s.id
  404. where g.id in('.$allGoodsIdStr.')';
  405. $goods = $this->objDGoods->query($sql);
  406. if ($goods === false){
  407. Logger::logs(E_USER_ERROR,'sql',__CLASS__,__LINE__,$this->objDGoods->error());
  408. return ResultWrapper::fail($this->objDGoods->error(),ErrorCode::$dberror);
  409. }
  410. $priceSelectParam = [];
  411. $allGoodsMap = [];//商品集合
  412. if (!empty($customerId)){
  413. $priceSelectParam['customerId'] = $customerId;
  414. }
  415. foreach ($goods as &$value){
  416. $priceSelectParam['material'][$value['shopId']][] = $value['id'];
  417. $value['images'] = empty($value['images']) ? [] : json_decode($value['images'],true);
  418. $allGoodsMap[$value['id']] = $value;
  419. }
  420. unset($value);
  421. //获取所有单位
  422. $sku = $this->objDSku->select(['id'=>$allSkuIds],'id,unitName,specData,isNew');
  423. if ($sku === false){
  424. Logger::logs(E_USER_ERROR,'sql',__CLASS__,__LINE__,$this->objDSku->error());
  425. return ResultWrapper::fail($this->objDSku->error(),ErrorCode::$dberror);
  426. }
  427. $skuMap = [];//sku集合
  428. foreach ($sku as &$value){
  429. $value['specGroup'] = empty($value['specData']) ? [] : json_decode($value['specData'],true);
  430. $skuMap[$value['id']] = $value;
  431. }
  432. unset($value);
  433. //获取商品的价格
  434. $priceResult = $this->objMPrice->getPrice($priceSelectParam);
  435. if (!$priceResult->isSuccess()) {
  436. Logger::logs(E_USER_ERROR,'sql',__CLASS__,__LINE__,$priceResult->getData());
  437. return ResultWrapper::fail($priceResult->getData(), $priceResult->getErrorCode());
  438. }
  439. $priceData = $priceResult->getData();
  440. //获取库存
  441. //获取商品的库存
  442. $inventorySelectParams = [];
  443. foreach ($goods as $value){
  444. $inventorySelectParams[$value['shopId']][] = $value['skuId'];
  445. }
  446. $inventoryMap = [];
  447. foreach ($inventorySelectParams as $shopId => $skuIds){
  448. $inventoryResult = $this->objMInventory->getInventoryByShopIdAndSkuIds($shopId,$skuIds);
  449. if (!$inventoryResult->isSuccess()){
  450. return ResultWrapper::fail($inventoryResult->getData(),$inventoryResult->getErrorCode());
  451. }
  452. $inventoryMap[$shopId] = $inventoryResult->getData();
  453. }
  454. foreach ($params as $key => &$param){
  455. if (!isset($skuMap[$param['skuId']]) || !isset($allGoodsMap[$param['goodsId']])){
  456. unset($params[$key]);
  457. }
  458. $param['basicGoodsId'] = isset($allGoodsMap[$param['goodsId']]) ? $allGoodsMap[$param['goodsId']]['basicGoodsId'] : 0;
  459. $param['shopId'] = isset($allGoodsMap[$param['goodsId']]) ? $allGoodsMap[$param['goodsId']]['shopId'] : 0;
  460. $param['salePrice'] = isset($priceData[$param['shopId']][$param['goodsId']][$param['skuId']]) ? $priceData[$param['shopId']][$param['goodsId']][$param['skuId']]['salePrice'] : 0;
  461. $param['marketPrice'] = isset($priceData[$param['shopId']][$param['goodsId']][$param['skuId']]) ? $priceData[$param['shopId']][$param['goodsId']][$param['skuId']]['marketPrice'] : 0;
  462. $param['unitName'] = isset($skuMap[$param['skuId']]) ? $skuMap[$param['skuId']]['unitName'] : '';
  463. $param['specGroup'] = isset($skuMap[$param['skuId']]) ? $skuMap[$param['skuId']]['specGroup'] : [];
  464. $param['inventory'] = isset($inventoryMap[$param['shopId']][$param['skuId']]['num']) ? $inventoryMap[$param['shopId']][$param['skuId']]['num'] : 0;
  465. $param['images'] = isset($allGoodsMap[$param['goodsId']]) ? $allGoodsMap[$param['goodsId']]['images'] : [];
  466. $param['goodsName'] = isset($allGoodsMap[$param['goodsId']]) ? $allGoodsMap[$param['goodsId']]['goodsName'] : '';
  467. }
  468. return ResultWrapper::success($params);
  469. }
  470. /**
  471. * Doc: (des="换算库存")
  472. * User: XMing
  473. * Date: 2020/7/29
  474. * Time: 10:00 上午
  475. * @param array $inventory
  476. * @return ResultWrapper
  477. */
  478. public function conversionInventory(array $inventory)
  479. {
  480. //查询出所有基础商品sku数据
  481. $allGoodsBasicIds = [];
  482. foreach ($inventory as $key => $value){
  483. $allGoodsBasicIds[] = $key;
  484. }
  485. $allSku = $this->objDSku->select(['goodsId'=>$allGoodsBasicIds,'deleteStatus'=>StatusCode::$standard],'id,goodsId,unitId,isMaster,specGroupHash,conversion,specType,isNew');
  486. if ($allSku === false){
  487. return ResultWrapper::fail($this->objDSku->error(),ErrorCode::$dberror);
  488. }
  489. $allSkuMap = [];
  490. $allSkuHashMap = [];
  491. foreach ($allSku as $value){
  492. $allSkuMap[$value['goodsId']][$value['id']] = $value;
  493. $hash = md5($value['goodsId'].$value['isMaster'].$value['specGroupHash']);
  494. $allSkuHashMap[$value['goodsId']][$hash] = $value;
  495. }
  496. foreach ($allSkuMap as $goodsBasicId => $items){
  497. foreach($items as $skuId => $item){
  498. if ($item['isMaster'] == StatusCode::$standard){
  499. $allSkuMap[$goodsBasicId][$skuId]['inventoryNum'] = isset($inventory[$goodsBasicId][$skuId]['inventoryNum']) ? $inventory[$goodsBasicId][$skuId]['inventoryNum'] : 0;
  500. $allSkuMap[$goodsBasicId][$skuId]['costPrice'] = isset($inventory[$goodsBasicId][$skuId]['costPrice']) ? $inventory[$goodsBasicId][$skuId]['costPrice'] : 0;
  501. continue;
  502. }
  503. //辅助单位
  504. //获取到辅助单位对应主单位skuId
  505. $hash = md5($goodsBasicId.StatusCode::$standard.$item['specGroupHash']);
  506. if (!isset($allSkuHashMap[$goodsBasicId][$hash]['id'])){
  507. $allSkuMap[$goodsBasicId][$skuId]['inventoryNum'] = 0;
  508. $allSkuMap[$goodsBasicId][$skuId]['costPrice'] = 0;
  509. continue;
  510. }
  511. $conversion = $item['conversion'];
  512. $masterSkuId = $allSkuHashMap[$goodsBasicId][$hash]['id'];
  513. //获取主单位库存
  514. if (!isset($inventory[$goodsBasicId][$masterSkuId]['inventoryNum'])){
  515. $allSkuMap[$goodsBasicId][$skuId]['inventoryNum'] = 0;
  516. $allSkuMap[$goodsBasicId][$skuId]['costPrice'] = 0;
  517. continue;
  518. }
  519. $masterInv = $inventory[$goodsBasicId][$masterSkuId]['inventoryNum'];
  520. $masterCostPrice = $inventory[$goodsBasicId][$masterSkuId]['costPrice'];
  521. if ($masterInv == 0){
  522. $allSkuMap[$goodsBasicId][$skuId]['inventoryNum'] = 0;
  523. $allSkuMap[$goodsBasicId][$skuId]['costPrice'] = 0;
  524. continue;
  525. }
  526. if ($item['isNew'] == StatusCode::$standard){
  527. //新
  528. //主换辅 除
  529. $inventoryNum = bcdiv($masterInv,$conversion,8);
  530. }else{
  531. //旧
  532. //主换辅 乘
  533. $inventoryNum = bcmul($masterInv,$conversion,8);
  534. }
  535. //$inventoryNum = bcmul($masterInv,$conversion,8);
  536. $allSkuMap[$goodsBasicId][$skuId]['inventoryNum'] = $inventoryNum;
  537. $allSkuMap[$goodsBasicId][$skuId]['costPrice'] = $masterCostPrice;
  538. }
  539. }
  540. return ResultWrapper::success($allSkuMap);
  541. }
  542. }