BookGiftsService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2017/12/4
  6. * Time: 上午11:49
  7. */
  8. namespace App\Modules\Book\Services;
  9. use App\Modules\Book\Models\BookGifts;
  10. use App\Modules\Book\Models\BookGiftsSend;
  11. use App\Modules\BookGifts\Models\BookGiftsStatsByBook;
  12. use App\Modules\BookGifts\Models\BookGiftsStatsByGift;
  13. use App\Modules\User\Services\UserService;
  14. use DB;
  15. use Redis;
  16. class BookGiftsService
  17. {
  18. public static function getAllGifts() {
  19. $gifts = Redis::get('BookGifts:');
  20. if($gifts){
  21. $gifts = unserialize($gifts);
  22. }else{
  23. $gifts = BookGifts::getAllGifts();
  24. Redis::set('BookGifts:',serialize($gifts));
  25. Redis::expire('BookGifts:',86400);
  26. }
  27. return $gifts;
  28. }
  29. public static function getOneGift($id) {
  30. return BookGifts::find($id);
  31. }
  32. public static function sendGiftToBook($param) {
  33. DB::beginTransaction();
  34. try{
  35. $coin_paid = self::payCoin($param['cost'],$param['uid']);
  36. if($coin_paid) {
  37. $param['cost_reward'] =$coin_paid['decrease_reward'];
  38. $param['cost_recharge'] =$coin_paid['decrease_recharge'];
  39. }
  40. $BookGifts = BookGiftsSend::addGift($param);
  41. if(!$BookGifts || !$coin_paid) {
  42. DB::rollback();
  43. return false;
  44. };
  45. }catch (\Exception $e){
  46. DB::rollback();
  47. \Log::error('sendGiftForBook failed:'.$e->getMessage());
  48. return false;
  49. }
  50. DB::commit();
  51. return $BookGifts;
  52. }
  53. public static function getSendRecords($bid) {
  54. return BookGiftsSend::getSendRecords($bid);
  55. }
  56. public static function getSendRecordsV2($bid) {
  57. return BookGiftsSend::getSendRecordsV2($bid);
  58. }
  59. public static function getConsumeSendRecords($uid) {
  60. return BookGiftsSend::getConsumeSendRecords($uid);
  61. }
  62. protected static function payCoin($fee,$uid) {
  63. //DB::beginTransaction();
  64. $res = $result1 = $result2 = null;
  65. $user = UserService::getById($uid);
  66. if($user->balance < $fee){
  67. return false;
  68. }
  69. $decrease_recharge = 0;
  70. $decrease_reward =0;
  71. if ($user->charge_balance >= $fee) {
  72. $data['charge_balance'] = $fee;
  73. $charge_fee = $fee;
  74. $data['reward_balance'] = 0;
  75. $result1 = $user->decrement('balance', $fee);
  76. $result2 = $user->decrement('charge_balance', $fee);
  77. $decrease_recharge = $fee;
  78. } elseif ($user->charge_balance > 0) {
  79. $data['charge_balance'] = $user->charge_balance;
  80. $charge_fee = $user->charge_balance;
  81. $reword_fee = $data['reward_balance'] = $fee - $user->charge_balance;
  82. $result1 = $user->decrement('balance', $fee);
  83. $result2 = $user->decrement('charge_balance', $charge_fee);
  84. $result3 = $user->decrement('reward_balance', $reword_fee);
  85. $decrease_recharge = $charge_fee;
  86. $decrease_reward =$reword_fee;
  87. } else {
  88. $data['charge_balance'] = 0;
  89. $reword_fee = $data['reward_balance'] = $fee;
  90. $result1 = $user->decrement('balance', $fee);
  91. $result2 = $user->decrement('reward_balance', $reword_fee);
  92. $decrease_reward =$reword_fee;
  93. }
  94. if ($result2 && $result1) {
  95. //DB::commit();
  96. return compact('decrease_recharge','decrease_reward','fee');
  97. //return true;
  98. }
  99. //DB::rollback();
  100. return false;
  101. }
  102. public static function getGiftsSendStatisticByBook($start,$end){
  103. return BookGiftsSend::getGiftsSendStatisticByBook($start,$end);
  104. }
  105. public static function getGiftsSendStatisticByGift($start,$end){
  106. return BookGiftsSend::getGiftsSendStatisticByGift($start,$end);
  107. }
  108. public static function getGiftsSendDaillyStatsByBook($start='',$end='') {
  109. return BookGiftsStatsByBook::getStats($start,$end);
  110. }
  111. public static function getGiftsSendDaillyStatsByGift() {
  112. return BookGiftsStatsByGift::getStats();
  113. }
  114. public static function chargeGiveGift($property,$price,$charge_type){
  115. if(!$property) $property = 'none';
  116. $gift_info = [
  117. 1=>['name_desc'=>'黄金玫瑰','cost'=>200,'icon'=>'https://yqcdn.iycdm.com/wap/present/20181016/3.png'],
  118. 2=>['name_desc'=>'文学奖','cost'=>800,'icon'=>'https://yqcdn.iycdm.com/wap/present/20181016/5.png'],
  119. 3=>['name_desc'=>'锦囊','cost'=>500,'icon'=>'https://yqcdn.iycdm.com/wap/present/20181016/4.png'],
  120. 4=>['name_desc'=>'小皮鞭','cost'=>200,'icon'=>'https://yqcdn.iycdm.com/wap/present/20181016/6.png'],
  121. 5=>['name_desc'=>'催更票','cost'=>100,'icon'=>'https://yqcdn.iycdm.com/wap/present/20181016/1.png'],
  122. 6=>['name_desc'=>'点个赞','cost'=>50,'icon'=>'https://yqcdn.iycdm.com/wap/present/20181016/2.png'],
  123. 7=>['name_desc'=>'文房四宝','cost'=>100,'icon'=>'https://cdn-novel.iycdm.com/h5/20181107/four_literature_tools.png'],
  124. 8=>['name_desc'=>'至尊大神','cost'=>1800,'icon'=>'https://cdn-novel.iycdm.com/h5/20181107/literature_emprior.png'],
  125. ];
  126. if($property == 'none' || $property == 'low'){
  127. $gift = [];
  128. if($price <2){
  129. return [];
  130. }
  131. if($price <18 && $price >=2){
  132. $gift = [['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']];
  133. }elseif ($price <30){
  134. $gift = [['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  135. ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']];
  136. }elseif($price < 50 ){
  137. $gift = [
  138. ['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  139. ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  140. ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
  141. ];
  142. }elseif ($price <100){
  143. $gift = [
  144. ['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  145. ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  146. ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  147. ['gift_id'=>3,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
  148. ];
  149. }elseif ($price <200){
  150. $gift = [
  151. ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  152. ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  153. ['gift_id'=>3,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  154. ['gift_id'=>2,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
  155. ];
  156. }elseif ($price >=200){
  157. $gift = [
  158. ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  159. ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  160. ['gift_id'=>3,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  161. ['gift_id'=>2,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  162. ['gift_id'=>8,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
  163. ];
  164. }
  165. if($charge_type == 'YEAR_ORDER'){
  166. $gift = [
  167. ['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  168. ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  169. ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  170. ['gift_id'=>3,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  171. ['gift_id'=>2,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  172. ['gift_id'=>8,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
  173. ];
  174. }
  175. if($gift){
  176. foreach ($gift as &$item){
  177. $item['name_desc'] = $gift_info[$item['gift_id']]['name_desc'];
  178. $item['cost'] = $gift_info[$item['gift_id']]['cost'];
  179. $item['icon'] = $gift_info[$item['gift_id']]['icon'];
  180. }
  181. }
  182. return $gift;
  183. }
  184. if($property == 'high' || $property == 'medium'){
  185. $gift = [];
  186. if($price < 18){
  187. return [];
  188. }
  189. if($price <30 && $price >=18){
  190. $gift = [
  191. ['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  192. ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  193. ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
  194. ];
  195. }elseif($price < 50 ){
  196. $gift = [
  197. ['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  198. ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  199. ['gift_id'=>1,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>'']
  200. ];
  201. }elseif ($price <100){
  202. $gift = [
  203. ['gift_id'=>6,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
  204. ['gift_id'=>7,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
  205. ['gift_id'=>1,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
  206. ['gift_id'=>3,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
  207. ];
  208. }elseif ($price <200){
  209. $gift = [
  210. ['gift_id'=>7,'num'=>3,'name_desc'=>'','cost'=>'','icon'=>''],
  211. ['gift_id'=>1,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
  212. ['gift_id'=>3,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  213. ['gift_id'=>2,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
  214. ];
  215. }elseif ($price >=200){
  216. $gift = [
  217. ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  218. ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  219. ['gift_id'=>3,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  220. ['gift_id'=>2,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  221. ['gift_id'=>8,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
  222. ];
  223. }
  224. if($charge_type == 'YEAR_ORDER'){
  225. $gift = [
  226. ['gift_id'=>6,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
  227. ['gift_id'=>7,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
  228. ['gift_id'=>1,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
  229. ['gift_id'=>3,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
  230. ['gift_id'=>2,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
  231. ['gift_id'=>8,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>'']
  232. ];
  233. }
  234. if($gift){
  235. foreach ($gift as &$item){
  236. $item['name_desc'] = $gift_info[$item['gift_id']]['name_desc'];
  237. $item['cost'] = $gift_info[$item['gift_id']]['cost'];
  238. $item['icon'] = $gift_info[$item['gift_id']]['icon'];
  239. }
  240. }
  241. return $gift;
  242. }
  243. return [];
  244. }
  245. }