MComBinPackage.Class.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. <?php
  2. namespace JinDouYun\Model\Market;
  3. use JinDouYun\Dao\Market\DComBinLog;
  4. use JinDouYun\Dao\Market\DComBinPackage;
  5. use JinDouYun\Model\Goods\MGoods;
  6. use JinDouYun\Model\MBaseModel;
  7. use JinDouYun\Model\System\MBasicSetup;
  8. use Mall\Framework\Core\ErrorCode;
  9. use Mall\Framework\Core\ResultWrapper;
  10. use Mall\Framework\Core\StatusCode;
  11. /**
  12. * @copyright Copyright (c) https://www.qianniaovip.com All rights reserved
  13. * Description:
  14. * Class MComBinPackage
  15. * @package JinDouYun\Model\Market
  16. */
  17. class MComBinPackage extends MBaseModel
  18. {
  19. /**
  20. * @var DComBinPackage
  21. */
  22. private $objDComBinPackage;
  23. /**
  24. * @var
  25. */
  26. private $onlineEnterpriseId;
  27. /**
  28. * @var
  29. */
  30. private $onlineUserId;
  31. /**
  32. * @var int 预售状态
  33. */
  34. private $preSale;
  35. /**
  36. * @var DComBinLog
  37. */
  38. private $objDComBinLog;
  39. /**
  40. * MComBinPackage constructor.
  41. * @param string $onlineEnterpriseId
  42. * @param string $onlineUserId
  43. * @throws \Exception
  44. */
  45. public function __construct($onlineEnterpriseId = '', $onlineUserId = '')
  46. {
  47. $this->onlineEnterpriseId = $onlineEnterpriseId;
  48. $this->onlineUserId = $onlineUserId;
  49. parent::__construct($onlineEnterpriseId, $onlineUserId);
  50. $this->objDComBinPackage = new DComBinPackage();
  51. $this->objDComBinLog = new DComBinLog();
  52. $this->objDComBinPackage->setTable('qianniao_combin_package_' . $this->onlineEnterpriseId);
  53. $this->objDComBinLog->setTable('qianniao_combin_user_log_'.$this->onlineEnterpriseId);
  54. self::getPreSale();
  55. }
  56. /**
  57. * Doc: (des="")
  58. * User: XMing
  59. * Date: 2020/12/25
  60. * Time: 11:50 上午
  61. * @return ResultWrapper
  62. */
  63. public function getPreSale(): ResultWrapper
  64. {
  65. $objMBasicSetup = new MBasicSetup($this->onlineEnterpriseId);
  66. $setResult = $objMBasicSetup->getBasicField('preSale');
  67. $this->preSale = StatusCode::$delete;
  68. if (!$setResult->isSuccess()){
  69. return ResultWrapper::fail($setResult->getData(),$setResult->getErrorCode());
  70. }
  71. $set = (array)$setResult->getData();
  72. $this->preSale = getArrayItem($set,'preSale',StatusCode::$delete);
  73. if (empty($this->preSale)) $this->preSale = StatusCode::$delete;
  74. return ResultWrapper::success($set);
  75. }
  76. /**
  77. * Doc: (des="套餐添加")
  78. * User: XMing
  79. * Date: 2021/1/21
  80. * Time: 3:28 下午
  81. * @param array $params
  82. * @return ResultWrapper
  83. */
  84. public function add(array $params): ResultWrapper
  85. {
  86. $insert = self::buildPackageData($params);
  87. $insert['createTime'] = time();
  88. $insert['updateTime'] = time();
  89. $result = $this->objDComBinPackage->insert($insert);
  90. if ($result === false) {
  91. return ResultWrapper::fail($this->objDComBinPackage->error(), ErrorCode::$dberror);
  92. }
  93. return ResultWrapper::success($result);
  94. }
  95. /**
  96. * Doc: (des="套餐列表")
  97. * User: XMing
  98. * Date: 2021/1/21
  99. * Time: 5:18 下午
  100. * @param array $selectParams
  101. * @return ResultWrapper
  102. */
  103. public function getAll(array $selectParams): ResultWrapper
  104. {
  105. $buildSql = self::buildSqlBySelectParams($selectParams);
  106. $lists = $this->objDComBinPackage->query($buildSql['sql']);
  107. if ($lists === false) {
  108. return ResultWrapper::fail($this->objDComBinPackage->error(), ErrorCode::$dberror);
  109. }
  110. $count = $this->objDComBinPackage->query($buildSql['countSql']);
  111. if ($lists === false) {
  112. return ResultWrapper::fail($this->objDComBinPackage->error(), ErrorCode::$dberror);
  113. }
  114. $formatListsResult = self::formatLists($lists);
  115. if (!$formatListsResult->isSuccess()) {
  116. return ResultWrapper::fail($formatListsResult->getData(), $formatListsResult->getErrorCode());
  117. }
  118. $formatLists = $formatListsResult->getData();
  119. $ret = [
  120. 'data' => $formatLists,
  121. 'total' => isset($count[0]['total']) ? $count[0]['total'] : 0,
  122. ];
  123. return ResultWrapper::success($ret);
  124. }
  125. /**
  126. * Doc: (des="格式化数据")
  127. * User: XMing
  128. * Date: 2021/1/21
  129. * Time: 5:46 下午
  130. * @param array $lists
  131. * @return ResultWrapper
  132. */
  133. private function formatLists(array $lists): ResultWrapper
  134. {
  135. if (empty($lists)) {
  136. return ResultWrapper::success([]);
  137. }
  138. foreach ($lists as &$list) {
  139. unset($list['goodsData']);
  140. $list['allowInventory'] = bcsub($list['inventory'], $list['saleNum'], 2);
  141. }
  142. return ResultWrapper::success($lists);
  143. }
  144. /**
  145. * Doc: (des="构建sql")
  146. * User: XMing
  147. * Date: 2021/1/21
  148. * Time: 5:40 下午
  149. * @param array $selectParams
  150. * @return string[]
  151. */
  152. protected function buildSqlBySelectParams(array $selectParams): array
  153. {
  154. $tableName = $this->objDComBinPackage->get_Table();
  155. $fields = ' * ';
  156. $countFiled = ' COUNT(id) as total ';
  157. $time = time();
  158. $where = '';
  159. if (isset($selectParams['enableStatus']) && !empty($selectParams['enableStatus'])) {
  160. $where .= ' AND `enableStatus` = ' . $selectParams['enableStatus'];
  161. }
  162. if (isset($selectParams['keyword']) && !empty($selectParams['keyword'])) {
  163. $keyword = '"%' . $selectParams['keyword'] . '%"';
  164. $where .= ' AND `title` LIKE ' . $keyword;
  165. }
  166. // 1 未开始 2进行中 3已结束
  167. if (isset($selectParams['state']) && !empty($selectParams['state'])) {
  168. switch ($selectParams['state']) {
  169. case 1:
  170. $where .= ' AND `startTime` > ' . $time . ' AND `isExpire` = ' . StatusCode::$standard;
  171. break;
  172. case 2:
  173. $where .= ' AND ( (`startTime` <= ' . $time . ' AND `endTime` >= ' . $time.') OR `isExpire` = '.StatusCode::$delete.') ';
  174. break;
  175. case 3:
  176. $where .= ' AND `endTime` < ' . $time . ' AND `isExpire` = ' . StatusCode::$standard;
  177. break;
  178. }
  179. }
  180. if (isset($selectParams['shopId']) && !empty($selectParams['shopId'])){
  181. $where .= ' AND `shopId` = '.$selectParams['shopId'];
  182. }
  183. if (isset($selectParams['goodsId']) && !empty($selectParams['goodsId'])){
  184. $where .= ' AND FIND_IN_SET('.$selectParams['goodsId'].',`goodsIds`)';
  185. }
  186. $countSql = 'SELECT ' . $countFiled . ' FROM ' . $tableName . ' WHERE deleteStatus = ' . StatusCode::$standard . $where;
  187. $where .= ' ORDER BY createTime DESC ';
  188. if (isset($selectParams['limit']) && empty($selectParams['limit'])) {
  189. $where .= ' LIMIT ' . $selectParams['offset'] . ',' . $selectParams['limit'];
  190. }
  191. $sql = 'SELECT ' . $fields . ' FROM ' . $tableName . ' WHERE deleteStatus = ' . StatusCode::$standard . $where;
  192. return [
  193. 'sql' => $sql,
  194. 'countSql' => $countSql
  195. ];
  196. }
  197. /**
  198. * Doc: (des="")
  199. * User: XMing
  200. * Date: 2021/1/21
  201. * Time: 3:30 下午
  202. * @param array $params
  203. * @return array
  204. */
  205. public function buildPackageData(array $params): array
  206. {
  207. return [
  208. "title" => getArrayItem($params, 'title', ''),
  209. //varchar(30) DEFAULT '' COMMENT '活动名称',
  210. "isExpire" => getArrayItem($params, 'isExpire', StatusCode::$delete),
  211. //tinyint(3) DEFAULT '4' COMMENT '是否具有时间限制 默认5 4不限时间 5限制时间',
  212. "startTime" => getArrayItem($params, 'startTime', 0),
  213. //int(10) NOT NULL DEFAULT '0' COMMENT '活动开始时间',
  214. "endTime" => getArrayItem($params, 'endTime', 0),
  215. //int(10) NOT NULL DEFAULT '0' COMMENT '活动结束时间',
  216. "shopId" => getArrayItem($params, 'shopId', 0),
  217. //int(10) NOT NULL DEFAULT '0' COMMENT '商铺id',
  218. "shopName" => getArrayItem($params, 'shopName', ''),
  219. //varchar(30) DEFAULT NULL COMMENT '商铺名称',
  220. "goodsData" => getArrayItem($params, 'goodsData', null),
  221. //json DEFAULT NULL COMMENT '活动商品数据',
  222. "price" => getArrayItem($params, 'price', 0),
  223. //decimal(10,2) DEFAULT '0.00' COMMENT '套餐价格',
  224. "originPrice" => getArrayItem($params, 'originPrice', 0),
  225. //decimal(10,2) DEFAULT '0.00' COMMENT '套餐价格',
  226. "inventory" => getArrayItem($params, 'inventory', 0),
  227. //decimal(10,2) DEFAULT NULL COMMENT '套餐设置库存',
  228. "isLimit" => getArrayItem($params, 'isLimit', StatusCode::$delete),
  229. //tinyint(3) DEFAULT '4' COMMENT '是否开启每人限购 默认4 4不开启 5开启',
  230. "limitNum" => getArrayItem($params, 'limitNum', 0),
  231. //decimal(10,2) DEFAULT '0.00' COMMENT '每人限制购买数量',
  232. "expressType" => getArrayItem($params, 'expressType', StatusCode::$delete),
  233. //tinyint(3) DEFAULT '4' COMMENT '运费设置 默认4 4默认跟随商品 5包邮',
  234. "enableStatus" => getArrayItem($params, 'enableStatus', StatusCode::$delete),
  235. //tinyint(3) DEFAULT '4' COMMENT '是否启用 默认4 4禁用 5启用',
  236. "materielNum" => getArrayItem($params, 'materielNum', 0),
  237. "goodsIds" => getArrayItem($params,'goodsIds',''),
  238. ];
  239. }
  240. /**
  241. * Doc: (des="")
  242. * User: XMing
  243. * Date: 2021/1/21
  244. * Time: 6:41 下午
  245. * @param int $id
  246. * @return ResultWrapper
  247. * @throws \Exception
  248. */
  249. public function get(int $id): ResultWrapper
  250. {
  251. $package = $this->objDComBinPackage->get($id);
  252. if ($package === false) {
  253. return ResultWrapper::fail($this->objDComBinPackage->error(), ErrorCode::$dberror);
  254. }
  255. if (empty($package)) {
  256. return ResultWrapper::fail('未获取到指定信息', ErrorCode::$paramError);
  257. }
  258. $formatResult = self::formatPackage($package);
  259. if (!$formatResult->isSuccess()) {
  260. return ResultWrapper::fail($formatResult->getData(), $formatResult->getErrorCode());
  261. }
  262. return ResultWrapper::success($formatResult->getData());
  263. }
  264. /**
  265. * Doc: (des="格式化套餐详情")
  266. * User: XMing
  267. * Date: 2021/1/21
  268. * Time: 6:47 下午
  269. * @param array $data
  270. * @return ResultWrapper
  271. * @throws \Exception
  272. */
  273. private function formatPackage(array $data): ResultWrapper
  274. {
  275. if (empty($data)) {
  276. return ResultWrapper::success([]);
  277. }
  278. $data['allowInventory'] = bcsub($data['inventory'], $data['saleNum'], 2);
  279. $goodsData = json_decode(getArrayItem($data, 'goodsData', []),true);
  280. if (empty($goodsData)) {
  281. return ResultWrapper::fail('产品数据为空', ErrorCode::$paramError);
  282. }
  283. //获取商品详情
  284. $goodsIds = [];
  285. foreach ($goodsData as $datum){
  286. if (!in_array($datum['goodsId'],$goodsIds)){
  287. $goodsIds[] = $datum['goodsId'];
  288. }
  289. }
  290. unset($datum);
  291. $objMGoods = new MGoods($this->onlineEnterpriseId,false,$this->onlineUserId);
  292. $materielResult = $objMGoods->getGoodsByGoodsIds($goodsIds,$data['shopId']);
  293. if (!$materielResult->isSuccess()){
  294. return ResultWrapper::fail($materielResult->getData(),$materielResult->getErrorCode());
  295. }
  296. $goodsMaps = $materielResult->getData();
  297. foreach ($goodsData as &$datum){
  298. $datum['invalid'] = StatusCode::$standard;
  299. $datum['invalidMsg'] = '';
  300. if (!isset($goodsMaps[$datum['goodsId']])){
  301. $datum['invalid'] = StatusCode::$delete;
  302. $datum['invalidMsg'] = '商品已删除';
  303. continue;
  304. }
  305. $row = $goodsMaps[$datum['goodsId']];
  306. if ($row['enableStatus'] == StatusCode::$delete){
  307. $datum['invalid'] = StatusCode::$delete;
  308. $datum['invalidMsg'] = '商品已下架';
  309. continue;
  310. }
  311. $datum['title'] = getArrayItem($row,'title','');
  312. $datum['images'] = getArrayItem($row,'images',[]);
  313. $datum['code'] = createCode(StatusCode::$code['goodsBasic']['prefix'], $row['materielId'], StatusCode::$code['goodsBasic']['length']);
  314. }
  315. unset($datum);
  316. $data['goodsData'] = $goodsData;
  317. return ResultWrapper::success($data);
  318. }
  319. /**
  320. * Doc: (des="")
  321. * User: XMing
  322. * Date: 2021/1/21
  323. * Time: 6:41 下午
  324. * @param int $id
  325. * @return ResultWrapper
  326. */
  327. public function enable(int $id): ResultWrapper
  328. {
  329. $package = $this->objDComBinPackage->get($id);
  330. if ($package === false) {
  331. return ResultWrapper::fail($this->objDComBinPackage->error(), ErrorCode::$dberror);
  332. }
  333. if (empty($package)) {
  334. return ResultWrapper::fail('未获取到指定信息', ErrorCode::$paramError);
  335. }
  336. $update = [
  337. 'enableStatus' => $package['enableStatus'] == StatusCode::$standard ? StatusCode::$delete : StatusCode::$standard,
  338. 'updateTime' => time(),
  339. ];
  340. $result = $this->objDComBinPackage->update($update, $id);
  341. if ($result === false) {
  342. return ResultWrapper::fail($this->objDComBinPackage->error(), ErrorCode::$dberror);
  343. }
  344. return ResultWrapper::success(true);
  345. }
  346. /**
  347. * Doc: (des="")
  348. * User: XMing
  349. * Date: 2021/1/22
  350. * Time: 10:13 上午
  351. * @param array $data
  352. * @param int $id
  353. * @return ResultWrapper
  354. */
  355. public function edit(array $data, int $id): ResultWrapper
  356. {
  357. $update = self::buildPackageData($data);
  358. $update['updateTime'] = time();
  359. $result = $this->objDComBinPackage->update($update, $id);
  360. if ($result === false) {
  361. return ResultWrapper::fail($this->objDComBinPackage->error(), ErrorCode::$dberror);
  362. }
  363. return ResultWrapper::success(true);
  364. }
  365. /**
  366. * Doc: (des="")
  367. * User: XMing
  368. * Date: 2021/1/22
  369. * Time: 10:30 上午
  370. * @param array $params
  371. * @return ResultWrapper
  372. * @throws \Exception
  373. */
  374. public function lists(array $params): ResultWrapper
  375. {
  376. $params['state'] = 2;
  377. $params['enableStatus'] = StatusCode::$standard;
  378. $buildSql = self::buildSqlBySelectParams($params);
  379. $lists = $this->objDComBinPackage->query($buildSql['sql']);
  380. if ($lists === false){
  381. return ResultWrapper::fail($this->objDComBinPackage->error(),ErrorCode::$dberror);
  382. }
  383. $count = $this->objDComBinPackage->query($buildSql['countSql']);
  384. if ($count === false){
  385. return ResultWrapper::fail($this->objDComBinPackage->error(),ErrorCode::$dberror);
  386. }
  387. $formatListsResult = self::formatListsApi($lists);
  388. if (!$formatListsResult->isSuccess()){
  389. return ResultWrapper::fail($formatListsResult->getData(),$formatListsResult->getErrorCode());
  390. }
  391. $ret = [
  392. 'data' => $formatListsResult->getData(),
  393. 'total' => isset($count[0]['total']) ? $count[0]['total'] : 0,
  394. ];
  395. return ResultWrapper::success($ret);
  396. }
  397. /**
  398. * Doc: (des="")
  399. * User: XMing
  400. * Date: 2021/1/22
  401. * Time: 10:43 上午
  402. * @param array $lists
  403. * @return ResultWrapper
  404. * @throws \Exception
  405. */
  406. private function formatListsApi(array $lists): ResultWrapper
  407. {
  408. if (empty($lists)){
  409. return ResultWrapper::success([]);
  410. }
  411. $objMGoods = new MGoods($this->onlineEnterpriseId,false,$this->onlineUserId);
  412. foreach ($lists as &$list){
  413. $list['allowInventory'] = bcsub($list['inventory'], $list['saleNum'], 2);
  414. $goodsData = json_decode($list['goodsData'],true);
  415. $goodsIds = [];
  416. foreach ($goodsData as $datum){
  417. if (!in_array($datum['goodsId'],$goodsIds)){
  418. $goodsIds[] = $datum['goodsId'];
  419. }
  420. }
  421. unset($datum);
  422. $materielResult = $objMGoods->getGoodsByGoodsIds($goodsIds,$list['shopId']);
  423. if (!$materielResult->isSuccess()){
  424. return ResultWrapper::fail($materielResult->getData(),$materielResult->getErrorCode());
  425. }
  426. $goodsMaps = $materielResult->getData();
  427. foreach ($goodsData as &$datum){
  428. $datum['invalid'] = StatusCode::$standard;
  429. $datum['invalidMsg'] = '';
  430. if (!isset($goodsMaps[$datum['goodsId']])){
  431. $datum['invalid'] = StatusCode::$delete;
  432. $datum['invalidMsg'] = '商品已删除';
  433. continue;
  434. }
  435. $row = $goodsMaps[$datum['goodsId']];
  436. if ($row['enableStatus'] == StatusCode::$delete){
  437. $datum['invalid'] = StatusCode::$delete;
  438. $datum['invalidMsg'] = '商品已下架';
  439. continue;
  440. }
  441. $datum['title'] = getArrayItem($row,'title','');
  442. $datum['images'] = getArrayItem($row,'images',[]);
  443. $datum['code'] = createCode(StatusCode::$code['goodsBasic']['prefix'], $datum['materielId'], StatusCode::$code['goodsBasic']['length']);
  444. }
  445. unset($datum);
  446. $list['goodsData'] = $goodsData;
  447. }
  448. return ResultWrapper::success($lists);
  449. }
  450. /**
  451. * Doc: (des="")
  452. * User: XMing
  453. * Date: 2021/1/22
  454. * Time: 1:00 下午
  455. * @param int $id
  456. * @return ResultWrapper
  457. * @throws \Exception
  458. */
  459. public function details(int $id): ResultWrapper
  460. {
  461. $package = $this->objDComBinPackage->get($id);
  462. if ($package === false) {
  463. return ResultWrapper::fail($this->objDComBinPackage->error(), ErrorCode::$dberror);
  464. }
  465. if (empty($package)) {
  466. return ResultWrapper::fail('未获取到指定信息', ErrorCode::$paramError);
  467. }
  468. $formatDetailsResult = self::formatDetails($package);
  469. if (!$formatDetailsResult->isSuccess()){
  470. return ResultWrapper::fail($formatDetailsResult->getData(),$formatDetailsResult->getErrorCode());
  471. }
  472. $formatDetails = $formatDetailsResult->getData();
  473. return ResultWrapper::success($formatDetails);
  474. }
  475. /**
  476. * Doc: (des="组合套餐详情")
  477. * User: XMing
  478. * Date: 2021/1/22
  479. * Time: 2:35 下午
  480. * @param array $data
  481. * @return ResultWrapper
  482. * @throws \Exception
  483. */
  484. public function formatDetails(array $data): ResultWrapper
  485. {
  486. if (empty($data)){
  487. return ResultWrapper::success($data);
  488. }
  489. $goodsData = json_decode($data['goodsData'],true);
  490. if (empty($goodsData)){
  491. return ResultWrapper::fail('组合套餐配置异常',ErrorCode::$paramError);
  492. }
  493. $data['invalid'] = StatusCode::$standard;//活动有效状态
  494. $data['invalidMsg'] = '';//状态说明
  495. $data['allowInventory'] = floatval(bcsub($data['inventory'], $data['saleNum'], 2));
  496. if ($data['allowInventory'] < 0){
  497. $data['invalid'] = StatusCode::$delete;
  498. $data['invalidMsg'] = '抢光了~';
  499. }
  500. if ($data['enableStatus'] == StatusCode::$delete){
  501. $data['invalid'] = StatusCode::$delete;
  502. $data['invalidMsg'] = '下架了~';
  503. }
  504. $objMGoods = new MGoods($this->onlineEnterpriseId,true,$this->onlineUserId);
  505. foreach ($goodsData as &$list){
  506. $list['invalid'] = StatusCode::$standard;
  507. $list['invalidMsg'] = '';
  508. //获取商品的详情
  509. $goodsResult = $objMGoods->getApiGoodsInfo($list['goodsId']);
  510. if (!$goodsResult->isSuccess()){
  511. return ResultWrapper::fail($goodsResult->getData(),$goodsResult->getErrorCode());
  512. }
  513. $goods = $goodsResult->getData();
  514. if (empty($goods)){
  515. $data['invalid'] = StatusCode::$delete;
  516. $data['invalidMsg'] = '产品删除了~';
  517. $list['invalid'] = StatusCode::$delete;
  518. $list['invalidMsg'] = '商品已删除';
  519. continue;
  520. }
  521. unset($goods['skuData']);
  522. if ($goods['enableStatus'] == StatusCode::$delete){
  523. $data['invalid'] = StatusCode::$delete;
  524. $data['invalidMsg'] = '产品下架了~';
  525. $list['invalid'] = StatusCode::$delete;
  526. $list['invalidMsg'] = '商品已下架';
  527. continue;
  528. }
  529. if ($this->preSale == StatusCode::$delete){
  530. //需要校验库存
  531. if(floatval($goods['inventorTotal']) < 0){
  532. $data['invalid'] = StatusCode::$delete;
  533. $data['invalidMsg'] = '产品售馨了~';
  534. $list['invalid'] = StatusCode::$delete;
  535. $list['invalidMsg'] = '商品已售馨';
  536. continue;
  537. }
  538. }
  539. $list = array_merge($list,$goods);
  540. }
  541. unset($list);
  542. $data['goodsData'] = $goodsData;
  543. return ResultWrapper::success($data);
  544. }
  545. /**
  546. * Doc: (des="增加套餐购买数量")
  547. * User: XMing
  548. * Date: 2021/1/22
  549. * Time: 9:47 下午
  550. * @param int $id
  551. * @param array $params
  552. * @param array $orderMap
  553. * @return ResultWrapper
  554. */
  555. public function incPackNum(int $id,array $params,array $orderMap): ResultWrapper
  556. {
  557. if (!isset($params['userCenterId'])){
  558. return ResultWrapper::fail('userCenterId参数错误',ErrorCode::$paramError);
  559. }
  560. $userId = $params['userCenterId'];
  561. $package = $this->objDComBinPackage->get($id);
  562. if ($package === false){
  563. return ResultWrapper::fail($this->objDComBinPackage->error(),ErrorCode::$dberror);
  564. }
  565. if (empty($package)){
  566. return ResultWrapper::fail('套餐已下架',ErrorCode::$paramError);
  567. }
  568. if ($package['isLimit'] == StatusCode::$standard){
  569. //开启了每人限制
  570. $limitNum = $package['limitNum'];
  571. $logNum = $this->objDComBinLog->count(['userCenterId' => $userId,'comBinId' => $id]);
  572. if ($logNum === false){
  573. return ResultWrapper::fail($this->objDComBinLog->error(),ErrorCode::$dberror);
  574. }
  575. if ($logNum >= $limitNum){
  576. return ResultWrapper::fail('你已经购买过此套餐了',ErrorCode::$paramError);
  577. }
  578. }
  579. $inventory = $package['inventory'];
  580. $oldSaleNum = $package['saleNum'];
  581. $oldSubOrderNum = $package['subOrderNum'];
  582. $oldSubOrderPayMoney = $package['subOrderPayMoney'];
  583. if ($oldSaleNum >= $inventory){
  584. return ResultWrapper::fail('套餐抢光了',ErrorCode::$paramError);
  585. }
  586. $update = [
  587. 'saleNum' => bcadd($oldSaleNum,1,2),
  588. 'subOrderNum' => bcadd($oldSubOrderNum,1,2),
  589. 'subOrderPayMoney' => bcadd($oldSubOrderPayMoney,$package['price'])
  590. ];
  591. $incResult = $this->objDComBinPackage->update($update,$id);
  592. if ($incResult === false){
  593. return ResultWrapper::fail($this->objDComBinPackage->error(),ErrorCode::$dberror);
  594. }
  595. $log = [];
  596. foreach ($orderMap as $no => $orderId){
  597. $log = [
  598. 'sourceId' => $orderId,
  599. 'sourceNo' => $no,
  600. 'comBinId' => $id,
  601. 'customerId' => $params['id'],
  602. 'customerName' => $params['name'],
  603. 'userCenterId' => $params['userCenterId'],
  604. 'buyNum' => 1,
  605. 'createTime' => time(),
  606. 'updateTime' => time()
  607. ];
  608. }
  609. if (!empty($log)){
  610. $result = $this->objDComBinLog->insert($log);
  611. if ($result === false){
  612. return ResultWrapper::fail($this->objDComBinLog->error(),ErrorCode::$dberror);
  613. }
  614. }
  615. return ResultWrapper::success(true);
  616. }
  617. }