HandleService.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace app\api\service\mlbb;
  3. use app\api\library\mlbb\LvRenSdk;
  4. use think\Cache;
  5. use think\Db;
  6. use think\Env;
  7. /**
  8. * 旅人游戏相关处理
  9. */
  10. class HandleService
  11. {
  12. protected $gift_cache_mark = 'mlbb_gift_pool:'; // 礼包码缓存标识
  13. protected $gift_cache_mark_time = 60*60*24*60; // 礼包码缓存时间,60天
  14. /**
  15. * 礼包码导入
  16. *
  17. * @param $mark_code string 礼包标识
  18. * @param $codeLists array 礼包码,逗号分隔
  19. *
  20. * @return array
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. * @throws \think\exception\DbException
  24. */
  25. public function importGift($mark_code, $codeLists){
  26. if(!$mark_code || !$codeLists){
  27. return ['code' => -100, 'msg' => '参数有误!', 'data' => []];
  28. }
  29. $prizeInfo = Db::table('lr_prize')->where(['mark_code' => $mark_code])->find();
  30. if(!$prizeInfo){
  31. return ['code' => -101, 'msg' => '礼包码标识不存在!', 'data' => []];
  32. }
  33. $data = [];
  34. $cacheCodes = [];
  35. foreach($codeLists as $v){
  36. $code = trim($v);
  37. $data[] = [
  38. 'prize_id' => $prizeInfo['id'],
  39. 'code' => $code,
  40. 'status' => 1,
  41. 'craete_time' => date('Y-m-d H:i:s', NOW_TIMESTAMP)
  42. ];
  43. $cacheCodes[] = $code;
  44. }
  45. if(count($cacheCodes) <= 0){
  46. return ['code' => -102, 'msg' => '导入码为空!', 'data' => []];
  47. }
  48. Db::startTrans();
  49. try {
  50. // PS:插入时候有兑换码唯一索引,缓存不做去重处理
  51. $res = Db::table('lr_prize_gift')->insertAll($data);
  52. if(!$res){
  53. throw new \Exception('存储失败,请稍后再试!', -111);
  54. }
  55. $updateRes = Db::table('lr_prize')->where(['mark_code' => $mark_code])->setInc('remaining_num', count($data));
  56. if(!$updateRes){
  57. throw new \Exception('存储编辑失败,请稍后再试!', -112);
  58. }
  59. // 对应礼包码存储缓存
  60. if(!$this->handleGiftCache('add', $mark_code, $cacheCodes)){
  61. throw new \Exception('缓存失败,请稍后再试!', -113);
  62. }
  63. Db::commit();
  64. } catch (\Exception $e) {
  65. Db::rollback();
  66. return ['code' => -199, 'msg' => '导入失败:' . $e->getMessage(), 'data' => []];
  67. }
  68. return ['code' => 200, 'msg' => '导入成功', 'data' => []];
  69. }
  70. /**
  71. * 获取礼包码
  72. * @param $mark_code
  73. *
  74. * @return array
  75. */
  76. public function getCacheGiftCode($mark_code){
  77. $res = $this->handleGiftCache('get', $mark_code);
  78. if($res){
  79. return ['code' => 200, 'msg' => '', 'data' => ['code' => $res]];
  80. }
  81. return ['code' => -100, 'msg' => '没有礼包码', 'data' => []];
  82. }
  83. /**
  84. * 添加礼包码
  85. * @param $mark_code
  86. *
  87. * @return array
  88. */
  89. public function addCacheGiftCode($mark_code, $code){
  90. if(!$mark_code || !$code){
  91. return ['code' => -110, 'msg' => '缺少礼包码', 'data' => []];
  92. }
  93. $prize_id = Db::table('lr_prize')->where(['mark_code' => $mark_code])->value('id');
  94. if(!$prize_id){
  95. return ['code' => -111, 'msg' => '错误的礼包码标识!', 'data' => []];
  96. }
  97. $giftCount = Db::table('lr_prize_gift')->where(['prize_id' => $prize_id, 'mark_code' => $code])->count();
  98. if($giftCount > 0){
  99. return ['code' => -112, 'msg' => '重复添加,当前礼包码已存在!', 'data' => []];
  100. }
  101. $res = $this->handleGiftCache('add', $mark_code, $code);
  102. if($res){
  103. return ['code' => 200, 'msg' => '', 'data' => ['code' => $res]];
  104. }
  105. return ['code' => -100, 'msg' => '没有礼包码', 'data' => []];
  106. }
  107. // 获取礼包码数量
  108. public function getCacheGiftNum(){
  109. $prizeList = Db::table('lr_prize')->where(['type' => 'pack'])->field('name,mark_code,all_num,use_num,remaining_num')->select();
  110. $notice = false;
  111. $noticeMsg = "## 礼包码数量预警\n";
  112. // $redis = Cache::store('redis')->handler();
  113. // $cacheKey = 'mlbb_detection_prize_notice:'.date("YmdH");
  114. foreach ($prizeList as $k => &$v) {
  115. $v['cache_num'] = $this->handleGiftCache('num', $v['mark_code']);
  116. if($v['cache_num'] < 100){
  117. $notice = true;
  118. $noticeMsg .= "- {$v['name']} - {$v['mark_code']}, 总计={$v['all_num']}, 剩余={$v['remaining_num']}, 已使用={$v['use_num']}, 缓存={$v['cache_num']}\n";
  119. }
  120. }
  121. $noticeMsg .= "时间: ".date('Y-m-d H:i:s', NOW_TIMESTAMP)."\n";
  122. // $res = $redis->get($cacheKey);
  123. // if($notice && !$res){
  124. // $redis->setex($cacheKey, 60*30, 1); // 30分钟内不重复发送
  125. // }
  126. if($notice){
  127. curlDDMarkdown($noticeMsg, Env::get('dingtalk.notic_url'), true);
  128. }
  129. return ['code' => 200, 'msg' => '', 'data' => $prizeList];
  130. }
  131. /**
  132. * 礼包操作缓存管理
  133. *
  134. * @param $type string 操作类型 get-获取 add-添加 num-数量
  135. * @param $mark_code string 礼包标识
  136. * @param $codeLists array 礼包码列表(仅添加时使用)
  137. *
  138. * @return void|bool
  139. */
  140. protected function handleGiftCache($type, $mark_code, $codeLists = []){
  141. if(!$type || !$mark_code){
  142. return false;
  143. }
  144. $cacheKey = $this->gift_cache_mark.$mark_code;
  145. $redis = Cache::store('redis')->handler();
  146. switch ($type){
  147. case 'get':
  148. $res = $redis->lPop($cacheKey); // string
  149. break;
  150. case 'add':
  151. $res = $redis->lPush($cacheKey, ...$codeLists); // int
  152. $redis->Expire($cacheKey, $this->gift_cache_mark_time);
  153. Db::table('lr_prize')->where(['mark_code' => $mark_code])->update(['all_num' => Db::raw('all_num + '.count($codeLists))]);
  154. break;
  155. case 'num':
  156. $res = $redis->Llen($cacheKey); // int
  157. break;
  158. default:
  159. $res = false;
  160. break;
  161. }
  162. return $res;
  163. }
  164. /**
  165. * 获取用户代金卷
  166. *
  167. * @param $coupon_id string 代金卷ID
  168. * @param $user_id string 用户ID
  169. *
  170. * @return array
  171. * @throws \think\db\exception\DataNotFoundException
  172. * @throws \think\db\exception\ModelNotFoundException
  173. * @throws \think\exception\DbException
  174. */
  175. public function getMemberCoupon($mark_code, $user_id){
  176. if (!$mark_code || !$user_id) {
  177. return ['code' => -100, 'msg' => '参数有误!', 'data' => []];
  178. }
  179. $coupon_id = Db::table('lr_prize')->where('mark_code', $mark_code)->value('mark_value');
  180. if(!$coupon_id){
  181. return ['code' => -110, 'msg' => '未找到当前奖品标识, '.$mark_code, 'data' => []];
  182. }
  183. $couponInfo = model('Common/Coupon')->where('id', $coupon_id)->whereRaw(sprintf('(type_id=1 and end_time>=%s) or (type_id=2)', Db::quote(date('Y-m-d'))))->find();
  184. if (!$couponInfo) {
  185. return ['code' => -111, 'msg' => '代金卷不存在, '. $coupon_id, 'data' => []];
  186. }
  187. // // 判断是否已领取过{一个账户一次活动可能领取重复代金卷}
  188. // if(Db::table('cy_coupon_member')->where(['coupon_id' => $couponInfo['id'], 'member_id' => $user_id])->count() > 0){
  189. // return ['code' => -112, 'msg' => '当前代金卷已领取!', 'data' => []];
  190. // }
  191. // 判断类型
  192. if ($couponInfo['type_id'] == 1) {
  193. $start_time = 0;
  194. $end_time = 0;
  195. } else {
  196. $start_time = strtotime(date('Y-m-d'));
  197. $end_time = strtotime(date('Y-m-d')) + 86400 * $couponInfo['day'] - 1;
  198. }
  199. $code = makeUniqueid('Q') . 'MLBB';
  200. $couponMemberInfo = [
  201. 'coupon_id' => $couponInfo['id'],
  202. 'member_id' => $user_id,
  203. 'start_time' => $start_time,
  204. 'end_time' => $end_time,
  205. 'examine' => 2,
  206. 'code' => $code,
  207. ];
  208. Db::startTrans();
  209. try {
  210. $saveCouponMember = Db::table('cy_coupon_member')->insert($couponMemberInfo);
  211. if(!$saveCouponMember){
  212. throw new \Exception('代金券领取失败', -120);
  213. }
  214. $saveCoupon = Db::table('cy_coupon')->where('id', $couponInfo['id'])->update(['grant_num' => $couponInfo['grant_num'] + 1,'receive_num' => $couponInfo['receive_num'] + 1]);
  215. if(!$saveCoupon){
  216. throw new \Exception('代金券更新失败', -121);
  217. }
  218. Db::commit();
  219. } catch (\Exception $e) {
  220. Db::rollback();
  221. return ['code' => -199, 'msg' => '操作异常:'.$e->getMessage(), 'data' => []];
  222. }
  223. return ['code' => 200, 'msg' => '操作成功', 'data' => ['code' => $code]];
  224. }
  225. // 发放道具
  226. public function entendRewards($entendData){
  227. $result = (new LvRenSdk())->sendRewards($entendData['sub_username'], $entendData['server_id'], $entendData['role_id'], $entendData['actId'], $entendData['actSubId']);
  228. if($result['code'] != 200){
  229. return ['code' => -100, 'msg' => $result['msg'], 'data' => []];
  230. }
  231. return ['code' => 200, 'msg' => '操作成功', 'data' => []];
  232. }
  233. }