TaskController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. namespace app\api\controller;
  3. use app\portal\model\UserModel;
  4. use app\weixin\model\AccountLog;
  5. use app\weixin\model\Books;
  6. use app\weixin\model\Devices;
  7. use app\weixin\model\Member;
  8. use app\weixin\model\UserBalanceLog;
  9. use app\weixin\model\UserContactLog;
  10. use app\weixin\service\Award;
  11. use app\weixin\service\PRedis;
  12. use think\Controller;
  13. class TaskController extends Controller
  14. {
  15. /**
  16. * 订单超时处理
  17. * @throws \think\Exception
  18. * @throws \think\exception\PDOException
  19. */
  20. public function catchBook()
  21. {
  22. try {
  23. $key = input('key', '');
  24. $checkKey = config('task.key');
  25. if ($key != $checkKey) {
  26. showJson(1004, 2009, '', "\n");
  27. }
  28. $cancelTime = config('task.orderCancelTime');
  29. $cancelTime = $cancelTime ? $cancelTime * 60 : 30 * 60;
  30. // 未支付
  31. $cancelTime = date('Y-m-d H:i:s', time() - $cancelTime);
  32. $cancelCount = Books::where(['status' => 1])
  33. ->where('created_at', '<=', $cancelTime)
  34. ->count('id');
  35. Books::where(['status' => 1])
  36. ->where('created_at', '<=', $cancelTime)
  37. ->update(['status' => 4, 'remark' => '超时自动取消']);
  38. // 已取消的删除
  39. $deleteTime = date('Y-m-d H:i:s', time() - 3 * 24 * 3600);
  40. $deleteCount = Books::where(['status' => 4])
  41. ->where('created_at', '<=', $deleteTime)
  42. ->count('id');
  43. Books::where(['status' => 4])
  44. ->where('created_at', '<=', $deleteTime)
  45. ->delete();
  46. $msg = "报名订单自动取消已处理成功,累计取消{$cancelCount}个,删除{$deleteCount}个";
  47. return showJson(1005, $msg, '', "\n");
  48. } catch (\Exception $exception) {
  49. return showJson(1004, $exception->getMessage(), '', "\n");
  50. }
  51. }
  52. /**
  53. * 认识申请超时处理
  54. */
  55. public function cancelContact()
  56. {
  57. $key = input('key', '');
  58. $checkKey = config('task.contactKey');
  59. if ($key != $checkKey) {
  60. showJson(1004, 2009, '', "\n");
  61. }
  62. try {
  63. // 申请失效时间
  64. $siteInfo = cmf_get_site_info();
  65. $expire = isset($siteInfo['contact_time']) ? intval($siteInfo['contact_time']) : 0;
  66. $expire = $expire ? $expire : 1;
  67. $dataList = UserContactLog::where(['status' => 1])
  68. ->where('created_at', '<', date('Y-m-d H:i:s', time() - $expire * 24 * 3600))
  69. ->field('id,contact_uid')
  70. ->select();
  71. if (empty($dataList)) {
  72. showJson(1004, 1003, '', "\n");
  73. }
  74. $results = [];
  75. foreach ($dataList as $val) {
  76. $cid = isset($val['id']) ? intval($val['id']) : 0;
  77. $userId = isset($val['contact_uid']) ? intval($val['contact_uid']) : 0;
  78. if ($cid && $userId) {
  79. $res = Member::contactConfirm($userId, $cid, 4);
  80. if (is_array($res)) {
  81. $results[] = $res;
  82. }
  83. }
  84. }
  85. PRedis::set('tasks:contact:' . date('Ymd'), ['datalist' => $dataList, 'result' => $results], 3600);
  86. $msg = "认识申请记录超时处理结果,累计处理" . count($results) . "个";
  87. return showJson(1005, $msg, "\n");
  88. } catch (\Exception $exception) {
  89. return showJson(1004, $exception->getMessage(), '', "\n");
  90. }
  91. }
  92. /**
  93. * 批量推荐队列处理
  94. */
  95. public function catchMakeHearts()
  96. {
  97. set_time_limit(0);
  98. $key = input('key', '');
  99. $checkKey = config('task.heartKey');
  100. if ($key != $checkKey) {
  101. showJson(1004, 2009, '', "\n");
  102. }
  103. try {
  104. $userIds = [];
  105. $queenKey = "queens:hearts:" . date('Ymd');
  106. //echo $queenKey."<br>\n";
  107. for ($i = 0; $i < 500; $i++) {
  108. $userId = PRedis::lpop($queenKey);
  109. //echo $userId."\n";
  110. if ($userId) {
  111. $userIds[] = $userId;
  112. $url = url('/api/task/catchUserHeart', '', '', true);
  113. httpRequest($url, ['uid' => $userId], 'post', '', 2);
  114. }
  115. }
  116. $msg = "更新推荐数据结果,累计处理" . count($userIds) . "个会员数据更新";
  117. return showJson(1005, $msg, "\n");
  118. } catch (\Exception $exception) {
  119. return showJson(1004, $exception->getMessage(), '', "\n");
  120. }
  121. }
  122. /**
  123. * 清除过期签到爱心
  124. */
  125. public function clearSignHeart(){
  126. set_time_limit(0);
  127. $key = input('key', '');
  128. $checkKey = config('task.clearHeartKey');
  129. if ($key != $checkKey) {
  130. showJson(1004, 2009, '', "\n");
  131. }
  132. try {
  133. $month = date('Y-m-01', time() - 2 * 86400);
  134. $users = AccountLog::where(['type'=> 12,'status'=> 2])
  135. ->where('created_at','>=', $month)
  136. ->order('created_at','asc')
  137. ->column('user_id');
  138. $userIds = [];
  139. if($users){
  140. foreach($users as $userId){
  141. // 清除签到爱心
  142. if($userId && \app\weixin\service\Member::clearSignRedHeart($userId)){
  143. $userIds[] = $userId;
  144. }
  145. }
  146. }
  147. $msg = "清除签到爱心数据结果,共".count($users)."个,累计处理" . count($userIds) . "个会员数据更新";
  148. return showJson(1005, $msg, "\n");
  149. } catch (\Exception $exception) {
  150. return showJson(1004, $exception->getMessage(), '', "\n");
  151. }
  152. }
  153. /**
  154. * 处理怦然心动
  155. */
  156. public function catchUserHeart()
  157. {
  158. $uid = input('uid', 0);
  159. $dataList = Member::getHeartList($uid, '', true);
  160. showJson(1005, 1001, $dataList);
  161. }
  162. /**
  163. * 更新怦然心动推荐数据入队处理
  164. */
  165. public function makeHearts()
  166. {
  167. set_time_limit(0);
  168. $key = input('key', '');
  169. $checkKey = config('task.heartKey');
  170. if ($key != $checkKey) {
  171. showJson(1004, 2009, '', "\n");
  172. }
  173. try {
  174. // 查询需要推荐的用户
  175. $dataList = Member::alias('m')
  176. ->join('user_profile up', 'up.userid=m.id', 'left')
  177. ->field('m.openid,m.user_nickname,m.id,up.idcard_check,m.is_reg_profile')
  178. ->where(['m.user_status' => 1, 'm.user_type' => 2, 'up.idcard_check' => 2, 'm.is_reg_profile' => 1])
  179. ->where(function ($query) {
  180. return $query->where('m.heart_recommend_at', '<', date('Y-m-d 19:00:00'))
  181. ->whereOr('m.heart_recommend_at', 'exp', 'is null');
  182. })
  183. ->order('m.id')
  184. ->column('m.id');
  185. if (empty($dataList)) {
  186. showJson(1004, 1003, '', "\n");
  187. }
  188. $sql = Member::getLastSql();
  189. //echo $sql;
  190. // 处理数据更新
  191. $userids = [];
  192. $queenKey = "queens:hearts:" . date('Ymd');
  193. foreach ($dataList as $userId) {
  194. if ($userId && PRedis::rpush($queenKey, $userId)) {
  195. $userids[] = $userId;
  196. }
  197. }
  198. PRedis::expire($queenKey, 2 * 3600);
  199. PRedis::set('tasks:hearts:' . date('Ymd'), ['datalist' => $dataList, 'results' => $userids, 'time' => date('Y-m-d H:i:s'), 'sql' => $sql], 3600);
  200. $msg = "更新推荐数据入队结果,累计处理" . count($userids) . "个会员数据更新";
  201. return showJson(1005, $msg, "\n");
  202. } catch (\Exception $exception) {
  203. return showJson(1004, $exception->getMessage(), '', "\n");
  204. }
  205. }
  206. /**
  207. * 更新隐身
  208. * @throws \think\Exception
  209. * @throws \think\db\exception\DataNotFoundException
  210. * @throws \think\db\exception\ModelNotFoundException
  211. * @throws \think\exception\DbException
  212. * @throws \think\exception\PDOException
  213. */
  214. public function updateHeartStatus()
  215. {
  216. set_time_limit(0);
  217. $key = input('key', '');
  218. $checkKey = config('task.upHeartKey');
  219. if ($key != $checkKey) {
  220. showJson(1004, 2009, '', "\n");
  221. }
  222. $where = ['m.is_heart' => 1, 'm.is_reg_profile' => 1, 'm.user_status' => 1, 'm.user_type' => 2];
  223. $dataList = Member::alias('m')
  224. ->join('user_profile up', 'up.userid=m.id', 'left')
  225. ->where($where)
  226. ->where(function ($query) {
  227. $query->where(db()->raw("up.introduce is NULL or up.introduce = ''"))
  228. ->whereOr(db()->raw("up.family is NULL or up.family = ''"))
  229. ->whereOr(db()->raw("up.hobby is NULL or up.hobby = ''"))
  230. ->whereOr(db()->raw("up.purpose is NULL or up.purpose = ''"))
  231. ->whereOr(db()->raw("up.cause is NULL or up.cause = ''"))
  232. ->whereOr(db()->raw("up.expect is NULL or up.expect = ''"));
  233. })
  234. ->field('m.id,up.introduce,up.family,up.hobby,up.purpose,up.cause,up.expect')
  235. ->select()
  236. ->each(function ($profile, $k) {
  237. if (empty($profile) || (empty($profile['photolist']) || empty($profile['introduce']) || empty($profile['family']) || empty($profile['hobby']) || empty($profile['purpose']) || empty($profile['cause']) || empty($profile['expect']))) {
  238. Member::where(['id' => $profile['id']])->update(['is_heart' => 2, 'remark' => '系统检测自动设置隐身']);
  239. }
  240. });
  241. showJson(1005, 1001, $dataList);
  242. }
  243. /**
  244. * 活动报名分销收益结算
  245. * @throws \think\Exception
  246. * @throws \think\Exception\DbException
  247. * @throws \think\db\exception\DataNotFoundException
  248. * @throws \think\db\exception\ModelNotFoundException
  249. */
  250. public function catchBookMarket()
  251. {
  252. set_time_limit(0);
  253. $key = input('key', '');
  254. $checkKey = config('task.catchBookMarket');
  255. if ($key != $checkKey) {
  256. showJson(1004, 2009, '', "\n");
  257. }
  258. $catchIds = [];
  259. $catchList = [];
  260. $bookList = Books::alias('b')
  261. ->leftJoin('activity a', 'a.id=b.aid')
  262. ->leftJoin('user u', 'u.id=b.uid')
  263. ->where('a.endtime', '<=', time())
  264. // ->where('a.endtime', '>', time()-24*3600*3)
  265. ->where('u.parent_id', '>', 0)
  266. ->where(['a.is_top' => 0, 'a.status' => 1, 'b.status' => 3, 'b.is_market' => 2])
  267. ->field('b.id,b.uid,b.aid,b.money,u.parent_id,b.status')
  268. ->order('a.endtime desc,b.book_at asc,b.id asc')
  269. ->paginate(50)
  270. ->each(function ($item, $k) use (&$catchList, &$catchIds) {
  271. $userId = isset($item['uid']) ? intval($item['uid']) : 0;
  272. $aid = isset($item['aid']) ? $item['aid'] : 0;
  273. $money = isset($item['money']) ? floatval($item['money']) : 0;
  274. $inviteInfo = Member::getInviteInfo($userId);
  275. $inviteId = isset($inviteInfo['invite_id']) ? $inviteInfo['invite_id'] : 0;
  276. $item['invite'] = $inviteInfo;
  277. if ($inviteInfo && $inviteId > 0 && $aid > 0 && $money > 0) {
  278. if (!UserBalanceLog::checkHasMarketBySource($inviteId, $userId, $aid, 9)) {
  279. $catchList[] = $item;
  280. PRedis::set('markets:activity:book_' . $aid . ':' . $userId . '_' . $inviteId, ['info' => $item, 'inviteInfo' => $inviteInfo], 7200);
  281. Award::marketAward($inviteId, $userId, 9, $money);
  282. $catchIds[] = $item['id'];
  283. }
  284. }
  285. });
  286. if($catchIds){
  287. Books::whereIn('id', $catchIds)->update(['is_market'=> 1,'remark'=> '分销已结算']);
  288. }
  289. $bookList = $bookList? $bookList->toArray() : [];
  290. showJson(1005, 1001, ['total'=> $bookList['total'],'bookList' => $bookList['data'], 'catchList' => $catchList]);
  291. }
  292. }