| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 |
- <?php
- namespace app\api\controller;
- use app\portal\model\UserModel;
- use app\user\model\PoolModel;
- use app\weixin\model\AccountLog;
- use app\weixin\model\Books;
- use app\weixin\model\Devices;
- use app\weixin\model\Member;
- use app\weixin\model\UserBalanceLog;
- use app\weixin\model\UserContactLog;
- use app\weixin\service\Award;
- use app\weixin\service\Export;
- use app\weixin\service\PRedis;
- use think\Controller;
- use think\Db;
- class TaskController extends Controller
- {
- /**
- * 订单超时处理
- * @throws \think\Exception
- * @throws \think\exception\PDOException
- */
- public function catchBook()
- {
- try {
- $key = input('key', '');
- $checkKey = config('task.key');
- if ($key != $checkKey) {
- showJson(1004, 2009, '', "\n");
- }
- $cancelTime = config('task.orderCancelTime');
- $cancelTime = $cancelTime ? $cancelTime * 60 : 30 * 60;
- // 未支付
- $cancelTime = date('Y-m-d H:i:s', time() - $cancelTime);
- $cancelCount = Books::where(['status' => 1])
- ->where('created_at', '<=', $cancelTime)
- ->count('id');
- Books::where(['status' => 1])
- ->where('created_at', '<=', $cancelTime)
- ->update(['status' => 4, 'remark' => '超时自动取消']);
- // 已取消的删除
- $deleteTime = date('Y-m-d H:i:s', time() - 3 * 24 * 3600);
- $deleteCount = Books::where(['status' => 4])
- ->where('created_at', '<=', $deleteTime)
- ->count('id');
- Books::where(['status' => 4])
- ->where('created_at', '<=', $deleteTime)
- ->delete();
- $msg = "报名订单自动取消已处理成功,累计取消{$cancelCount}个,删除{$deleteCount}个";
- return showJson(1005, $msg, '', "\n");
- } catch (\Exception $exception) {
- return showJson(1004, $exception->getMessage(), '', "\n");
- }
- }
- /**
- * 认识申请超时处理
- */
- public function cancelContact()
- {
- $key = input('key', '');
- $checkKey = config('task.contactKey');
- if ($key != $checkKey) {
- showJson(1004, 2009, '', "\n");
- }
- try {
- // 申请失效时间
- $siteInfo = cmf_get_site_info();
- $expire = isset($siteInfo['contact_time']) ? intval($siteInfo['contact_time']) : 0;
- $expire = $expire ? $expire : 1;
- $dataList = UserContactLog::where(['status' => 1])
- ->where('created_at', '<', date('Y-m-d H:i:s', time() - $expire * 24 * 3600))
- ->field('id,contact_uid')
- ->select();
- if (empty($dataList)) {
- showJson(1004, 1003, '', "\n");
- }
- $results = [];
- foreach ($dataList as $val) {
- $cid = isset($val['id']) ? intval($val['id']) : 0;
- $userId = isset($val['contact_uid']) ? intval($val['contact_uid']) : 0;
- if ($cid && $userId) {
- $res = Member::contactConfirm($userId, $cid, 4);
- if (is_array($res)) {
- $results[] = $res;
- }
- }
- }
- PRedis::set('tasks:contact:' . date('Ymd'), ['datalist' => $dataList, 'result' => $results], 3600);
- $msg = "认识申请记录超时处理结果,累计处理" . count($results) . "个";
- return showJson(1005, $msg, "\n");
- } catch (\Exception $exception) {
- return showJson(1004, $exception->getMessage(), '', "\n");
- }
- }
- /**
- * 批量推荐队列处理
- */
- public function catchMakeHearts()
- {
- set_time_limit(0);
- $key = input('key', '');
- $checkKey = config('task.heartKey');
- if ($key != $checkKey) {
- showJson(1004, 2009, '', "\n");
- }
- try {
- $userIds = [];
- $queenKey = "queens:hearts:" . date('Ymd');
- //echo $queenKey."<br>\n";
- for ($i = 0; $i < 500; $i++) {
- $userId = PRedis::lpop($queenKey);
- //echo $userId."\n";
- if ($userId) {
- $userIds[] = $userId;
- $url = url('/api/task/catchUserHeart', '', '', true);
- httpRequest($url, ['uid' => $userId], 'post', '', 2);
- }
- }
- $msg = "更新推荐数据结果,累计处理" . count($userIds) . "个会员数据更新";
- return showJson(1005, $msg, "\n");
- } catch (\Exception $exception) {
- return showJson(1004, $exception->getMessage(), '', "\n");
- }
- }
- /**
- * 清除过期签到爱心
- */
- public function clearSignHeart()
- {
- set_time_limit(0);
- $key = input('key', '');
- $checkKey = config('task.clearHeartKey');
- if ($key != $checkKey) {
- showJson(1004, 2009, '', "\n");
- }
- try {
- $month = date('Y-m-01', time() - 2 * 86400);
- $users = AccountLog::where(['type' => 12, 'status' => 2])
- ->where('created_at', '>=', $month)
- ->order('created_at', 'asc')
- ->column('user_id');
- $userIds = [];
- if ($users) {
- foreach ($users as $userId) {
- // 清除签到爱心
- if ($userId && \app\weixin\service\Member::clearSignRedHeart($userId)) {
- $userIds[] = $userId;
- }
- }
- }
- $msg = "清除签到爱心数据结果,共" . count($users) . "个,累计处理" . count($userIds) . "个会员数据更新";
- return showJson(1005, $msg, "\n");
- } catch (\Exception $exception) {
- return showJson(1004, $exception->getMessage(), '', "\n");
- }
- }
- /**
- * 处理怦然心动
- */
- public function catchUserHeart()
- {
- $uid = input('uid', 0);
- $dataList = Member::getHeartList($uid, '', true);
- showJson(1005, 1001, $dataList);
- }
- /**
- * 更新怦然心动推荐数据入队处理
- */
- public function makeHearts()
- {
- set_time_limit(0);
- $key = input('key', '');
- $checkKey = config('task.heartKey');
- if ($key != $checkKey) {
- showJson(1004, 2009, '', "\n");
- }
- try {
- // 查询需要推荐的用户
- $dataList = Member::alias('m')
- ->join('user_profile up', 'up.userid=m.id', 'left')
- ->field('m.openid,m.user_nickname,m.id,up.idcard_check,m.is_reg_profile')
- ->where(['m.user_status' => 1, 'm.user_type' => 2, 'up.idcard_check' => 2, 'm.is_reg_profile' => 1])
- ->where(function ($query) {
- return $query->where('m.heart_recommend_at', '<', date('Y-m-d 19:00:00'))
- ->whereOr('m.heart_recommend_at', 'exp', 'is null');
- })
- ->order('m.id')
- ->column('m.id');
- if (empty($dataList)) {
- showJson(1004, 1003, '', "\n");
- }
- $sql = Member::getLastSql();
- //echo $sql;
- // 处理数据更新
- $userids = [];
- $queenKey = "queens:hearts:" . date('Ymd');
- foreach ($dataList as $userId) {
- if ($userId && PRedis::rpush($queenKey, $userId)) {
- $userids[] = $userId;
- }
- }
- PRedis::expire($queenKey, 2 * 3600);
- PRedis::set('tasks:hearts:' . date('Ymd'), ['datalist' => $dataList, 'results' => $userids, 'time' => date('Y-m-d H:i:s'), 'sql' => $sql], 3600);
- $msg = "更新推荐数据入队结果,累计处理" . count($userids) . "个会员数据更新";
- return showJson(1005, $msg, "\n");
- } catch (\Exception $exception) {
- return showJson(1004, $exception->getMessage(), '', "\n");
- }
- }
- /**
- * 更新隐身
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public function updateHeartStatus()
- {
- set_time_limit(0);
- $key = input('key', '');
- $checkKey = config('task.upHeartKey');
- if ($key != $checkKey) {
- showJson(1004, 2009, '', "\n");
- }
- $where = ['m.is_heart' => 1, 'm.is_reg_profile' => 1, 'm.user_status' => 1, 'm.user_type' => 2];
- $dataList = Member::alias('m')
- ->join('user_profile up', 'up.userid=m.id', 'left')
- ->where($where)
- ->where(function ($query) {
- $query->where(db()->raw("up.introduce is NULL or up.introduce = ''"))
- ->whereOr(db()->raw("up.family is NULL or up.family = ''"))
- ->whereOr(db()->raw("up.hobby is NULL or up.hobby = ''"))
- ->whereOr(db()->raw("up.purpose is NULL or up.purpose = ''"))
- ->whereOr(db()->raw("up.cause is NULL or up.cause = ''"))
- ->whereOr(db()->raw("up.expect is NULL or up.expect = ''"));
- })
- ->field('m.id,up.introduce,up.family,up.hobby,up.purpose,up.cause,up.expect')
- ->select()
- ->each(function ($profile, $k) {
- if (empty($profile) || (empty($profile['photolist']) || empty($profile['introduce']) || empty($profile['family']) || empty($profile['hobby']) || empty($profile['purpose']) || empty($profile['cause']) || empty($profile['expect']))) {
- Member::where(['id' => $profile['id']])->update(['is_heart' => 2, 'remark' => '系统检测自动设置隐身']);
- }
- });
- showJson(1005, 1001, $dataList);
- }
- /**
- * 活动报名分销收益结算
- * @throws \think\Exception
- * @throws \think\Exception\DbException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function catchBookMarket()
- {
- set_time_limit(0);
- $key = input('key', '');
- $checkKey = config('task.catchBookMarket');
- if ($key != $checkKey) {
- showJson(1004, 2009, '', "\n");
- }
- $catchIds = [];
- $catchList = [];
- $bookList = Books::alias('b')
- ->leftJoin('activity a', 'a.id=b.aid')
- ->leftJoin('user u', 'u.id=b.uid')
- ->where('a.endtime', '<=', time())
- // ->where('a.endtime', '>', time()-24*3600*3)
- ->where('u.parent_id', '>', 0)
- ->where(['a.is_top' => 0, 'a.status' => 1, 'b.status' => 3, 'b.is_market' => 2])
- ->field('b.id,b.uid,b.aid,b.money,u.parent_id,b.status')
- ->order('a.endtime desc, b.book_at asc,b.id asc')
- ->paginate(80)
- ->each(function ($item, $k) use (&$catchList, &$catchIds) {
- $userId = isset($item['uid']) ? intval($item['uid']) : 0;
- $aid = isset($item['aid']) ? $item['aid'] : 0;
- $money = isset($item['money']) ? floatval($item['money']) : 0;
- $inviteInfo = Member::getInviteInfo($userId);
- $inviteId = isset($inviteInfo['invite_id']) ? $inviteInfo['invite_id'] : 0;
- //$item['invite'] = $inviteInfo;
- PRedis::set('markets:activity:book_temp_' . $aid . ':' . $userId . '_' . $inviteId, ['info' => $item, 'inviteInfo' => $inviteInfo], 5 * 86400);
- if ($inviteInfo && $inviteId > 0 && $aid > 0 && $money > 0) {
- if (!UserBalanceLog::checkHasMarketBySource($inviteId, $userId, $aid, 9)) {
- $catchList[] = $item;
- PRedis::set('markets:activity:book_' . $aid . ':' . $userId . '_' . $inviteId, ['info' => $item, 'inviteInfo' => $inviteInfo], 7200);
- Award::marketAward($inviteId, $userId, 9, $money);
- $catchIds[] = $item['id'];
- }
- }
- });
- if ($catchIds) {
- Books::whereIn('id', $catchIds)->update(['is_market' => 1, 'remark' => '分销已结算']);
- }
- $bookList = $bookList ? $bookList->toArray() : [];
- showJson(1005, 1001, ['total' => $bookList['total'], 'bookList' => $bookList['data'], 'catchList' => $catchList]);
- }
- /**
- * 更新报表数据
- * @throws \think\Exception
- * @throws \think\exception\PDOException
- */
- public function updateExport()
- {
- set_time_limit(0);
- $key = input('key', '');
- $checkKey = config('task.updateExport');
- if ($key != $checkKey) {
- showJson(1004, 2009, '', "\n");
- }
- $result = Export::updateData();
- showJson(1005, 1001, ['ids' => $result, 'date' => date('Y-m-d H:i:s')]);
- }
- /**
- * 更新销售资源过期数据
- * @throws \think\Exception
- * @throws \think\exception\PDOException
- */
- public function salesExpired()
- {
- set_time_limit(0);
- $key = input('key', '');
- $checkKey = config('task.salesExpired');
- if ($key != $checkKey) {
- showJson(1004, 2009, '', "\n");
- }
- if(date("H:i") >= '06:00' && date("H:i") <= '23:00'){
- showJson(1004, '非更新时间段', '', "\n");
- }
- $count = 0;
- $datas = [];
- $result = PoolModel::alias('p')
- ->where('p.expire_at', '>', date('Y-m-d H:i:s', time() - 7*86400))
- ->where(['p.type' => 0, 'p.status' => 1])
- ->field('id,sale_uid,user_id,followup_num,follow_time,expire_at,intention')
- ->limit(100)
- ->order('p.stop_time asc,p.expire_at asc')
- ->select()
- ->each(function ($item, $k) use (&$count, &$datas) {
- $userId = isset($item['user_id']) ? $item['user_id'] : 0;
- $saleUid = isset($item['sale_uid']) ? $item['sale_uid'] : 0;
- $expireAt = isset($item['expire_at']) ? $item['expire_at'] : '';
- $followTime = isset($item['follow_time']) ? $item['follow_time'] : '';
- $followTime = $followTime && $followTime != '0000-00-00 00:00:00'? $followTime : '';
- $updateData = ['updated_at' => date('Y-m-d H:i:s'),'stop_time'=> date('Y-m-d H:i:s')];
- // 到期数据处理
- $state = false;
- if($expireAt && $expireAt <= date('Y-m-d H:i:s')){
- $updateData['status'] = 2;
- $data = [
- 'sale_uid' => 0,
- 'user_id' => $userId,
- 'type' => 2,
- 'followup_num' => 0,
- 'create_time' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s'),
- 'expire_at' => 0,
- 'status' => 1,
- ];
- $info = PoolModel::checkData($userId);
- $intention = isset($item['intention'])? $item['intention'] : 0;
- if(!in_array($intention, [5,8])){
- if(in_array($intention, [7])){
- $data['type'] = 8;
- }
- if (empty($info)) {
- $datas[] = $data;
- } else{
- PoolModel::where(['id' => $info['id'], 'user_id' => $userId])->update($data);
- }
- }else{
- $updateData['type'] = 0;
- }
- // 到期未跟进
- if($item['followup_num'] <= 0 || ($followTime && $followTime <date('Y-m-d H:i:s'))){
- $updateData['agency'] = 3;
- $state = true;
- }
- }else if ($expireAt){
- // 今日坠海
- $time = strtotime($expireAt);
- $dayTime = strtotime(date('Y-m-d'));
- //
- if($followTime < date('Y-m-d', $dayTime+ 86400) && $followTime >= date('Y-m-d')){
- $updateData['agency'] = 1;
- $item['expire_text'] = '今日需跟进';
- $state = true;
- }else if($followTime < date('Y-m-d', $dayTime+ 2*86400) && $followTime >= date('Y-m-d', $dayTime + 86400)){
- $updateData['agency'] = 2;
- $item['expire_text'] = '明日需跟进';
- $state = true;
- }else if($followTime < date('Y-m-d')){
- $updateData['agency'] = 3;
- $item['expire_text'] = '到期未跟进';
- $state = true;
- }
- if($time >= time() && $time < $dayTime + 86400){
- $item['expire_text'] = '今天坠海';
- $updateData['agency'] = 4;
- $state = true;
- }else if($time>=$dayTime+86400 && $time < $dayTime + 2 * 86400){
- $item['expire_text'] = '明天坠海';
- $updateData['agency'] = 5;
- $state = true;
- }else if($time>=$dayTime+7*86400 && $time < $dayTime + 8 * 86400){
- $item['expire_text'] = '一礼拜后坠海';
- $updateData['agency'] = 6;
- $state = true;
- }else if ($time >= $dayTime + 8 * 86400){
- $item['expire_text'] = '超过7天';
- $updateData['agency'] = 7;
- $state = true;
- }
- }
- // 更新待办事项
- if($updateData && $state == true){
- $count++;
- PoolModel::where(['id' => $item['id']])->update($updateData);
- }
- });
- if ($datas) {
- PoolModel::insertAll($datas);
- }
- // 缓存
- PRedis::set("caches:sales:expired", ['count'=> $count,'datas'=> $datas,'result'=> $result, 'date'=> date('Y-m-d H:i:s')], 86400);
- showJson(1005, 1008, ['count' => $count, 'date' => date('Y-m-d H:i:s')]);
- }
- }
|