TaskController.php 8.7 KB

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