BookGiftsService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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) return [];
  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 <18 && $price >=2){
  129. $gift = [['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']];
  130. }elseif ($price <30){
  131. $gift = [['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  132. ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']];
  133. }elseif($price < 50 ){
  134. $gift = [
  135. ['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  136. ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  137. ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
  138. ];
  139. }elseif ($price <100){
  140. $gift = [
  141. ['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  142. ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  143. ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  144. ['gift_id'=>3,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
  145. ];
  146. }elseif ($price <200){
  147. $gift = [
  148. ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  149. ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  150. ['gift_id'=>3,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  151. ['gift_id'=>2,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
  152. ];
  153. }elseif ($price >=200){
  154. $gift = [
  155. ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  156. ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  157. ['gift_id'=>3,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  158. ['gift_id'=>2,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  159. ['gift_id'=>8,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
  160. ];
  161. }
  162. if($charge_type == 'YEAR_ORDER'){
  163. $gift = [
  164. ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  165. ['gift_id'=>2,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  166. ['gift_id'=>3,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  167. ['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  168. ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  169. ['gift_id'=>8,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
  170. ];
  171. }
  172. if($gift){
  173. foreach ($gift as $item){
  174. $item['name_desc'] = $gift_info[$item['gift_id']];
  175. $item['cost'] = $gift_info[$item['cost']];
  176. $item['icon'] = $gift_info[$item['icon']];
  177. }
  178. }
  179. return $gift;
  180. }
  181. if($property == 'high' || $property == 'medium'){
  182. $gift = [];
  183. if($price <30 && $price >=18){
  184. $gift = [
  185. ['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  186. ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  187. ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
  188. ];
  189. }elseif($price < 50 ){
  190. $gift = [
  191. ['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  192. ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  193. ['gift_id'=>1,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>'']
  194. ];
  195. }elseif ($price <100){
  196. $gift = [
  197. ['gift_id'=>6,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
  198. ['gift_id'=>7,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
  199. ['gift_id'=>1,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
  200. ['gift_id'=>3,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
  201. ];
  202. }elseif ($price <200){
  203. $gift = [
  204. ['gift_id'=>7,'num'=>3,'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. ['gift_id'=>2,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
  208. ];
  209. }elseif ($price >=200){
  210. $gift = [
  211. ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  212. ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  213. ['gift_id'=>3,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  214. ['gift_id'=>2,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
  215. ['gift_id'=>8,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
  216. ];
  217. }
  218. if($charge_type == 'YEAR_ORDER'){
  219. $gift = [
  220. ['gift_id'=>1,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
  221. ['gift_id'=>2,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
  222. ['gift_id'=>3,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
  223. ['gift_id'=>6,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
  224. ['gift_id'=>7,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
  225. ['gift_id'=>8,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>'']
  226. ];
  227. }
  228. if($gift){
  229. foreach ($gift as $item){
  230. $item['name_desc'] = $gift_info[$item['gift_id']];
  231. $item['cost'] = $gift_info[$item['cost']];
  232. $item['icon'] = $gift_info[$item['icon']];
  233. }
  234. }
  235. return $gift;
  236. }
  237. return [];
  238. }
  239. }