Award.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Award.php UTF-8
  4. * 奖品
  5. *
  6. * @date : 2018/5/10 22:49
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\controller\shop;
  13. use huo\controller\common\Base;
  14. use huo\controller\member\MemCache;
  15. use huo\model\integral\AwardsModel;
  16. use huolib\constant\MemItgConst;
  17. use huolib\status\ShopStatus;
  18. use huolib\tool\Rand;
  19. use think\Cache;
  20. class Award extends Base {
  21. /**
  22. * 实例化
  23. *
  24. * @return static
  25. */
  26. public static function ins() {
  27. return new static();
  28. }
  29. /**
  30. * @param int $mem_id
  31. * @param int $act_id
  32. * @param int $cost_integral
  33. *
  34. * @return array
  35. */
  36. public function draw($mem_id, $act_id = 0, $cost_integral = 0) {
  37. $_award_datas = $this->getAwards($act_id);
  38. $_arr = [];
  39. foreach ($_award_datas as $_k => $_v) {
  40. $_arr[$_k] = $_v['rate'];
  41. }
  42. $_key = Rand::getRand($_arr);
  43. $_award_data = $_award_datas[$_key];
  44. $_goods_id = $_award_data['goods_id'];
  45. $_cost_integral = $cost_integral;
  46. $_rs = (new ItgOrder())->drawExchange($mem_id, $_goods_id, [], $act_id, $cost_integral);
  47. if (ShopStatus::NO_ERROR != $_rs['code']) {
  48. return $this->huoError($_rs['code'], $_rs['msg']);
  49. }
  50. $_goods_data = GoodsCache::ins()->getInfoByGoodsId($_goods_id);
  51. if (empty($_cost_integral)) {
  52. $_cost_integral = $_goods_data['integral'];
  53. }
  54. $_me_data = MemCache::ins()->getMeInfoById($mem_id);
  55. $_rdata['my_integral'] = $_me_data['my_integral'];
  56. $_rdata['cost_integral'] = $_cost_integral;
  57. $_rdata['award_id'] = $_award_data['id'];
  58. $_rdata['award_name'] = $_award_data['award_name'];
  59. $_rdata['has_award'] = $_goods_data['gain_integral'] < 0.01 ? MemItgConst::AWARD_NO_GOODS
  60. : MemItgConst::AWARD_HAS_GOODS;
  61. $_code = ShopStatus::NO_ERROR;
  62. return $this->huoSuccess($_code, ShopStatus::getMsg($_code), $_rdata);
  63. }
  64. /**
  65. * 获取活动奖品KEY
  66. *
  67. * @param int $act_id 活动ID
  68. *
  69. * @return string
  70. */
  71. public function getAwardsKey($act_id) {
  72. return 'act_awards_'.$act_id;
  73. }
  74. /**
  75. * 获取活动奖品信息
  76. *
  77. * @param $act_id
  78. *
  79. * @return array|bool|mixed
  80. */
  81. public function getAwards($act_id = 0) {
  82. $_key = $this->getAwardsKey($act_id);
  83. $_awards_data_json = Cache::get($_key);
  84. $_awards_data = json_decode($_awards_data_json, true);
  85. if (!is_array($_awards_data)) {
  86. $_awards_data = $_awards_data_json;
  87. }
  88. if (!is_array($_awards_data) || empty($_awards_data)) {
  89. $_awards_data = (new AwardsModel())->getAwards($act_id);
  90. if (empty($_awards_data)) {
  91. return false;
  92. }
  93. $this->saveAwardsCache($act_id, $_awards_data);
  94. }
  95. return $_awards_data;
  96. }
  97. /**
  98. * 保存活动奖品信息
  99. *
  100. * @param int $act_id
  101. * @param array $awards_data
  102. * @param int $ttl
  103. */
  104. public function saveAwardsCache($act_id, $awards_data, $ttl = 3600) {
  105. $_key = $this->getAwardsKey($act_id);
  106. Cache::set($_key, json_encode($awards_data), $ttl);
  107. }
  108. /**
  109. * 更新奖品
  110. *
  111. * @param $award_id
  112. * @param $award_data
  113. *
  114. * @return bool
  115. */
  116. public function updateAward($award_id, $award_data) {
  117. $_key = $this->getAwardsKey($award_data['act_id']);
  118. Cache::rm($_key);
  119. return (new AwardsModel())->updateAward($award_data, $award_id);
  120. }
  121. /**
  122. * 添加奖品
  123. *
  124. * @param $award_data
  125. *
  126. * @return bool|mixed
  127. */
  128. public function addAward($award_data) {
  129. $_key = $this->getAwardsKey($award_data['act_id']);
  130. Cache::rm($_key);
  131. return (new AwardsModel())->addAward($award_data);
  132. }
  133. /**
  134. * 排序 排序字段为list_orders数组 POST 排序字段为:list_order
  135. *
  136. * @param $_act_id 活动id
  137. * @param $ids //排序值
  138. *
  139. * @return bool
  140. */
  141. public function listOrders($_act_id, $ids) {
  142. $_key = $this->getAwardsKey($_act_id);
  143. Cache::rm($_key);
  144. $model = new AwardsModel();
  145. $pk = $model->getPk(); //获取主键名称
  146. if (!empty($ids)) {
  147. foreach ($ids as $key => $r) {
  148. $data['list_order'] = $r;
  149. $model->where([$pk => $key])->update($data);
  150. }
  151. }
  152. return true;
  153. }
  154. }