PrizeService.php 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hanl
  5. * Date: 2018/6/19
  6. * Time: 10:13
  7. */
  8. namespace App\Services;
  9. use DB;
  10. use App\Common\Api;
  11. use App\Models\Configs;
  12. use App\Models\Prizes;
  13. use App\Models\LotteryBoxs;
  14. use App\Services\ActivityService;
  15. use Illuminate\Support\Facades\Redis;
  16. class PrizeService
  17. {
  18. public function __construct($configs = [])
  19. {
  20. }
  21. /**
  22. * 获取奖品详情
  23. * @param $params
  24. * @return array
  25. */
  26. public static function getPrizeInfo($params){
  27. $activity_id = $params['activity_id'];
  28. $corp_id = $params['corp_id'];
  29. $prize_id = $params['prize_id'];
  30. $prizeInfo = Prizes::where('activity_id', $activity_id)->where('corp_id', $corp_id)->where('prize_id', $prize_id)->first();
  31. //数据库存储以千分位为单位,页面以百分位单位
  32. $prizeInfo['prize_weights'] = $prizeInfo['prize_weights']/10;
  33. return Api::arr(config('code.success'), trans('msg.success'), $prizeInfo);
  34. }
  35. /**
  36. * 添加/修改奖品详情
  37. * @param $params
  38. * @return array|string
  39. */
  40. public static function prizeDetail($params){
  41. $query = [];
  42. $query['activity_id'] = $params['activity_id'];
  43. $query['corp_id'] = $params['corp_id'];
  44. $query['lottery_level'] = $params['lottery_level'];
  45. $query['prize_name'] = $params['prize_name'];
  46. $query['prize_day_rule'] = $params['prize_day_rule'];
  47. $query['prize_count'] = $params['prize_count'];
  48. $query['prize_weights'] = $params['prize_weights']*10;
  49. $query['prize_image_url'] = $params['prize_image_url'];
  50. $query['update_time'] = time();
  51. if (!empty($params['prize_id'])) {
  52. //奖品信息判断
  53. $actPrizeInfo = self::getPrizeInfo($params);
  54. if($actPrizeInfo['code'] != 0 || empty($actPrizeInfo['data'])){
  55. return Api::arr(config('code.fail'), trans('msg.prize_not_exist'));
  56. }
  57. $res = Prizes::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->where('lottery_level', $params['lottery_level'])->where('prize_id', $params['prize_id'])->update($query);
  58. //奖品每日抽取逻辑优化 设置差值同步到redis
  59. $num = $query['prize_day_rule'] - $actPrizeInfo['data']['prize_day_rule'];
  60. $key = 'bs_prize_count:'.$params['prize_id'].':'.date('Ymd');
  61. if(Redis::exists($key) && $num != 0) {
  62. Redis::incrBy($key, $num);
  63. }
  64. } else {
  65. $query['create_time'] = time();
  66. $res = Prizes::insert($query);
  67. }
  68. if ($res) {
  69. return Api::arr(config('code.success'), trans('msg.success'));
  70. } else {
  71. return Api::arr(config('code.fail'), trans('msg.sql_error'));
  72. }
  73. }
  74. /**
  75. * 获取宝箱内的奖品列表
  76. * @param $corp_id
  77. * @param $activity_id
  78. * @param $lottery_level
  79. * @return array
  80. */
  81. public static function getPrizeList($corp_id, $activity_id, $lottery_level){
  82. $actInfo = ActivityService::getActivityInfo($activity_id, $corp_id);
  83. if(!$actInfo){
  84. return Api::arr(config('code.activity_not_exist'), trans('msg.activity_not_exist'));
  85. }
  86. $data = Prizes::where('activity_id', $activity_id)
  87. ->where('lottery_level', $lottery_level)
  88. ->where('status', '!=', 0)
  89. ->get()->toArray();
  90. foreach($data as $k=>$v){
  91. $data[$k]['prize_weights'] = $v['prize_weights']/10;
  92. }
  93. return Api::arr(config('code.success'), trans('msg.success'), $data);
  94. }
  95. /**
  96. * 奖品删除或下架
  97. * @param $params
  98. * @return array
  99. */
  100. public static function prizeSoldOut($params){
  101. $prizeInfo = Prizes::where('activity_id', $params['activity_id'])
  102. ->where('corp_id', $params['corp_id'])
  103. ->where('lottery_level', $params['lottery_level'])
  104. ->where('prize_id', $params['prize_id'])
  105. ->first();
  106. if(!$prizeInfo){
  107. return Api::arr(config('code.prize_not_exist'), trans('msg.prize_not_exist'));
  108. }
  109. $res = Prizes::where('activity_id', $params['activity_id'])
  110. ->where('corp_id', $params['corp_id'])
  111. ->where('lottery_level', $params['lottery_level'])
  112. ->where('prize_id', $params['prize_id'])
  113. ->update([
  114. 'status'=>$params['status'],
  115. 'update_time'=>time()
  116. ]);
  117. if ($res) {
  118. return Api::arr(config('code.success'), trans('msg.success'));
  119. } else {
  120. return Api::arr(config('code.fail'), trans('msg.sql_error'));
  121. }
  122. }
  123. /**
  124. * 获取活动宝箱配置
  125. * 2018-11-06 16:17
  126. * @param $params
  127. * @return array
  128. */
  129. public static function getLotteryBoxs($params){
  130. $activityId = $params['activity_id'];
  131. $corpId = $params['corp_id'];
  132. $boxInfo = LotteryBoxs::where('activity_id', $activityId)->where('corp_id', $corpId)->get()->toArray();
  133. return Api::arr(config('code.success'), trans('msg.success'), $boxInfo);
  134. }
  135. /**
  136. * 生成默认宝箱或保存宝箱参数
  137. * @param $params
  138. * @param bool $type
  139. * @return array
  140. */
  141. public static function defaultBox($params, $type=false){
  142. if($type){ //添加默认宝箱设置
  143. $boxs = [1,2,3,4];
  144. $querys = [];
  145. foreach($boxs as $k=>$v){
  146. $query = [];
  147. $query['activity_id'] = $params['activity_id'];
  148. $query['corp_id'] = $params['corp_id'];
  149. $query['lottery_level'] = $v;
  150. $query['open_mode'] = 'km';
  151. $query['create_time'] = $query['update_time'] = time();
  152. switch($v){
  153. case 1:
  154. $query['open_rule'] = 100;
  155. $query['lottery_name'] = '铜宝箱';
  156. $query['lottery_icon'] = '//s1-hn.aghcdn.com/brainstorming/prize/prize_one.png';
  157. break;
  158. case 2:
  159. $query['open_rule'] = 200;
  160. $query['lottery_name'] = '银宝箱';
  161. $query['lottery_icon'] = '//s1-hn.aghcdn.com/brainstorming/prize/prize_two.png';
  162. break;
  163. case 3:
  164. $query['open_rule'] = 500;
  165. $query['lottery_name'] = '金宝箱';
  166. $query['lottery_icon'] = '//s1-hn.aghcdn.com/brainstorming/prize/prize_three.png';
  167. break;
  168. case 4:
  169. $query['open_rule'] = 0;
  170. $query['lottery_name'] = '幸运宝箱';
  171. $query['lottery_icon'] = '//s1-hn.aghcdn.com/brainstorming/prize/prize_four.png';
  172. break;
  173. }
  174. $querys[] = $query;
  175. }
  176. $res = LotteryBoxs::insert($querys);
  177. if($res){
  178. $data = LotteryBoxs::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->get();
  179. return Api::arr(config('code.success'), trans('msg.success'), $data);
  180. } else {
  181. return Api::arr(config('code.sql_error'), trans('msg.sql_error'));
  182. }
  183. } else { //保存宝箱配置
  184. $arr = collect($params['box_config'])->toArray();
  185. foreach($arr as $k=>$v){
  186. $query = [];
  187. $query['lottery_name'] = $v['lottery_name'];
  188. $query['status'] = $v['status'];
  189. $query['open_rule'] = $v['open_rule'];
  190. $query['update_time'] = time();
  191. $actLevelInfo = LotteryBoxs::where('activity_id', $params['activity_id'])
  192. ->where('corp_id',$params['corp_id'])
  193. ->where('lottery_level', $k)
  194. ->first();
  195. if($actLevelInfo){
  196. LotteryBoxs::where('activity_id', $params['activity_id'])
  197. ->where('corp_id',$params['corp_id'])
  198. ->where('lottery_level', $k)
  199. ->update($query);
  200. } else {
  201. $query['activity_id'] = $params['activity_id'];
  202. $query['corp_id'] = $params['corp_id'];
  203. $query['lottery_level'] = $params['lottery_level'];
  204. $query['create_time'] = time();
  205. LotteryBoxs::insert($query);
  206. }
  207. }
  208. return true;
  209. }
  210. }
  211. }
  212. ?>