CouponModelService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace bizHd\promote\services;
  3. use bizHd\base\services\BaseService;
  4. use common\components\arrayUtil;
  5. use common\components\error;
  6. use common\components\util;
  7. use Yii;
  8. class CouponModelService extends BaseService
  9. {
  10. public static $baseFile = '\bizHd\promote\classes\CouponModelClass';
  11. //列出商家所有的卷模板
  12. public static function getCouponList($sjId)
  13. {
  14. $list = self::getAllList('*', ['sjId' => $sjId], 'level desc');
  15. return $list;
  16. }
  17. //按type、targetId作为键,列出商家所有的卷模板
  18. public static function getKeySortCouponList()
  19. {
  20. $list = self::getCouponList();
  21. if (empty($list)) {
  22. return [];
  23. }
  24. $data = [];
  25. foreach ($list as $val) {
  26. $key = $val['type'] . '_' . $val['targetId'];
  27. $data[$key][] = $val;
  28. }
  29. return $data;
  30. }
  31. //获取通用的优惠卷,用于首次、二次和三次消费时送卷 ssh 2019.9.18
  32. public static function getCommonCoupon()
  33. {
  34. $list = self::getCouponList();
  35. if (empty($list)) {
  36. return [];
  37. }
  38. $coupon = [];
  39. foreach ($list as $key => $val) {
  40. $type = $val['type'];
  41. $targetId = $val['targetId'];
  42. if ($type == 0) {
  43. if (in_array($targetId, [0, 1, 2])) {
  44. $coupon[$targetId] = $val;
  45. }
  46. }
  47. }
  48. return $coupon;
  49. }
  50. //根据分类和用途取可以得到的优惠卷,并且按优先级排序 ssh
  51. public static function getAvailableCouponListById($categoryId, $usageId)
  52. {
  53. $rightList = [];
  54. $couponList = self::getKeySortCouponList();
  55. /**找出分类卷**/
  56. $categoryKey = '1_' . $categoryId;
  57. if (isset($couponList[$categoryKey])) {
  58. $rightList = array_merge($rightList, $couponList[$categoryKey]);
  59. }
  60. /**找出用途卷**/
  61. $usageKey = '2_' . $usageId;
  62. if (isset($couponList[$usageKey])) {
  63. $rightList = array_merge($rightList, $couponList[$usageKey]);
  64. }
  65. /**找出通用卷 0_开头**/
  66. foreach ($couponList as $key => $val) {
  67. if (strstr($key, '0_')) {
  68. $rightList = array_merge($rightList, $val);
  69. }
  70. }
  71. if (empty($rightList)) {
  72. return [];
  73. }
  74. /**按优先级排序**/
  75. $rightList = arrayUtil::arraySort($rightList, 'level');
  76. return $rightList;
  77. }
  78. //添加或更新卷模板 ssh 2019.8.18
  79. public static function replaceModel($data, $sjId)
  80. {
  81. $valid = array_keys($data);
  82. $diff = array_diff(['type', 'targetId', 'amount', 'meetAmount', 'reward', 'name', 'id'], $valid);
  83. if (!empty($diff)) {
  84. error::instance()->setError('参数错误');
  85. return false;
  86. }
  87. $type = $data['type'];
  88. $id = $data['id'];
  89. unset($data['id']);
  90. $data['validTime'] = !empty($data['validTime']) && is_numeric($data['validTime']) ? $data['validTime'] : 10;
  91. if (!empty($id)) {
  92. self::updateById($id, $data);
  93. }
  94. $targetId = $data['targetId'];
  95. //分类和用途类优惠卷不能重复添加 ssh 2019.8.17
  96. if (in_array($type, [1, 2])) {
  97. $has = self::getByCondition(['sjId' => $sjId, 'type' => $type, 'targetId' => $targetId]);
  98. if (!empty($has)) {
  99. error::instance()->setError('已经添加过了');
  100. return false;
  101. }
  102. }
  103. $time = time();
  104. $createTime = date("Y-m-d H:i:s");
  105. $data['sjId'] = $sjId;
  106. $data['addTime'] = $time;
  107. $data['createTime'] = $createTime;
  108. self::add($data);
  109. return true;
  110. }
  111. //取出按商品分类设置的优惠券 ssh 2019.8.18
  112. public static function getCategoryCoupon($sjId)
  113. {
  114. return self::getAllByCondition(['sjId' => $sjId, 'type' => 1], null, '*', 'targetId');
  115. }
  116. //取出按用途设置的优惠券 ssh 2019.8.18
  117. public static function getUsageCoupon($sjId)
  118. {
  119. return self::getAllByCondition(['sjId' => $sjId, 'type' => 2], null, '*', 'targetId');
  120. }
  121. //取优惠卷信息并确认是否商家自己的
  122. public static function getCouponInfo($id, $sjId)
  123. {
  124. $info = self::getById($id);
  125. if (empty($info)) {
  126. util::fail('没有优惠卷信息');
  127. }
  128. if ($info['sjId'] != $sjId) {
  129. util::fail('你无法操作');
  130. }
  131. return $info;
  132. }
  133. }