TaskController.php 13 KB

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