SignMeal.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace app\weixin\model;
  3. use app\weixin\service\PRedis;
  4. use think\Model;
  5. class SignMeal extends Model
  6. {
  7. protected $table = 'sg_sign_meals';
  8. /**
  9. * 获取信息
  10. * @param $where 条件
  11. * @param string $field 字段
  12. * @return array|false|\PDOStatement|string|Model
  13. */
  14. public static function getInfo($where, $field = "*")
  15. {
  16. $info = self::where($where)->field($field)->find();
  17. return $info? $info->toArray() : [];
  18. }
  19. /**
  20. * 获取套餐列表
  21. * @return array|bool|\PDOStatement|string|\think\Collection
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. * @throws \think\exception\DbException
  25. */
  26. public static function getList(){
  27. $cacheKey = "cache:signMeals";
  28. $dataList = PRedis::get($cacheKey);
  29. if($dataList){
  30. return $dataList;
  31. }
  32. $where = ['status'=> 1];
  33. $dataList = SignMeal::where($where)
  34. ->field('id,name,give_num,remark,day,status')
  35. ->select();
  36. $dataList = $dataList? $dataList->toArray() : [];
  37. if($dataList){
  38. PRedis::set($cacheKey, $dataList, 3600);
  39. }
  40. return $dataList;
  41. }
  42. /**
  43. * 签到处理
  44. * @param $userId
  45. * @param $params
  46. * @return array|false|int
  47. * @throws \think\Exception
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. * @throws \think\exception\DbException
  51. * @throws \think\exception\PDOException
  52. */
  53. public static function catchSign($userId, $params){
  54. $memberInfo = Member::where(['id' => $userId,'user_status'=> 1])->field('id,openid,sign_day,user_nickname,sign_at,redheart')->find();
  55. if(empty($memberInfo)){
  56. return false;
  57. }
  58. $signDay = isset($params['day'])? $params['day'] : 0;
  59. $signGive = isset($params['give_num'])? $params['give_num'] : 0;
  60. $signMealId = isset($params['id'])? $params['id'] : 0;
  61. $curSignDay = isset($memberInfo['sign_day'])? $memberInfo['sign_day'] : 0;
  62. $curSignAt = isset($memberInfo['sign_at'])? $memberInfo['sign_at'] : '';
  63. if($curSignDay && (empty($curSignAt) || $curSignAt <= date('Y-m-d', strtotime(date('Y-m-d')) - 86400))){
  64. $curSignDay = 0;
  65. }
  66. if($curSignAt >= date('Y-m-d') && $curSignDay){
  67. return 2143;
  68. }
  69. if(db('account_log')->where('created_at','>=',date('Y-m-d'))->where('remark','like',"连续签到%")->where(['type'=> 4,'status'=> 2])->value('id')){
  70. return 2143;
  71. }
  72. if($curSignDay != $signDay-1 && $curSignDay<7){
  73. return 2142;
  74. }
  75. // 处理
  76. db()->startTrans();
  77. if(!db()->name('user')->where(['id' => $userId])->update(['sign_day'=> $signDay,'sign_at'=> date('Y-m-d H:i:s')])){
  78. db()->rollback();
  79. return 2144;
  80. }
  81. if($signGive>0){
  82. if(!db()->name('user')->where(['id' => $userId])->setInc('redheart',$signGive)){
  83. db()->rollback();
  84. return 2145;
  85. }
  86. $accountData = [
  87. 'type' => 12,
  88. 'account_type' => 1,
  89. 'change_type' => 1,
  90. 'user_id' => $userId,
  91. 'money' => $signGive,
  92. 'balance' => isset($memberInfo['redheart'])? $memberInfo['redheart'] : 0,
  93. 'created_at' => date('Y-m-d H:i:s'),
  94. 'remark' => "连续签到{$signDay}天",
  95. ];
  96. PRedis::set('cache:signs:account_' . $userId, ['params' => $params, 'log' => $accountData,'user'=> $memberInfo], 600);
  97. db('account_log')->insertGetId($accountData);
  98. }
  99. db()->commit();
  100. return ['id'=> $userId];
  101. }
  102. }