Books.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. <?php
  2. namespace app\weixin\model;
  3. use app\weixin\service\PRedis;
  4. use think\Db;
  5. use think\Model;
  6. class Books extends Model
  7. {
  8. protected $table = 'sg_books';
  9. /**
  10. * 获取活动报名人数
  11. * @param $activityId
  12. * @return float|string
  13. */
  14. public static function getBookCount($activityId, $sex = 0, $status = 3)
  15. {
  16. $where = ['b.aid' => $activityId];
  17. if ($sex) {
  18. $where['u.sex'] = $sex;
  19. }
  20. return Books::alias('b')
  21. ->join('user u', 'u.id=b.uid', 'left')
  22. ->where($where)
  23. ->where('b.id', '>', 0)
  24. ->where(function ($query) use ($status) {
  25. $status = is_array($status) ? $status : [$status];
  26. return $query->where('b.status', 'in', $status);
  27. })
  28. ->count('b.id');
  29. }
  30. /**
  31. * 获取活动报名人数
  32. * @param $activityId
  33. * @return float|string
  34. */
  35. public static function getBookNumBySex($activityId, $sex = 0)
  36. {
  37. $where = ['b.aid' => $activityId];
  38. return Books::alias('b')
  39. ->join('user u', 'u.id=b.uid', 'left')
  40. ->where($where)
  41. ->where('b.id', '>', 0)
  42. ->where(function ($query) use ($sex) {
  43. if ($sex) {
  44. $query->where('b.book_num', 'like', "{$sex}%");
  45. }
  46. $query->whereNotNull('b.book_num')->where('b.status', 3);
  47. })
  48. ->max('b.book_num_val');
  49. }
  50. /**
  51. * 获取活动报名用户列表
  52. * @param $activityId
  53. * @return float|string
  54. */
  55. public static function getBookList($activityId, $pageSize = 20, $field = '', $userId = 0)
  56. {
  57. $userInfo = [];
  58. $field = $field ? $field : 'b.id,b.uid,b.aid,b.book_at,m.user_nickname,m.avatar';
  59. if ($userId) {
  60. $userInfo = Member::getInfo(['id' => $userId], 'id,sex');
  61. }
  62. $dataList = Books::alias('b')
  63. ->join('user m', 'm.id=b.uid', 'left')
  64. ->join('user_profile up', 'up.userid=m.id', 'left')
  65. ->where(['b.aid' => $activityId, 'b.status' => 3])
  66. ->where('b.id', '>', 0)
  67. ->where(function ($query) use ($userInfo) {
  68. $querys = $query;
  69. $userId = isset($userInfo['id']) ? intval($userInfo['id']) : 0;
  70. $sex = isset($userInfo['sex']) ? intval($userInfo['sex']) : 0;
  71. if ($userId) {
  72. $querys = $querys->where('b.uid', 'not in', $userId);
  73. }
  74. if ($sex) {
  75. $sex = $sex == 1 ? 2 : 1;
  76. $querys = $querys->where('m.sex', 'in', [0, $sex]);
  77. }
  78. return $querys;
  79. })
  80. ->field($field)
  81. ->order('b.book_num_val,b.id')
  82. ->paginate($pageSize)
  83. ->each(function ($item, $k) use ($userId) {
  84. $item['avatar'] = isset($item['avatar']) ? cmf_get_image_preview_url($item['avatar']) : '';
  85. if (isset($item['birthday'])) {
  86. $birthday = isset($item['birthday']) ? $item['birthday'] : 0;
  87. $item['birthday_txt'] = $birthday ? date('Y年m月d日', $birthday) : '无';
  88. $item['birthday_day'] = $birthday ? date('y年', $birthday) : '无';
  89. }
  90. $height = isset($item['height']) ? intval($item['height']) : 0;
  91. $item['height_txt'] = $height ? intval($item['height']) . 'CM' : '无';
  92. $item['height'] = $height;
  93. $weight = isset($item['weight']) ? intval($item['weight']) : 0;
  94. $item['weight_txt'] = $weight ? intval($item['weight']) . 'KG' : '无';
  95. $item['weight'] = $weight;
  96. // VIP
  97. $vipAuth = isset($item['vip_auth']) ? intval($item['vip_auth']) : 0;
  98. $vipExpire = isset($item['vip_expire']) ? intval($item['vip_expire']) : 0;
  99. if ($vipAuth && $vipExpire >= time()) {
  100. $item['vip_auth'] = 1;
  101. $item['vip_expire'] = date('Y-m-d', $item['vip_expire']);
  102. } else {
  103. $item['vip_auth'] = 0;
  104. $item['vip_expire'] = '';
  105. }
  106. // 学历
  107. $educations = config('weixin.educations');
  108. $edu = isset($item['education']) ? $item['education'] : 0;
  109. $item['education_txt'] = $edu && isset($educations[$edu]) ? $educations[$edu] : '无';
  110. $item['education'] = $edu;
  111. // 地址
  112. $province = isset($item['province']) ? trim($item['province']) : '';
  113. $city = isset($item['city']) ? trim($item['city']) : '';
  114. $item['now_address'] = trim($province . ' ' . $city);
  115. $homeProvince = isset($item['home_province']) ? trim($item['home_province']) : '';
  116. $homeCity = isset($item['home_city']) ? trim($item['home_city']) : '';
  117. $item['home_address'] = trim($homeProvince . ' ' . $homeCity);
  118. if ($userId) {
  119. $item['is_select'] = 0;
  120. $item['select_num'] = '';
  121. $uid = isset($item['uid']) ? intval($item['uid']) : 0;
  122. $heartUids = HeartMatch::getHeartUids($item['aid'], $userId);
  123. if ($heartUids && $uid && in_array($uid, $heartUids)) {
  124. $item['is_select'] = 1;
  125. $item['select_num'] = array_search($uid, $heartUids) + 1;
  126. }
  127. }
  128. return $item;
  129. });
  130. return $dataList ? $dataList->toArray() : [];
  131. }
  132. /**
  133. * 获取推送用户列表
  134. * @param $activityId 活动ID
  135. * @param string $field 字段
  136. * @return array
  137. */
  138. public static function getPushUserList($activityId, $field = '')
  139. {
  140. $field = $field ? $field : 'b.id,b.uid,b.aid,m.openid,m.user_nickname';
  141. $dataList = Books::alias('b')
  142. ->join('user m', 'm.id=b.uid', 'left')
  143. ->where(['b.aid' => $activityId, 'b.status' => 3])
  144. ->field($field)
  145. ->select();
  146. return $dataList ? $dataList->toArray() : [];
  147. }
  148. /**
  149. * 获取报名信息
  150. * @param $where 条件
  151. * @param string $field 字段
  152. * @return array
  153. */
  154. public static function getInfo($where, $field = '')
  155. {
  156. $field = $field ? $field : 'id,uid,aid,money,credit,pay_type,book_num,order_sn,prepay_id,status';
  157. $info = Books::where($where)->where('id', '>', 0)->field($field)->find();
  158. return $info ? $info->toArray() : [];
  159. }
  160. /**
  161. * 更新报名数据
  162. * @param $where
  163. * @param $data
  164. * @return bool|int|string
  165. */
  166. public static function saveData($where, $data)
  167. {
  168. if (!is_array($data)) {
  169. return false;
  170. }
  171. $data['updated_at'] = date('Y-m-d H:i:s');
  172. return Books::where($where)->update($data);
  173. }
  174. /**
  175. * 验证用户是否已经报名该活动
  176. * @param $userId 用户ID
  177. * @param $activityId 活动ID
  178. * @return mixed
  179. */
  180. public static function checkUserBook($userId, $activityId, $status = 3)
  181. {
  182. return Books::where(['uid' => $userId, 'aid' => $activityId, 'status' => $status])->value('id');
  183. }
  184. /**
  185. * 生成报名编号
  186. * @param $activityId
  187. * @return mixed
  188. */
  189. public static function makeBookNo($activityId, $prefix = '', $sex = 0)
  190. {
  191. $num = self::getBookNumBySex($activityId, $prefix);
  192. $num = $num ? $num : 0;
  193. $bookNo = makeNo($num + 1, $prefix);
  194. return $bookNo;
  195. }
  196. /**
  197. * 获取选我的人列表
  198. * @param $activityId 活动ID
  199. * @param $userId 当前用户
  200. * @param int $pageSize 分页大小
  201. * @param string $field 字段
  202. * @return array
  203. */
  204. public static function getMatchList($activityId, $userId, $pageSize = 10, $field = '')
  205. {
  206. $userInfo = [];
  207. $field = $field ? $field : "hm.id,hm.aid,hm.uid,hm.heart_uids,m.user_nickname,m.avatar,m.sex,m.birthday,m.real_name,up.age,up.height,up.weight,m.vip_auth,m.vip_expire,up.company,up.occupation,up.graduate,up.education,up.province,up.city,up.home_province,up.home_city,up.show_company,up.show_graduate,up.idcard_check,up.education_check,up.position_check,find_in_set('{$userId}', hm.`heart_uids`) as choose_rank";
  208. if ($userId) {
  209. $userInfo = Member::getInfo(['id' => $userId], 'id,sex');
  210. }
  211. $dataList = HeartMatch::alias('hm')
  212. ->join('user m', 'm.id=hm.uid', 'left')
  213. ->join('user_profile up', 'up.userid=m.id', 'left')
  214. ->where(['hm.aid' => $activityId])
  215. ->where("find_in_set('{$userId}', hm.`heart_uids`)")
  216. ->where('hm.status', 'in', [1, 2, 3])
  217. ->where('hm.id', '>', 0)
  218. ->where(function ($query) use ($userInfo) {
  219. $querys = $query;
  220. $userId = isset($userInfo['id']) ? intval($userInfo['id']) : 0;
  221. $sex = isset($userInfo['sex']) ? intval($userInfo['sex']) : 0;
  222. if ($userId) {
  223. $querys = $querys->where('hm.uid', 'not in', $userId);
  224. }
  225. if ($sex) {
  226. $sex = $sex == 1 ? 2 : 1;
  227. $querys = $querys->where('m.sex', 'in', [0, $sex]);
  228. }
  229. return $querys;
  230. })
  231. ->field($field)
  232. ->order('hm.created_at desc')
  233. ->paginate($pageSize)
  234. ->each(function ($item, $k) {
  235. $item['avatar'] = isset($item['avatar']) ? cmf_get_image_preview_url($item['avatar']) : '';
  236. if (isset($item['birthday'])) {
  237. $birthday = isset($item['birthday']) ? $item['birthday'] : 0;
  238. $item['birthday_txt'] = $birthday ? date('Y年m月d日', $birthday) : '无';
  239. $item['birthday_day'] = $birthday ? date('y年', $birthday) : '无';
  240. }
  241. $height = isset($item['height']) ? intval($item['height']) : 0;
  242. $item['height_txt'] = $height ? intval($item['height']) . 'CM' : '无';
  243. $item['height'] = $height;
  244. $weight = isset($item['weight']) ? intval($item['weight']) : 0;
  245. $item['weight_txt'] = $weight ? intval($item['weight']) . 'KG' : '无';
  246. $item['weight'] = $weight;
  247. // VIP
  248. $vipAuth = isset($item['vip_auth']) ? intval($item['vip_auth']) : 0;
  249. $vipExpire = isset($item['vip_expire']) ? intval($item['vip_expire']) : 0;
  250. if ($vipAuth && $vipExpire >= time()) {
  251. $item['vip_auth'] = 1;
  252. $item['vip_expire'] = date('Y-m-d', $item['vip_expire']);
  253. } else {
  254. $item['vip_auth'] = 0;
  255. $item['vip_expire'] = '';
  256. }
  257. // 学历
  258. $educations = config('weixin.educations');
  259. $edu = isset($item['education']) ? $item['education'] : 0;
  260. $item['education_txt'] = isset($educations[$edu]) ? $educations[$edu] : '无';
  261. $item['education'] = $edu;
  262. // 地址
  263. $province = isset($item['province']) ? trim($item['province']) : '';
  264. $city = isset($item['city']) ? trim($item['city']) : '';
  265. $item['now_address'] = trim($province . ' ' . $city);
  266. $homeProvince = isset($item['home_province']) ? trim($item['home_province']) : '';
  267. $homeCity = isset($item['home_city']) ? trim($item['home_city']) : '';
  268. $item['home_address'] = trim($homeProvince . ' ' . $homeCity);
  269. $item['book_no'] = Books::where(['uid' => $item['uid'], 'aid' => $item['aid'], 'status' => 3])
  270. ->value('book_num');
  271. return $item;
  272. });
  273. return $dataList ? $dataList->toArray() : [];
  274. }
  275. /**
  276. * 获取待匹配的用户列表
  277. * @param $activityId 活动ID
  278. * @param string $field 字段
  279. * @return array
  280. */
  281. public static function getMatchUserList($activityId, $field = '')
  282. {
  283. $field = $field ? $field : "hm.id,hm.aid,hm.uid,b.book_num as book_no,hm.heart_uids,m.openid,m.sex,m.user_nickname,m.real_name,m.mobile,up.wechat_code,b.book_num as book_no";
  284. $dataList = HeartMatch::alias('hm')
  285. ->join('user m', 'm.id=hm.uid', 'left')
  286. ->join('user_profile up', 'up.userid=m.id', 'left')
  287. ->join('books b', 'b.uid=m.id and b.aid=hm.aid', 'left')
  288. ->where(['hm.aid' => $activityId, 'b.status' => 3, 'hm.status' => 1])
  289. ->where('hm.id', '>', 0)
  290. // ->field($field)
  291. ->order('hm.id')
  292. ->column($field, 'hm.uid');
  293. // ->select();
  294. return $dataList;
  295. }
  296. /**
  297. * 设置匹配用户
  298. * @param $activityId
  299. * @param $userId
  300. * @param $uids
  301. * @return array|bool|int
  302. */
  303. public static function setMatchUser($activityId, $userId, $uids)
  304. {
  305. $siteInfo = $siteInfo = cmf_get_site_info();
  306. $chooseNum = isset($siteInfo['choose_num']) ? intval($siteInfo['choose_num']) : 0;
  307. $contactType = isset($siteInfo['contact_type']) ? $siteInfo['contact_type'] : 1;
  308. $chooseNum = $chooseNum ? $chooseNum : 3;
  309. $uids = is_array($uids) ? $uids : explode(',', $uids);
  310. if (count($uids) > $chooseNum) {
  311. return lang('choose_limit', ['num' => $chooseNum]);
  312. }
  313. if (count($uids) <= 0) {
  314. return 6008;
  315. }
  316. // 验证活动
  317. $activityInfo = Activity::where(['id' => $activityId, 'status' => 1])
  318. ->field('id,title,starttime,is_match')
  319. ->find();
  320. if (empty($activityInfo)) {
  321. return 5001;
  322. }
  323. // 验证匹配状态
  324. $isMatch = isset($activityInfo['is_match']) ? intval($activityInfo['is_match']) : 0;
  325. if ($isMatch == 1) {
  326. return 6002;
  327. }
  328. // 验证是否签到
  329. $isSignin = Books::where(['aid' => $activityId, 'uid' => $userId, 'status' => 3])->value('is_signin');
  330. if (!in_array($isSignin, [2, 3])) {
  331. return 5023;
  332. }
  333. $where = ['aid' => $activityId, 'uid' => $userId];
  334. $data = [
  335. 'aid' => $activityId,
  336. 'uid' => $userId,
  337. 'heart_uids' => is_array($uids) ? implode(',', $uids) : $uids,
  338. 'created_at' => date('Y-m-d H:i:s'),
  339. 'status' => 1,
  340. ];
  341. $matchData = HeartMatch::where($where)->field('id,heart_uids,status')->find();
  342. $status = isset($matchData['status']) ? intval($matchData['status']) : 0;
  343. // 若开启互选模式则直接匹配
  344. if ($contactType == 3) {
  345. // 是否设置过互选
  346. $matchUids = isset($matchData['heart_uids']) ? $matchData['heart_uids'] : '';
  347. if ($matchUids) {
  348. return 6010;
  349. }
  350. if (empty($matchData) && !HeartMatch::insertGetId($data)) {
  351. return 6007;
  352. }else if($matchData){
  353. HeartMatch::where($where)->update(['heart_uids' => is_array($uids) ? implode(',', $uids) : $uids]);
  354. }
  355. // 处理互选
  356. $res = Books::setMutualSelect($userId, $uids, $activityInfo);
  357. } else {
  358. if ($matchData) {
  359. // 未匹配
  360. if ($status == 1 || $status == 4) {
  361. $res = HeartMatch::where($where)->update($data);
  362. } else {
  363. return 6009;
  364. }
  365. } else {
  366. $res = HeartMatch::insertGetId($data);
  367. }
  368. }
  369. // 操作日志
  370. UserLog::saveLog(['user_id' => $userId, 'type' => 4, 'content' => "设置互选匹配用户"]);
  371. return ['id' => $res];
  372. }
  373. /**
  374. * 处理互选
  375. * @param $userId 当前互选用户
  376. * @param $uids 选择的用户ID
  377. * @param $activityInfo 活动基础信息
  378. * @return array|bool
  379. * @throws \think\Exception
  380. * @throws \think\db\exception\DataNotFoundException
  381. * @throws \think\db\exception\ModelNotFoundException
  382. * @throws \think\exception\DbException
  383. * @throws \think\exception\PDOException
  384. */
  385. private static function setMutualSelect($userId, $uids, $activityInfo)
  386. {
  387. $uids = !is_array($uids) && $uids ? [$uids] : $uids;
  388. $activityId = isset($activityInfo['id']) ? intval($activityInfo['id']) : 0;
  389. if (empty($activityId) || empty($uids)) {
  390. return false;
  391. }
  392. $field = 'm.id as uid,m.real_name,m.openid,m.sex,m.user_nickname,up.wechat_code,hm.uid as muid,hm.match_uid,hm.heart_uids,b.book_num';
  393. $matchUsers = HeartMatch::alias('hm')
  394. ->leftJoin('user m', 'm.id=hm.uid')
  395. ->leftJoin('user_profile up', 'up.userid=hm.uid')
  396. ->leftJoin('books b', 'b.uid=hm.uid and b.aid=hm.aid')
  397. ->field($field)
  398. ->where('hm.uid', 'in', $uids)
  399. ->where(['hm.aid' => $activityId, 'b.status' => 3])
  400. ->where('find_in_set(:uid,hm.heart_uids)', ['uid' => $userId])
  401. ->where('hm.status', '<', 4)
  402. ->select();
  403. // 如果不存在互选用户
  404. $matchUsers = $matchUsers? $matchUsers->toArray() : [];
  405. if (empty($matchUsers)) {
  406. return false;
  407. }
  408. $memberInfo = Member::alias('m')
  409. ->leftJoin('user_profile up', 'up.userid=m.id')
  410. ->leftJoin('books b', 'b.uid=m.id')
  411. ->field('m.id,m.openid,m.real_name,m.sex,m.user_nickname,up.wechat_code,b.book_num')
  412. ->where(['m.id' => $userId, 'm.user_status' => 1, 'b.aid' => $activityId, 'b.status' => 3])
  413. ->find();
  414. if (empty($memberInfo)) {
  415. return false;
  416. }
  417. // 进行互选处理
  418. $matchUids = [];
  419. $matchAt = time();
  420. $title = isset($activityInfo['title']) ? trim($activityInfo['title']) : '无';
  421. $matchTime = $matchAt ? date('Y.m.d H:i', $matchAt) : date('Y.m.d H:i', time());
  422. $openid = isset($memberInfo['openid']) ? trim($memberInfo['openid']) : '';
  423. $bookNo = isset($memberInfo['book_num']) ? trim($memberInfo['book_num']) : '无';
  424. $nickname = isset($memberInfo['user_nickname']) ? $memberInfo['user_nickname'] : '';
  425. $wechatCode = isset($memberInfo['wechat_code']) ? trim($memberInfo['wechat_code']) : '无';
  426. $sex = isset($memberInfo['sex']) ? intval($memberInfo['sex']) : 0;
  427. $sex = $sex == 1 ? '男' : '女';
  428. PRedis::set('matchs:activity_' . $activityId.':users_'.$userId, ['data' => $memberInfo, 'matchs' => $matchUsers], 600);
  429. foreach ($matchUsers as $matchData) {
  430. $uids = isset($matchData['match_uid']) && $matchData['match_uid'] ? explode(',', $matchData['match_uid']) : [];
  431. $matchUid = isset($matchData['muid']) ? intval($matchData['muid']) : 0;
  432. $matchName = isset($matchData['user_nickname']) ? $matchData['user_nickname'] : '';
  433. $matchNo = isset($matchData['book_num']) ? trim($matchData['book_num']) : '无';
  434. $matchWechatCode = isset($matchData['wechat_code']) ? trim($matchData['wechat_code']) : '无';
  435. $matchSex = isset($matchData['sex']) ? intval($matchData['sex']) : 0;
  436. $matchSex = $matchSex == 1 ? '男' : '女';
  437. // 通知处理
  438. if (empty($matchUid)) {
  439. continue;
  440. }
  441. /*if (($uids && in_array($userId, $uids)) || ($matchUids && in_array($matchUid, $matchUids))) {
  442. continue;
  443. }*/
  444. // 更新被匹配用户数据
  445. $uids[] = $userId;
  446. $uids = array_unique($uids);
  447. $matchDate = date('Y-m-d H:i:s', $matchAt);
  448. $errorKey = 'matchs:activity_' . $activityId . ':error:u_' . $userId . '_' . $matchUid;
  449. $updateData = ['match_uid' => implode(',', $uids), 'match_at' => $matchDate, 'status' => 2];
  450. if (empty($uids) || !HeartMatch::where(['aid' => $activityId, 'uid' => $matchUid])->update($updateData)) {
  451. PRedis::set($errorKey.'_1', ['data' => $updateData, 'match' => $matchData], 600);
  452. continue;
  453. }
  454. // 更新互选用户数据
  455. $matchUids[] = $matchUid;
  456. $matchUids = array_unique($matchUids);
  457. $updateData = ['match_uid' => implode(',', $matchUids), 'match_at' => $matchDate, 'status' => 2];
  458. if (empty($matchUids) || !HeartMatch::where(['aid' => $activityId, 'uid' => $userId])->update($updateData)) {
  459. PRedis::set($errorKey.'_2', ['data' => $updateData, 'match' => $matchData], 600);
  460. continue;
  461. }
  462. // 匹配消息通知
  463. if ($openid) {
  464. $params = [
  465. 'title' => "恭喜!活动缘分互选匹配成功,已为您匹配到缘分用户!\n\n活动主题:\t{$title}\n\n您的编号:\t{$bookNo}\n\n对方编号:\t{$matchNo}\n\n匹配时间:\t{$matchTime}\n\n对方微信号:\t{$matchWechatCode}",
  466. 'remark' => "感谢您的使用,点击详情查看互选信息",
  467. 'type' => 'match',
  468. 'keywords' => [
  469. 'name' => [
  470. 'value' => $matchName . '(昵称)',
  471. 'color' => '#173177',
  472. ],
  473. 'sex' => [
  474. 'value' => $matchSex,
  475. 'color' => '#173177',
  476. ],
  477. 'tel' => [
  478. 'value' => '隐私信息不显示',
  479. 'color' => '#173177',
  480. ],
  481. ],
  482. 'url' => url('/weixin/activity/match?id=' . $activityId . '&type=2', '', '', true),
  483. ];
  484. PRedis::set('matchs:activity_' . $activityId . ':message:' . $userId.'_'.$matchUid, ['info' => $memberInfo, 'match'=> $matchData, 'params' => $params], 600);
  485. Wechat::sendTplMsg($openid, $params);
  486. }
  487. // 通知被匹配用户
  488. $matchOpenId = isset($matchData['openid']) ? trim($matchData['openid']) : '';
  489. if ($matchOpenId) {
  490. $params = [
  491. 'title' => "恭喜!活动缘分互选匹配成功,已为您匹配到缘分用户!\n\n活动主题:\t{$title}\n\n您的编号:\t{$matchNo}\n\n对方编号:\t{$bookNo}\n\n匹配时间:\t{$matchTime}\n\n对方微信号:\t{$wechatCode}",
  492. 'remark' => "感谢您的使用,点击详情查看互选信息",
  493. 'type' => 'match',
  494. 'keywords' => [
  495. 'name' => [
  496. 'value' => $nickname . '(昵称)',
  497. 'color' => '#173177',
  498. ],
  499. 'sex' => [
  500. 'value' => $sex,
  501. 'color' => '#173177',
  502. ],
  503. 'tel' => [
  504. 'value' => '隐私信息不显示',
  505. 'color' => '#173177',
  506. ],
  507. ],
  508. 'url' => url('/weixin/activity/match?id=' . $activityId . '&type=2', '', '', true),
  509. ];
  510. PRedis::set('matchs:activity_' . $activityId . ':message:' . $matchUid.'_'.$userId, ['info' => $matchData, 'match'=> $memberInfo, 'params' => $params], 600);
  511. Wechat::sendTplMsg($matchOpenId, $params);
  512. }
  513. }
  514. return ['matchUsers' => $matchUsers, 'matchUids' => $matchUids];
  515. }
  516. /**
  517. * 获取匹配用户
  518. * @param $aid 活动ID
  519. * @param $userId 当前用户ID
  520. * @param $heartUids 当前用户已选的人ID,小写都好分隔字符串
  521. * @param int $sex
  522. * @return array
  523. */
  524. public static function matchUser($activityId, $userId, $heartUids, $sex = 0, $round = 1)
  525. {
  526. $field = "hm.id,hm.uid,b.book_num as book_no,m.sex,m.user_nickname,m.real_name,m.mobile,up.wechat_code,find_in_set('{$userId}', hm.`heart_uids`) as choose_rank,hm.created_at";
  527. $info = HeartMatch::alias('hm')
  528. ->join('user m', 'm.id=hm.uid', 'left')
  529. ->join('user_profile up', 'up.userid=m.id', 'left')
  530. ->join('books b', 'b.uid=m.id and b.aid=hm.aid', 'left')
  531. ->where(['hm.aid' => $activityId, 'b.status' => 3, 'hm.status' => 1])
  532. ->where('hm.uid', 'in', $heartUids)
  533. ->where(function ($query) use ($sex) {
  534. if ($sex) {
  535. $sex = $sex == 1 ? 2 : 1;
  536. return $query->where('m.sex', 'in', [0, $sex]);
  537. }
  538. })
  539. ->where(db()->raw("find_in_set('{$userId}', hm.`heart_uids`) = {$round}"))
  540. ->where('hm.id', '>', 0)
  541. ->field($field)
  542. ->order("hm.id")
  543. ->limit(1)
  544. ->find();
  545. // PRedis::set('test:sql:'.$userId.'_'.$aid, HeartMatch::getLastSql(), 600);
  546. return $info ? $info->toArray() : [];
  547. }
  548. }