TaskController.php 11 KB

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