MIntegralGoods.Class.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. <?php
  2. /**
  3. * 积分商品管理Model
  4. * Created by PhpStorm.
  5. * User: haoren
  6. * Date: 2021/03/25
  7. * Time: 16:00
  8. */
  9. namespace JinDouYun\Model\Integral;
  10. use Exception;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\StatusCode;
  13. use Mall\Framework\Core\ResultWrapper;
  14. use JinDouYun\Dao\Integral\DIntegralGoods;
  15. use JinDouYun\Dao\Integral\DIntegralGoodsExchange;
  16. use JinDouYun\Dao\Integral\DIntegralRule;
  17. use JinDouYun\Dao\Customer\DCustomerIntegralDesc;
  18. use JinDouYun\Dao\UserCenter\DUserCenter;
  19. use JinDouYun\Model\MBaseModel;
  20. use JinDouYun\Model\Customer\MCustomer;
  21. class MIntegralGoods extends MBaseModel
  22. {
  23. private $objDIntegralGoods;
  24. private $objDIntegralGoodsExchange;
  25. private $objDIntegralRule;
  26. private $userCenterId;
  27. private $enterpriseId;
  28. public function __construct($enterpriseId, $userCenterId = false)
  29. {
  30. $this->enterpriseId = $enterpriseId;
  31. $this->userCenterId = $userCenterId;
  32. $this->objDIntegralGoods = new DIntegralGoods('default');
  33. $this->objDIntegralGoodsExchange = new DIntegralGoodsExchange('default');
  34. $this->objDIntegralRule = new DIntegralRule('default');
  35. $this->objDIntegralGoods->setTable('qianniao_integral_goods_' . $enterpriseId);
  36. $this->objDIntegralGoodsExchange->setTable('qianniao_integral_goods_exchange_' . $enterpriseId);
  37. $this->objDIntegralRule->setTable('qianniao_integral_rule_' . $enterpriseId);
  38. }
  39. /**
  40. * 积分商品添加
  41. * @param $params
  42. * @return ResultWrapper
  43. * @throws Exception
  44. */
  45. public function addIntegralGoods($params)
  46. {
  47. $params['deleteStatus'] = StatusCode::$standard;
  48. $params['enableStatus'] = StatusCode::$standard;
  49. $params['createTime'] = time();
  50. $params['updateTime'] = time();
  51. $dbResult = $this->objDIntegralGoods->insert($params);
  52. if ($dbResult === false) {
  53. return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
  54. }
  55. return ResultWrapper::success($dbResult);
  56. }
  57. /**
  58. * 积分商品删除
  59. * @param $where
  60. * @return ResultWrapper
  61. */
  62. public function deleteIntegralGoods($where)
  63. {
  64. $params = [
  65. 'deleteStatus' => StatusCode::$delete,
  66. 'updateTime' => time(),
  67. ];
  68. $dbResult = $this->objDIntegralGoods->update($params, $where);
  69. if ($dbResult === false) {
  70. return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
  71. }
  72. $returnData = $dbResult;
  73. return ResultWrapper::success($returnData);
  74. }
  75. /**
  76. * 积分商品禁用
  77. * @param $where
  78. * @return ResultWrapper
  79. */
  80. public function enableIntegralGoods($where)
  81. {
  82. $dbResult = $this->objDIntegralGoods->get($where);
  83. if ($dbResult === false) {
  84. return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
  85. }
  86. $update = [
  87. 'enableStatus' => StatusCode::$standard,
  88. 'updateTime' => time(),
  89. ];
  90. if($dbResult['enableStatus'] == StatusCode::$standard){
  91. $update['enableStatus'] = StatusCode::$delete;
  92. }
  93. $dbResult = $this->objDIntegralGoods->update($update, $where);
  94. if ($dbResult === false) {
  95. return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
  96. }
  97. $returnData = $dbResult;
  98. return ResultWrapper::success($returnData);
  99. }
  100. /**
  101. * 积分商品修改
  102. * @param $updateData
  103. * @param $where
  104. * @return ResultWrapper
  105. */
  106. public function updateIntegralGoods($updateData, $where)
  107. {
  108. $dbResult = $this->objDIntegralGoods->get($where);
  109. if($dbResult === false){
  110. return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$paramError);
  111. }
  112. $goods = $dbResult;
  113. unset($dbResult);
  114. $dbResult = $this->objDIntegralGoods->update($updateData, $where);
  115. if ($dbResult === false) {
  116. return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
  117. }
  118. return ResultWrapper::success($dbResult);
  119. }
  120. /**
  121. * 积分商品列表
  122. * @param $selectParams
  123. * @return ResultWrapper
  124. * @throws Exception
  125. */
  126. public function getAllIntegralGoods($selectParams)
  127. {
  128. $limit = $selectParams['limit'];
  129. unset($selectParams['limit']);
  130. $offset = $selectParams['offset'];
  131. unset($selectParams['offset']);
  132. $selectParams['deleteStatus'] = StatusCode::$standard;
  133. isset($selectParams['search']) && $selectParams['search'] = ['name' => $selectParams['search']];
  134. $selectParams = self::formatSqlWhere($selectParams);
  135. $dbResult = $this->objDIntegralGoods->select($selectParams,'*', 'sort desc,createTime desc', $limit, $offset);
  136. if ($dbResult === false) {
  137. return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
  138. }
  139. $countResult = $this->objDIntegralGoods->count($selectParams);
  140. if ($countResult === false) {
  141. return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
  142. }
  143. $return = [
  144. 'data' => self::formatIntegralGoods($dbResult),
  145. 'total' => ($countResult) ? intval($countResult) : 0,
  146. ];
  147. return ResultWrapper::success($return);
  148. }
  149. /**
  150. * 积分商品详情
  151. * @param $where
  152. * @return ResultWrapper
  153. * @throws Exception
  154. */
  155. public function getIntegralGoodsInfo($where)
  156. {
  157. $where['deleteStatus'] = StatusCode::$standard;
  158. $dbResult = $this->objDIntegralGoods->get($where);
  159. if ($dbResult === false) {
  160. return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
  161. }
  162. return ResultWrapper::success(self::formatIntegralGoods($dbResult));
  163. }
  164. /**
  165. * 积分商品格式化
  166. * @param $params
  167. * @return array
  168. */
  169. public function formatIntegralGoods($params)
  170. {
  171. if(isset($params['id'])){
  172. $data = [$params];
  173. }else{
  174. $data = $params;
  175. }
  176. foreach($data as &$value){
  177. $value['images'] = json_decode($value['images'], true);
  178. $value['categoryName'] = $value['category'] == StatusCode::$delete ? '虚拟商品' : '实物商品';
  179. $value['details'] = htmlspecialchars_decode($value['details'], ENT_QUOTES);
  180. }
  181. unset($value);
  182. if(isset($params['id'])){
  183. $return = array_shift($data);
  184. }else{
  185. $return = $data;
  186. }
  187. return $return;
  188. }
  189. /**
  190. * 查询积分商品数据
  191. * @param $where
  192. * @return ResultWrapper
  193. */
  194. public function getIntegralGoods($where)
  195. {
  196. $where['deleteStatus'] = StatusCode::$standard;
  197. $dbResult = $this->objDIntegralGoods->get($where);
  198. if ($dbResult === false) {
  199. return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
  200. }
  201. return ResultWrapper::success($dbResult);
  202. }
  203. /**
  204. * 查询积分商品数据
  205. * @param $where
  206. * @return ResultWrapper
  207. */
  208. public function selectIntegralGoods($where)
  209. {
  210. $where['deleteStatus'] = StatusCode::$standard;
  211. $dbResult = $this->objDIntegralGoods->select($where);
  212. if ($dbResult === false) {
  213. return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
  214. }
  215. return ResultWrapper::success($dbResult);
  216. }
  217. /**
  218. * 积分商品兑换添加
  219. * @param $params
  220. * @return ResultWrapper
  221. */
  222. public function addIntegralGoodsExchange($params)
  223. {
  224. //查询商品
  225. $dbResult = $this->objDIntegralGoods->get(['id' => $params['goodsId'], 'deleteStatus' => StatusCode::$standard]);
  226. if($dbResult === false){
  227. return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
  228. }
  229. $goods = $dbResult;
  230. unset($dbResult);
  231. if(empty($goods)){
  232. return ResultWrapper::fail('商品不存在', ErrorCode::$paramError);
  233. }
  234. if($goods['enableStatus'] == StatusCode::$delete){
  235. return ResultWrapper::fail('商品已下架', ErrorCode::$paramError);
  236. }
  237. /* if($params['num'] > $goods['num']){
  238. return ResultWrapper::fail('商品可兑换数量不足', ErrorCode::$paramError);
  239. }*/
  240. if($params['num'] > $goods['limit']){
  241. return ResultWrapper::fail('每位用户限购'.$goods['limit'], ErrorCode::$paramError);
  242. }
  243. $params['integral'] = $goods['integral'];
  244. $params['amount'] = bcmul($params['num'], $params['integral'], 2);
  245. //查询客户
  246. $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId);
  247. $where = [
  248. 'userCenterId' => isset($params['userCenterId']) ? $params['userCenterId'] : $this->userCenterId,
  249. 'deleteStatus' => StatusCode::$standard
  250. ];
  251. $modelResult = $objMCustomer->getCustomerData($where);
  252. if(!$modelResult->isSuccess()){
  253. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  254. }
  255. $customer = $modelResult->getData();
  256. unset($modelResult);
  257. if(empty($customer)){
  258. return ResultWrapper::fail('用户不存在', ErrorCode::$paramError);
  259. }
  260. $whereGoodsExchange = [
  261. 'customerId' => $customer['id'],
  262. 'goodsId' => $goods['id'],
  263. 'shopId' => $params['shopId'],
  264. 'status' => [4,5]
  265. ];
  266. $sql = 'select sum(num) as num from qianniao_integral_goods_exchange_'.$this->enterpriseId.' where '.self::formatSqlWhere($whereGoodsExchange);
  267. $dbResult = $this->objDIntegralGoodsExchange->query($sql);
  268. if($dbResult === false){
  269. return ResultWrapper::fail($this->objDIntegralGoodsExchange->error(), ErrorCode::$dberror);
  270. }
  271. $exchangeNum = $dbResult[0]['num'];
  272. unset($dbResult);
  273. if(bcadd($exchangeNum, $params['num']) > $goods['limit']){
  274. return ResultWrapper::fail('每位用户限购'.$goods['limit'], ErrorCode::$paramError);
  275. }
  276. if($customer['integral'] < $params['amount']){
  277. return ResultWrapper::fail('积分不足', ErrorCode::$paramError);
  278. }
  279. $beginStatus = $this->objDIntegralGoodsExchange->beginTransaction();
  280. //扣除积分
  281. $updateCustomer = [
  282. 'integral' => bcsub($customer['integral'], $params['amount'], 2),
  283. 'updateTime' => time()
  284. ];
  285. $modelResult = $objMCustomer->updateCustomer($updateCustomer, ['id' => $customer['id']]);
  286. if(!$modelResult->isSuccess()){
  287. $this->objDIntegralGoodsExchange->rollBack();
  288. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  289. }
  290. unset($modelResult);
  291. $params['status'] = StatusCode::$delete;
  292. $params['no'] = createOrderSn(StatusCode::$source['manage'], 99, $params['userCenterId']);
  293. $params['createTime'] = time();
  294. $params['updateTime'] = time();
  295. $params['customerId'] = $customer['id'];
  296. $dbResult = $this->objDIntegralGoodsExchange->insert($params);
  297. if($dbResult === false){
  298. $this->objDIntegralGoodsExchange->rollBack();
  299. return ResultWrapper::fail($this->objDIntegralGoodsExchange->error(), ErrorCode::$dberror);
  300. }
  301. $exchangeId = $dbResult;
  302. unset($dbResult);
  303. //增加记录
  304. $insertIntegralDesc = [
  305. 'userCenterId' => $customer['userCenterId'],
  306. 'customerId' => $customer['id'],
  307. 'shopId' => isset($params['shopId']) ? $params['shopId'] : 0,
  308. 'originId' => $exchangeId,
  309. 'originNo' => $params['no'],
  310. 'title' => '积分兑换商品',
  311. 'amount' => $params['amount'],
  312. 'changeAmount' => $updateCustomer['integral'],
  313. 'type' => StatusCode::$delete,
  314. 'source' => 4,
  315. 'createTime' => time(),
  316. 'updateTime' => time(),
  317. ];
  318. $objDCustomerIntegralDesc = new DCustomerIntegralDesc();
  319. $objDCustomerIntegralDesc->set_Table('qianniao_customer_integral_desc_'.$this->enterpriseId);
  320. $dbResult = $objDCustomerIntegralDesc->insert($insertIntegralDesc);
  321. if($dbResult === false){
  322. $this->objDIntegralGoodsExchange->rollBack();
  323. return ResultWrapper::fail($objDCustomerIntegralDesc->error(), ErrorCode::$dberror);
  324. }
  325. //修改商品
  326. $updateGoods = [
  327. 'num' => bcsub($goods['num'], $params['num']),
  328. 'changeNum' => bcadd($goods['changeNum'], $params['num']),
  329. 'updateTime' => time()
  330. ];
  331. $dbResult = $this->objDIntegralGoods->update($updateGoods, ['id' => $goods['id']]);
  332. if($dbResult === false){
  333. return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
  334. }
  335. $beginStatus && $this->objDIntegralGoodsExchange->commit();
  336. return ResultWrapper::success($exchangeId);
  337. }
  338. /**
  339. * 积分商品兑换修改
  340. * @param $update
  341. * @param $where
  342. * @return ResultWrapper
  343. */
  344. public function updateIntegralGoodsExchange($update, $where)
  345. {
  346. $dbResult = $this->objDIntegralGoodsExchange->get($where);
  347. if($dbResult === false){
  348. return ResultWrapper::fail($this->objDIntegralGoodsExchange->error(), ErrorCode::$dberror);
  349. }
  350. $exchange = $dbResult;
  351. unset($dbResult);
  352. if(isset($update['status']) && $update['status'] == 6 && $exchange['status'] != 4){
  353. return ResultWrapper::fail('订单已完成', ErrorCode::$paramError);
  354. }
  355. if(isset($update['status']) && $update['status'] == 5 && $exchange['status'] != 4){
  356. return ResultWrapper::fail('订单已失效', ErrorCode::$paramError);
  357. }
  358. if($update['status'] == 6){
  359. //返还积分
  360. $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId);
  361. $modelResult = $objMCustomer->getCustomerData(['id' => $exchange['customerId']]);
  362. if(!$modelResult->isSuccess()){
  363. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  364. }
  365. $customer = $modelResult->getData();
  366. unset($modelResult);
  367. $updateCustomer = [
  368. 'integral' => bcadd($customer['integral'], $exchange['amount'], 2),
  369. 'updateTime' => time(),
  370. ];
  371. $modelResult = $objMCustomer->updateCustomer($updateCustomer, ['id' => $customer['id']]);
  372. if(!$modelResult->isSuccess()){
  373. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  374. }
  375. //增加记录
  376. $insertIntegralDesc = [
  377. 'userCenterId' => $customer['userCenterId'],
  378. 'customerId' => $customer['id'],
  379. 'shopId' => isset($exchange['shopId']) ? $exchange['shopId'] : 0,
  380. 'originId' => $exchange['id'],
  381. 'originNo' => $exchange['no'],
  382. 'title' => '积分兑换商品失效,返还积分',
  383. 'amount' => $exchange['amount'],
  384. 'changeAmount' => $updateCustomer['integral'],
  385. 'type' => StatusCode::$standard,
  386. 'source' => 5,
  387. 'createTime' => time(),
  388. 'updateTime' => time(),
  389. ];
  390. $objDCustomerIntegralDesc = new DCustomerIntegralDesc();
  391. $objDCustomerIntegralDesc->set_Table('qianniao_customer_integral_desc_'.$this->enterpriseId);
  392. $dbResult = $objDCustomerIntegralDesc->insert($insertIntegralDesc);
  393. if($dbResult === false){
  394. return ResultWrapper::fail($objDCustomerIntegralDesc->error(), ErrorCode::$dberror);
  395. }
  396. }
  397. $update['updateTime'] = time();
  398. $dbResult = $this->objDIntegralGoodsExchange->update($update,$where);
  399. if($dbResult === false){
  400. return ResultWrapper::fail($this->objDIntegralGoodsExchange->error(), ErrorCode::$dberror);
  401. }
  402. return ResultWrapper::success($dbResult);
  403. }
  404. /**
  405. * 积分商品兑换列表
  406. * @return ResultWrapper
  407. */
  408. public function getAllIntegralGoodsExchange($params)
  409. {
  410. $sql = 'select e.*,g.name as integralGoodsName,g.category,g.images,c.name as customerName from qianniao_integral_goods_exchange_'.$this->enterpriseId.' e left join qianniao_integral_goods_'.$this->enterpriseId.' g on g.id = e.goodsId left join qianniao_customer_'.$this->enterpriseId.' c on c.id = e.customerId';
  411. $whereSql = '';
  412. if(isset($params['shopId'])){
  413. $whereSql .= (!empty($whereSql) ? ' and ' : '').' e.shopId = '.$params['shopId'];
  414. }
  415. if(isset($params['userCenterId'])){
  416. $whereSql .= (!empty($whereSql) ? ' and ' : '').' e.userCenterId = '.$params['userCenterId'];
  417. }
  418. if(isset($params['search'])){
  419. $whereSql .= (!empty($whereSql) ? ' and ' : '').' (c.name like "%'.$params['search'].'%" or g.name like "%'.$params['search'].'%")';
  420. }
  421. if(isset($params['status'])){
  422. $whereSql .= (!empty($whereSql) ? ' and ' : '').' e.status = '.$params['status'];
  423. }
  424. !empty($whereSql) && $whereSql = ' where '.$whereSql;
  425. $orderSql = ' order by e.createTime desc';
  426. $dbResult = $this->objDIntegralGoodsExchange->query($sql.$whereSql.$orderSql);
  427. if($dbResult === false){
  428. return ResultWrapper::fail($this->objDIntegralGoodsExchange->error(), ErrorCode::$dberror);
  429. }
  430. $data = $dbResult;
  431. unset($dbResult);
  432. $countSql = 'select count(*) as count from qianniao_integral_goods_exchange_'.$this->enterpriseId.' e left join qianniao_integral_goods_'.$this->enterpriseId.' g on g.id = e.goodsId left join qianniao_customer_'.$this->enterpriseId.' c on c.id = e.customerId';
  433. $dbResult = $this->objDIntegralGoodsExchange->query($countSql.$whereSql);
  434. if($dbResult === false){
  435. return ResultWrapper::fail($this->objDIntegralGoodsExchange->error(), ErrorCode::$dberror);
  436. }
  437. $count = $dbResult;
  438. unset($dbResult);
  439. $return = [
  440. 'data' => self::formatIntegralGoodsExchange($data),
  441. 'total' => isset($count[0]['count']) ? $count[0]['count'] : 0
  442. ];
  443. return ResultWrapper::success($return);
  444. }
  445. /**
  446. * 积分商品兑换详情
  447. * @param $where
  448. * @return ResultWrapper
  449. */
  450. public function getIntegralGoodsExchangeInfo($params)
  451. {
  452. $sql = 'select e.*,g.name as integralGoodsName,g.category,g.images,c.name as customerName from qianniao_integral_goods_exchange_'.$this->enterpriseId.' e left join qianniao_integral_goods_'.$this->enterpriseId.' g on g.id = e.goodsId left join qianniao_customer_'.$this->enterpriseId.' c on c.id = e.customerId';
  453. $whereSql = '';
  454. if(isset($params['id'])){
  455. $whereSql .= (!empty($whereSql) ? ' and ' : '').' e.id = '.$params['id'];
  456. }
  457. !empty($whereSql) && $whereSql = ' where '.$whereSql;
  458. $limitSql = ' limit 1';
  459. $dbResult = $this->objDIntegralGoodsExchange->query($sql.$whereSql.$limitSql);
  460. if($dbResult === false){
  461. return ResultWrapper::fail($this->objDIntegralGoodsExchange->error(), ErrorCode::$dberror);
  462. }
  463. $data = array_shift($dbResult);
  464. unset($dbResult);
  465. return ResultWrapper::success(self::formatIntegralGoodsExchange($data));
  466. }
  467. /**
  468. * 积分商品兑换格式化
  469. * @param $params
  470. * @return array|mixed
  471. */
  472. public function formatIntegralGoodsExchange($params)
  473. {
  474. if(isset($params['id'])){
  475. $data = [$params];
  476. }else{
  477. $data = $params;
  478. }
  479. $userCenterIds = array_column($data, 'userCenterId');
  480. $userCenterData = [];
  481. if(!empty($userCenterIds)){
  482. $objDUserCenter = new DUserCenter();
  483. $dbResult = $objDUserCenter->select(['id' => $userCenterIds]);
  484. if($dbResult){
  485. foreach($dbResult as $value){
  486. $userCenterData[$value['id']] = $value['mobile'];
  487. }
  488. }
  489. unset($dbResult);
  490. }
  491. foreach($data as &$value){
  492. $value['images'] = !empty($value['images']) ? json_decode($value['images'], true) : [];
  493. $value['categoryName'] = $value['category'] == StatusCode::$delete ? '虚拟商品' : '实物商品';
  494. $value['address'] = !empty($value['address']) ? json_decode($value['address'], true) : [];
  495. $value['mobile'] = isset($userCenterData[$value['userCenterId']]) ? $userCenterData[$value['userCenterId']] : '';
  496. }
  497. unset($value);
  498. if(isset($params['id'])){
  499. $return = array_shift($data);
  500. }else{
  501. $return = $data;
  502. }
  503. return $return;
  504. }
  505. /**
  506. * 积分规则添加
  507. * @param $params
  508. * @return ResultWrapper
  509. */
  510. public function addIntegralRule($params)
  511. {
  512. $dbResult = $this->objDIntegralRule->update(['enableStatus' => StatusCode::$delete, 'updateTime' => time()]);
  513. if($dbResult === false){
  514. return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
  515. }
  516. $params['deleteStatus'] = StatusCode::$standard;
  517. $dbResult = $this->objDIntegralRule->insert($params);
  518. if($dbResult === false){
  519. return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
  520. }
  521. return ResultWrapper::success($dbResult);
  522. }
  523. /**
  524. * 积分规则删除
  525. * @param $where
  526. * @return ResultWrapper
  527. */
  528. public function deleteIntegralRule($where)
  529. {
  530. $params = [
  531. 'deleteStatus' => StatusCode::$delete,
  532. 'updateTime' => time(),
  533. ];
  534. $dbResult = $this->objDIntegralRule->update($params, $where);
  535. if ($dbResult === false) {
  536. return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
  537. }
  538. $returnData = $dbResult;
  539. return ResultWrapper::success($returnData);
  540. }
  541. /**
  542. * 积分规则禁用
  543. * @param $where
  544. * @return ResultWrapper
  545. */
  546. public function enableIntegralRule($where)
  547. {
  548. $dbResult = $this->objDIntegralRule->get($where);
  549. if ($dbResult === false) {
  550. return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
  551. }
  552. $update = [
  553. 'enableStatus' => StatusCode::$standard,
  554. 'updateTime' => time(),
  555. ];
  556. if($dbResult['enableStatus'] == StatusCode::$standard){
  557. $update['enableStatus'] = StatusCode::$delete;
  558. }else{
  559. $result = $this->objDIntegralRule->update(['enableStatus' => StatusCode::$delete, 'updateTime' => time()],['enableStatus' => StatusCode::$standard]);
  560. if ($result === false) {
  561. return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
  562. }
  563. }
  564. $dbResult = $this->objDIntegralRule->update($update, $where);
  565. if ($dbResult === false) {
  566. return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
  567. }
  568. $returnData = $dbResult;
  569. return ResultWrapper::success($returnData);
  570. }
  571. /**
  572. * 积分规则修改
  573. * @param $updateData
  574. * @param $where
  575. * @return ResultWrapper
  576. */
  577. public function updateIntegralRule($updateData, $where)
  578. {
  579. $dbResult = $this->objDIntegralRule->get($where);
  580. if($dbResult === false){
  581. return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$paramError);
  582. }
  583. $rule = $dbResult;
  584. unset($dbResult);
  585. $dbResult = $this->objDIntegralRule->update($updateData, $where);
  586. if ($dbResult === false) {
  587. return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
  588. }
  589. return ResultWrapper::success($dbResult);
  590. }
  591. /**
  592. * 积分规则列表
  593. * @param $selectParams
  594. * @return ResultWrapper
  595. * @throws Exception
  596. */
  597. public function getAllIntegralRule($selectParams)
  598. {
  599. $limit = $selectParams['limit'];
  600. unset($selectParams['limit']);
  601. $offset = $selectParams['offset'];
  602. unset($selectParams['offset']);
  603. $selectParams['deleteStatus'] = StatusCode::$standard;
  604. isset($selectParams['search']) && $selectParams['search'] = ['title' => $selectParams['search']];
  605. $selectParams = self::formatSqlWhere($selectParams);
  606. $dbResult = $this->objDIntegralRule->select($selectParams,'*', 'createTime desc', $limit, $offset);
  607. if ($dbResult === false) {
  608. return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
  609. }
  610. $countResult = $this->objDIntegralRule->count($selectParams);
  611. if ($countResult === false) {
  612. return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
  613. }
  614. $return = [
  615. 'data' => self::formatIntegralRule($dbResult),
  616. 'total' => ($countResult) ? intval($countResult) : 0,
  617. ];
  618. return ResultWrapper::success($return);
  619. }
  620. /**
  621. * 积分规则详情
  622. * @param $where
  623. * @return ResultWrapper
  624. * @throws Exception
  625. */
  626. public function getIntegralRuleInfo($where)
  627. {
  628. $where['deleteStatus'] = StatusCode::$standard;
  629. $dbResult = $this->objDIntegralRule->get($where);
  630. if ($dbResult === false) {
  631. return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
  632. }
  633. return ResultWrapper::success(self::formatIntegralRule($dbResult));
  634. }
  635. /**
  636. * 积分规则格式化
  637. * @param $params
  638. * @return array
  639. */
  640. public function formatIntegralRule($params)
  641. {
  642. if(isset($params['id'])){
  643. $data = [$params];
  644. }else{
  645. $data = $params;
  646. }
  647. foreach($data as &$value){
  648. $value['goods'] = json_decode($value['goods'], true);
  649. }
  650. unset($value);
  651. if(isset($params['id'])){
  652. $return = array_shift($data);
  653. }else{
  654. $return = $data;
  655. }
  656. return $return;
  657. }
  658. /**
  659. * 查询积分规则数据
  660. * @param $where
  661. * @return ResultWrapper
  662. */
  663. public function getIntegralRule($where)
  664. {
  665. $where['deleteStatus'] = StatusCode::$standard;
  666. $dbResult = $this->objDIntegralRule->get($where);
  667. if ($dbResult === false) {
  668. return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
  669. }
  670. return ResultWrapper::success($dbResult);
  671. }
  672. /**
  673. * 查询积分规则数据
  674. * @param $where
  675. * @return ResultWrapper
  676. */
  677. public function selectIntegralRule($where)
  678. {
  679. $where['deleteStatus'] = StatusCode::$standard;
  680. $dbResult = $this->objDIntegralRule->select($where);
  681. if ($dbResult === false) {
  682. return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
  683. }
  684. return ResultWrapper::success($dbResult);
  685. }
  686. }