| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace app\weixin\model;
- use app\weixin\service\PRedis;
- use think\Model;
- class SignMeal extends Model
- {
- protected $table = 'sg_sign_meals';
- /**
- * 获取信息
- * @param $where 条件
- * @param string $field 字段
- * @return array|false|\PDOStatement|string|Model
- */
- public static function getInfo($where, $field = "*")
- {
- $info = self::where($where)->field($field)->find();
- return $info? $info->toArray() : [];
- }
- /**
- * 获取套餐列表
- * @return array|bool|\PDOStatement|string|\think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public static function getList(){
- $cacheKey = "cache:signMeals";
- $dataList = PRedis::get($cacheKey);
- if($dataList){
- return $dataList;
- }
- $where = ['status'=> 1];
- $dataList = SignMeal::where($where)
- ->field('id,name,give_num,remark,day,status')
- ->select();
- $dataList = $dataList? $dataList->toArray() : [];
- if($dataList){
- PRedis::set($cacheKey, $dataList, 3600);
- }
- return $dataList;
- }
- /**
- * 签到处理
- * @param $userId
- * @param $params
- * @return array|false|int
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public static function catchSign($userId, $params){
- $memberInfo = Member::where(['id' => $userId,'user_status'=> 1])->field('id,openid,sign_day,user_nickname,sign_at,redheart')->find();
- if(empty($memberInfo)){
- return false;
- }
- $signDay = isset($params['day'])? $params['day'] : 0;
- $signGive = isset($params['give_num'])? $params['give_num'] : 0;
- $signMealId = isset($params['id'])? $params['id'] : 0;
- $curSignDay = isset($memberInfo['sign_day'])? $memberInfo['sign_day'] : 0;
- $curSignAt = isset($memberInfo['sign_at'])? $memberInfo['sign_at'] : '';
- if($curSignDay && (empty($curSignAt) || $curSignAt <= date('Y-m-d', strtotime(date('Y-m-d')) - 86400))){
- $curSignDay = 0;
- }
- if($curSignAt >= date('Y-m-d') && $curSignDay){
- return 2143;
- }
- if(db('account_log')->where('created_at','>=',date('Y-m-d'))->where('remark','like',"连续签到%")->where(['type'=> 4,'status'=> 2])->value('id')){
- return 2143;
- }
- if($curSignDay != $signDay-1 && $curSignDay<7){
- return 2142;
- }
- // 处理
- db()->startTrans();
- if(!db()->name('user')->where(['id' => $userId])->update(['sign_day'=> $signDay,'sign_at'=> date('Y-m-d H:i:s')])){
- db()->rollback();
- return 2144;
- }
- if($signGive>0){
- if(!db()->name('user')->where(['id' => $userId])->setInc('redheart',$signGive)){
- db()->rollback();
- return 2145;
- }
- $accountData = [
- 'type' => 12,
- 'account_type' => 1,
- 'change_type' => 1,
- 'user_id' => $userId,
- 'money' => $signGive,
- 'balance' => isset($memberInfo['redheart'])? $memberInfo['redheart'] : 0,
- 'created_at' => date('Y-m-d H:i:s'),
- 'remark' => "连续签到{$signDay}天",
- ];
- PRedis::set('cache:signs:account_' . $userId, ['params' => $params, 'log' => $accountData,'user'=> $memberInfo], 600);
- db('account_log')->insertGetId($accountData);
- }
- db()->commit();
- return ['id'=> $userId];
- }
- }
|