Recharge.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Redis;
  4. //use think\Log;
  5. use app\main\constants\UserConstants;
  6. use think\exception\HttpException;
  7. use think\Log;
  8. use think\Model;
  9. class Recharge extends Model
  10. {
  11. // 表名
  12. protected $table = 'recharge';
  13. // 自动写入时间戳字段
  14. protected $autoWriteTimestamp = 'int';
  15. // 定义时间戳字段名
  16. protected $createTime = 'createtime';
  17. protected $updateTime = 'updatetime';
  18. // 追加属性
  19. protected $append = [
  20. 'type_text',
  21. 'free_endtime_text',
  22. 'edit_type_text'
  23. ];
  24. //当前链接的user_id分库
  25. protected $connectUserId = null;
  26. //最后一个插入的id
  27. protected $lastId = 0;
  28. /**
  29. * 设置分库链接数据
  30. *
  31. * @param $channel_id
  32. * @param $openid
  33. * @return $this
  34. */
  35. public function setConnect($user_id)
  36. {
  37. if ($this->connectUserId != $user_id) {
  38. $database = get_db_connect($this->table, $user_id);
  39. $this->setTable($database['table']);
  40. $this->connect($database);
  41. $this->connectUserId = $user_id;
  42. }
  43. return $this;
  44. }
  45. public function getTypeList()
  46. {
  47. return ['1' => __('书币充值'),'2' => __('VIP充值'),'3' => __('系统操作'),'4' => __('系统操作'),'5' => __('签到')];
  48. }
  49. public function getEditTypeList()
  50. {
  51. return ['1' => __('活动奖励'),'2' => __('系统操作')];
  52. }
  53. public function getTypeTextAttr($value, $data)
  54. {
  55. $value = $value ? $value : $data['type'];
  56. $list = $this->getTypeList();
  57. return isset($list[$value]) ? $list[$value] : '';
  58. }
  59. public function getFreeEndtimeTextAttr($value, $data)
  60. {
  61. $value = $value ? $value : $data['free_endtime'];
  62. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  63. }
  64. public function getEditTypeTextAttr($value, $data)
  65. {
  66. $value = $value ? $value : $data['edit_type'];
  67. $list = $this->getEditTypeList();
  68. return isset($list[$value]) ? $list[$value] : '';
  69. }
  70. protected function setFreeEndtimeAttr($value)
  71. {
  72. return $value && !is_numeric($value) ? strtotime($value) : $value;
  73. }
  74. /**
  75. * 可用免费书币查询 结果集按倒序排列
  76. * @param int $id user_id;
  77. */
  78. public function getFreeKandian($id){
  79. $res = $this->setConnect($id)->where('user_id',$id)->where('remain_free_kandian','>',0)->where('free_endtime','>',time())->order('free_endtime','desc')->select();
  80. if($res){
  81. $redis = Redis::instance();
  82. $zresult = $redis->zrangebyscore('ZR:' . $id, time(), '+inf'); //得到未过期免费书币记录
  83. $redis->del('ZR:' . $id);
  84. if (!empty($zresult)) {
  85. foreach ($zresult as $val) {
  86. $redis->del($val);
  87. }
  88. }
  89. foreach($res as $key=>$val){
  90. $rData = [];
  91. $rData['id'] = $val->id;
  92. $rData['remain_free_kandian'] = $val->remain_free_kandian;
  93. $rData['free_endtime'] = $val->free_endtime;
  94. $redis->setex('UR:'.$id.':'.$val->id,$val->free_endtime,json_encode($rData,JSON_UNESCAPED_UNICODE));
  95. $redis->zadd('ZR:'.$id,$val->free_endtime,'UR:'.$id.':'.$val->id);
  96. }
  97. $redis->expire('ZR:'.$id,86400*5);
  98. return is_array($res)?$res:$res->toArray();
  99. }else{
  100. return false;
  101. }
  102. }
  103. /**
  104. * 查询某个人最近的一条有效免费书币记录(返回一维数组)
  105. * @param int $id 用户id
  106. */
  107. public function getLastFreeRecord($id){
  108. $redis = Redis::instance();
  109. $redisKey = 'ZR:'.$id;
  110. if(!$redis->exists($redisKey)){
  111. $res = $this->getFreeKandian($id);
  112. $rechargeEnd = empty($res)?null:$res[0];
  113. }else{
  114. $zresult = $redis->zrevrangebyscore($redisKey, '+inf', time(), array('limit' => array(0, 1)));
  115. if($zresult){
  116. $strKey = $zresult[0];
  117. $rechargeEnd = $this->getUserFreeKandianFromCache($strKey);
  118. }else{
  119. $rechargeEnd = null;
  120. }
  121. }
  122. return $rechargeEnd;
  123. }
  124. /**
  125. * @param int $uid 用户id
  126. * 递归减少免费书币
  127. * return [] err 0成功,1失败 kandian:永久书币还需要扣除多少书币
  128. */
  129. public function reduceFreeKandian($uid,$free_kandian,$rid){
  130. $redis = Redis::instance();
  131. $redisKey = 'ZR:'.$uid;
  132. if($redis->exists($redisKey)){
  133. $zresult = $redis->zrangebyscore($redisKey, time(),'+inf', array('limit' => array(0, 1))); //得到该扣费的那条记录
  134. if($zresult){
  135. if($redis->get($zresult[0])){
  136. $recharge = $this->getUserFreeKandianFromCache($zresult[0]);
  137. }else{
  138. $returnArr = $this->getFreeKandian($uid);
  139. if($returnArr){
  140. $this->reduceFreeKandian($uid,$free_kandian,$rid);
  141. }else{
  142. return ['err'=>0,'kandian'=>$free_kandian];
  143. }
  144. }
  145. }
  146. }else{
  147. $result = $this->getFreeKandian($uid);
  148. if($result){
  149. $recharge = $result[count($result)-1];
  150. }else{
  151. return ['err'=>0,'kandian'=>$free_kandian];
  152. }
  153. }
  154. if(empty($recharge)){
  155. return ['err'=>0,'kandian'=>$free_kandian];
  156. }else{
  157. $userKey = 'UR:'.$uid.':'.$recharge['id'];
  158. if($recharge['remain_free_kandian']>=$free_kandian){ //如果此条记录够扣
  159. $remain = $recharge['remain_free_kandian'] - $free_kandian;
  160. $res = $this->setConnect($uid)->update(['remain_free_kandian'=>$remain],['id'=>$recharge['id']]);
  161. if($remain>0){
  162. $rData = [];
  163. $rData['id'] = $recharge['id'];
  164. $rData['remain_free_kandian'] = $remain;
  165. $rData['free_endtime'] = $recharge['free_endtime'];
  166. $redis->del($userKey);
  167. $redis->setex($userKey,$recharge['free_endtime']-time(),json_encode($rData,JSON_UNESCAPED_UNICODE));
  168. }else{
  169. $redis->del($userKey);
  170. $redis->zrem($redisKey,$userKey);
  171. }
  172. if($res){
  173. return ['err'=>0,'kandian'=>0];
  174. }else{
  175. return ['err'=>1];
  176. }
  177. }else{
  178. if($recharge['id'] == $rid){
  179. $redis->del($userKey);
  180. $redis->zrem($redisKey,$userKey);
  181. return ['err'=>0,'kandian'=>$free_kandian-$recharge['remain_free_kandian']];
  182. }else{
  183. $res = $this->setConnect($uid)->update(['remain_free_kandian'=>0],['id'=>$recharge['id']]);
  184. if($res){
  185. $redis->del($userKey);
  186. $redis->zrem($redisKey,$userKey);
  187. return $this->reduceFreeKandian($uid,$free_kandian-$recharge['remain_free_kandian'],$rid);
  188. }else{
  189. return ['err'=>1];
  190. }
  191. }
  192. }
  193. }
  194. }
  195. /**
  196. * 得到用户总共的免费书币数
  197. * @param int $uid
  198. * @return int
  199. */
  200. public function getTotalFreeKandian($uid){
  201. $totalKandian = 0;
  202. $redis = Redis::instance();
  203. $redisKey = 'ZR:'.$uid;
  204. if($redis->exists($redisKey)) {
  205. $zresult = $redis->zrangebyscore($redisKey, time(), '+inf'); //得到未过期免费书币记录
  206. if (!empty($zresult)) {
  207. foreach ($zresult as $val) {
  208. $data = $this->getUserFreeKandianFromCache($val);
  209. $totalKandian += $data['remain_free_kandian'];
  210. }
  211. }
  212. }else{
  213. $result = $this->setConnect($uid)->where('free_endtime','>',time())->where('user_id',$uid)->select();
  214. if($result){
  215. foreach($result as $val){
  216. $json = [];
  217. $json['id'] = $val->id;
  218. $json['remain_free_kandian'] = $val->remain_free_kandian;
  219. $json['free_endtime'] = $val->free_endtime;
  220. $redis->zadd($redisKey,$val->free_endtime,'UR:'.$uid.':'.$val->id);
  221. $redis->setex('UR:'.$uid.':'.$val->id, $val->free_endtime-time(), json_encode($json,JSON_UNESCAPED_UNICODE));
  222. $totalKandian += $val->remain_free_kandian;
  223. }
  224. $redis->expire($redisKey,86400*5);
  225. }
  226. }
  227. return $totalKandian;
  228. }
  229. /**
  230. * 处理自增ID
  231. *
  232. * @param $data
  233. */
  234. protected function autoId($data)
  235. {
  236. if (!isset($data['id'])) {
  237. // 获取redis自增id
  238. $redisAuto = Redis::instanceAuto();
  239. $newRechargeId = $redisAuto->incr('URID'); //redis自增返回新的recharge_id
  240. if (!$newRechargeId) {
  241. Log::error('充值记录自增ID获取失败!data:' . json_encode($data));
  242. throw new HttpException(500, '充值记录自增ID获取失败!');
  243. }
  244. $data['id'] = $newRechargeId;
  245. }
  246. return $data;
  247. }
  248. /**
  249. * 插入记录
  250. * @access public
  251. * @param mixed $data 数据
  252. * @param boolean $replace 是否replace
  253. * @param boolean $getLastInsID 返回自增主键
  254. * @param string $sequence 自增序列名
  255. * @return integer|string
  256. */
  257. public function insert(array $data = [], $replace = false, $getLastInsID = false, $sequence = null)
  258. {
  259. $data = $this->autoId($data);
  260. $res = parent::insert($data, $replace, false, $sequence);
  261. if ($res) {
  262. $this->lastId = $data['id'];
  263. if ($getLastInsID) {
  264. return $this->lastId;
  265. }
  266. }
  267. return $res;
  268. }
  269. /**
  270. * 插入记录并获取自增ID
  271. * @access public
  272. * @param mixed $data 数据
  273. * @param boolean $replace 是否replace
  274. * @param string $sequence 自增序列名
  275. * @return integer|string
  276. */
  277. public function insertGetId(array $data, $replace = false, $sequence = null)
  278. {
  279. $data = $this->autoId($data);
  280. if (parent::insert($data, $replace, false, $sequence)) {
  281. $this->lastId = $data['id'];
  282. return $this->lastId;
  283. } else {
  284. return 0;
  285. }
  286. }
  287. /**
  288. * 获取最近插入的ID
  289. * @access public
  290. * @param string $sequence 自增序列名
  291. * @return string
  292. */
  293. public function getLastInsID($sequence = null)
  294. {
  295. return $this->lastId;
  296. }
  297. public function getVipStartTime($userid)
  298. {
  299. $result = $this->setConnect($userid)
  300. ->where('user_id',$userid)
  301. ->whereIn('type',UserConstants::CHANNEL_VIP_TYPE)
  302. ->order('id','desc')
  303. ->limit(0,1)
  304. ->find();
  305. if($result){
  306. // 如果存在记录
  307. $data = $result->data;
  308. $vipTime = intval($data['day']) * 86400 + intval($data['hour']) * 60 * 60;
  309. if(($data['vip_starttime'] + $vipTime) > time()){
  310. $vip_starttime = $data['vip_starttime'] + $vipTime;
  311. } else{
  312. $vip_starttime = time();
  313. }
  314. }else{
  315. $vip_starttime = time();
  316. }
  317. return $vip_starttime;
  318. }
  319. /**
  320. * 获取充值缓存
  321. * @param $cacheKey
  322. * @return array|false|mixed|\PDOStatement|string|Model
  323. */
  324. public function getUserFreeKandianFromCache($cacheKey)
  325. {
  326. $data = Redis::instance()->get($cacheKey);
  327. if (!$data) {
  328. $cacheParam = explode(':', $cacheKey);
  329. $userId = $cacheParam[1];
  330. $rechargeId = $cacheParam[2];
  331. $recharge = $this->setConnect($userId)->where('id', $rechargeId)->find();
  332. $json = [];
  333. $json['id'] = $recharge->id;
  334. $json['remain_free_kandian'] = $recharge->remain_free_kandian;
  335. $json['free_endtime'] = $recharge->free_endtime;
  336. Redis::instance()->zadd('ZR:' . $userId, $recharge->free_endtime, 'UR:' . $userId . ':' . $recharge->id);
  337. Redis::instance()->setex('UR:' . $userId . ':' . $recharge->id, $recharge->free_endtime - time(), json_encode($json, JSON_UNESCAPED_UNICODE));
  338. } else {
  339. $recharge = json_decode($data, true);
  340. }
  341. return $recharge;
  342. }
  343. /**
  344. * 获取 渠道商vip_starttime
  345. * @param $userid
  346. * @return float|int
  347. */
  348. public function getChannelVipStartTime($userid)
  349. {
  350. $result = $this->setConnect($userid)
  351. ->where('user_id',$userid)
  352. ->where('dd', '=', '0')
  353. ->whereIn('type',UserConstants::CHANNEL_VIP_TYPE)
  354. ->order('id','desc')
  355. ->limit(0,1)
  356. ->find();
  357. if($result){
  358. // 如果存在记录
  359. $data = $result->data;
  360. $vipTime = intval($data['day']) * 86400 + intval($data['hour']) * 60 * 60;
  361. if(($data['channel_vip_starttime'] + $vipTime) > time()){
  362. $vip_starttime = $data['channel_vip_starttime'] + $vipTime;
  363. } else{
  364. $vip_starttime = time();
  365. }
  366. }else{
  367. $vip_starttime = time();
  368. }
  369. return $vip_starttime;
  370. }
  371. }