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. \app\weixin\service\Member::clearSignRedHeart('1000077');
  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('b.book_at asc,b.id asc')
  270. ->paginate(50)
  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. 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_' . $userId . '_' . $aid . '_' . $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. }