CheckService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. namespace App\Services\Pair;
  3. use App\Models\Fpdx\PairModel;
  4. use App\Models\User\UserModel;
  5. use Illuminate\Support\Facades\Redis;
  6. class CheckService
  7. {
  8. public $id;
  9. public $user;
  10. public function baocun($stage_id)
  11. {
  12. // 开始钉钉通知
  13. // \Curl::to("http://api.deep.fenpeiduixiang.com/api/ding/pairsave/begin/{$stage_id}")->get();
  14. Redis::Del('pair:data');
  15. $pairs = PairModel::where('stage_id', $stage_id)->whereNotNull('assoc_id')->get();
  16. foreach ($pairs as $pair) {
  17. try {
  18. $this->id = $pair->id;
  19. $user = $this->getUser($this->getPair()->uid);
  20. $other = $this->getUser($this->getOtherPair()->uid);
  21. Redis::Zadd('pair:data', $pair->id, json_encode([
  22. 'user' => $user,
  23. 'other' => $other,
  24. 'pair' => $pair,
  25. ]));
  26. } catch (\Exception $exception) {
  27. }
  28. }
  29. // 结束钉钉通知
  30. if (app()->environment() == 'production') {
  31. \Curl::to("http://api.deep.fenpeiduixiang.com/api/ding/pairsave/end/{$stage_id}")->get();
  32. }
  33. }
  34. //
  35. public function getUser($uid)
  36. {
  37. $user = UserModel::selectRaw(" `uid`,
  38. `uid`,
  39. `sex`,
  40. `sxo`,
  41. `age`,
  42. `height`,
  43. YEAR(`age`) as `year`,
  44. YEAR(`age`) - `pair_max_age` as `max_year`,
  45. YEAR(`age`)+ `pair_min_age` as `min_year`,
  46. `pair_min_age`,
  47. `pair_max_age`,
  48. `fpdx_like`,
  49. `pair_province`,
  50. `pair_all`,
  51. `home`,
  52. `address`,
  53. `school`
  54. ")->find($uid);
  55. @list($user->province, $user->city) = explode('-', $user->address);
  56. $user->fpdx_like = explode(',', $user->fpdx_like);
  57. return $user;
  58. }
  59. public function getPair()
  60. {
  61. return PairModel::find($this->id);
  62. }
  63. public function getOtherPair()
  64. {
  65. $pair = $this->getPair();
  66. return PairModel::where('assoc_id', $pair->id)->first();
  67. }
  68. /**
  69. * @param $stage_id
  70. * @return array
  71. * 1 => '年龄必须比我大',
  72. * 2 => '年龄必须比我小',
  73. * 3 => '身高必须比我高',
  74. * 4 => '身高必须比我矮',
  75. * 5 => '必须是同校',
  76. * 6 => '必须同城',
  77. * 7 => '必须是不同校',
  78. * 8 => '接受同省调剂',
  79. * 9 => '必须是同乡'
  80. */
  81. public function check($stage_id)
  82. {
  83. // 开始钉钉通知
  84. if (app()->environment() == 'production') {
  85. \Curl::to("http://api.deep.fenpeiduixiang.com/api/ding/paircheck/begin/{$stage_id}")->get();
  86. }
  87. $errors = array();
  88. $pairs = Redis::ZRANGE('pair:data', 0, -1, 'WITHSCORES');
  89. foreach ($pairs as $key => $id) {
  90. $data = json_decode($key);
  91. $user = $data->user;
  92. $other = $data->other;
  93. $pair = $data->pair;
  94. $message = $this->where($user, $other, $pair);
  95. if ($message) {
  96. array_push($errors, $pair->id . $message);
  97. }
  98. }
  99. // 结束钉钉通知
  100. if (app()->environment() == 'production') {
  101. \Curl::to("http://api.deep.fenpeiduixiang.com/api/ding/paircheck/end/{$stage_id}")->withData([
  102. 'errors' => $errors,
  103. ])->post();
  104. }
  105. return $errors;
  106. }
  107. public function where($user, $other, $pair)
  108. {
  109. $message = '';
  110. if (!$this->isSex($user, $other)) {
  111. $message .= "对方性取向不满足\t";
  112. }
  113. if (!$this->isSxo($user, $other)) {
  114. $message .= "我的性取向不满足\t";
  115. }
  116. if (!$this->isAgeDiff($user, $other)) {
  117. $message .= "年龄不在区间内\t";
  118. }
  119. if ($this->isRepeat($user, $other, $pair)) {
  120. $message .= "以前匹配过\t";
  121. }
  122. ## 不允许全国调剂
  123. if (!$user->pair_all) {
  124. ## 允许全国调剂
  125. if ($user->pair_province) {
  126. foreach ($user->fpdx_like as $type) {
  127. switch ($type) {
  128. case '5':
  129. if ($user->school != $other->school) {
  130. if (!$this->isProvince($user, $other)) {
  131. $message .= "没有跟我同校,";
  132. $message .= "也没有跟我同省\t";
  133. }
  134. }
  135. break;
  136. case '6':
  137. if (!$this->isCity($user, $other)) {
  138. if (!$this->isProvince($user, $other)) {
  139. $message .= "没有跟我同市,";
  140. $message .= "也没有跟我同省\t";
  141. }
  142. }
  143. break;
  144. case '7':
  145. if ($user->school == $other->school) {
  146. if (!$this->isProvince($user, $other)) {
  147. $message .= "必须跟我不同校,";
  148. $message .= "也没有跟我同省\t";
  149. }
  150. }
  151. break;
  152. case '8':
  153. if (!$this->isProvince($user, $other)) {
  154. $message .= "没有跟我同省\t";
  155. }
  156. break;
  157. }
  158. }
  159. } else {
  160. foreach ($user->fpdx_like as $type) {
  161. switch ($type) {
  162. case '5':
  163. if ($user->school != $other->school) {
  164. $message .= "没有跟我同校,";
  165. }
  166. break;
  167. case '6':
  168. if (!$this->isCity($user, $other)) {
  169. $message .= "没有跟我同市,";
  170. }
  171. break;
  172. case '7':
  173. if ($user->school == $other->school) {
  174. $message .= "必须跟我不同校,";
  175. }
  176. break;
  177. case '8':
  178. if (!$this->isProvince($user, $other)) {
  179. $message .= "没有跟我同省\t";
  180. }
  181. break;
  182. case '9':
  183. if (!$this->isHome($user, $other)) {
  184. $message .= "没有跟我同乡";
  185. }
  186. break;
  187. }
  188. }
  189. }
  190. }
  191. return $message;
  192. }
  193. public function isSex($user, $other)
  194. {
  195. return ($user->sex == $other->sxo) ? true : false;
  196. }
  197. public function isSxo($user, $other)
  198. {
  199. if ($user->sxo == $other->sex) {
  200. return true;
  201. } else {
  202. return false;
  203. }
  204. }
  205. public function isAgeDiff($user, $other)
  206. {
  207. $max = date('Y', strtotime($user->age)) + $user->pair_min_age;
  208. $min = date('Y', strtotime($user->age)) - $user->pair_max_age;
  209. $age = date('Y', strtotime($other->age));
  210. if ($min <= $age && $age <= $max) {
  211. return true;
  212. } else {
  213. return false;
  214. }
  215. }
  216. public function isRepeat($user, $other, $pair)
  217. {
  218. $array = PairModel::whereRaw("id IN (
  219. SELECT `assoc_id`
  220. FROM `koudai`.`kdgx_partner_charge_pair`
  221. WHERE `uid`= ?
  222. and `assoc_id` IS NOT NULL
  223. and `stage_id`< ?)", [$user->uid, $pair->stage_id])->pluck('uid')->toArray();
  224. if (in_array($other->uid, $array)) {
  225. return true;
  226. } else {
  227. return false;
  228. }
  229. }
  230. //年龄比我大
  231. public function isProvince($user, $other)
  232. {
  233. if ($user->province == $other->province) {
  234. return true;
  235. } else {
  236. return false;
  237. }
  238. }
  239. //年龄比我小
  240. public function isCity($user, $other)
  241. {
  242. if ($user->city == $other->city) {
  243. return true;
  244. } else {
  245. return false;
  246. }
  247. }
  248. //身高比我高
  249. public function isHome($user, $other)
  250. {
  251. if ($user->home == $other->home) {
  252. return true;
  253. } else {
  254. return false;
  255. }
  256. }
  257. //身高比我矮
  258. public function luodan($pair)
  259. {
  260. $user = $this->getUser($pair->uid);
  261. $unlikes = Redis::SMEMBERS('fpdx_pairs_' . $user->uid);
  262. \DB::connection()->enableQueryLog(); // 开启查询日志
  263. $others = UserModel::selectRaw("`uid`,
  264. `sex`,
  265. `sxo`,
  266. `age`,
  267. `height`,
  268. YEAR(`age`) as `year`,
  269. YEAR(`age`) - `pair_max_age` as `max_year`,
  270. YEAR(`age`)+ `pair_min_age` as `min_year`,
  271. `pair_min_age`, `pair_max_age`,
  272. `fpdx_like`,
  273. `pair_province`,
  274. `pair_all`,
  275. `address`,
  276. `home`,
  277. `school`")
  278. ->where('sex', $user->sxo)
  279. ->where('sxo', $user->sex)
  280. ->when($user->sex == 1 && $user->sxo == 2, function ($query) use ($user) {
  281. return $query->where('height', '<', $user->height - 5);
  282. })
  283. ->when($user->sex == 2 && $user->sxo == 1, function ($query) use ($user) {
  284. return $query->where('height', '>', $user->height + 5);
  285. })
  286. ->whereRaw(" YEAR(`age`) BETWEEN ? AND ?", [$user->max_year, $user->min_year])
  287. ->whereRaw("? BETWEEN YEAR(`age`) - `pair_max_age` AND YEAR(`age`)+ `pair_min_age`", [$user->year])
  288. ->when(in_array('1', $user->fpdx_like), function ($query) use ($user) {
  289. return $query->whereRaw("YEAR(`age`) <= ?", $user->year);
  290. })
  291. // 年龄必须比我小; 年龄差距最大4岁
  292. ->when(in_array('2', $user->fpdx_like), function ($query) use ($user) {
  293. return $query->whereRaw("YEAR(`age`) >= ?", $user->year);
  294. })
  295. ->when(in_array('3', $user->fpdx_like), function ($query) use ($user) {
  296. return $query->where('height', '>=', $user->height);
  297. })
  298. ->when(in_array('4', $user->fpdx_like), function ($query) use ($user) {
  299. return $query->where('height', '<=', $user->height);
  300. })
  301. ->when(in_array('5', $user->fpdx_like), function ($query) use ($user) {
  302. return $query->where('school', $user->school);
  303. })
  304. ->when(in_array('6', $user->fpdx_like), function ($query) use ($user) {
  305. return $query->where('address', $user->address);
  306. })
  307. ->when(in_array('7', $user->fpdx_like), function ($query) use ($user) {
  308. return $query->where('school', '<>', $user->school);
  309. })
  310. ->when(in_array('8', $user->fpdx_like), function ($query) use ($user) {
  311. return $query->where('address', 'like', $user->province . "-%");
  312. })
  313. ->when(in_array('9', $user->fpdx_like), function ($query) use ($user) {
  314. return $query->where('home', $user->home);
  315. })
  316. ->whereIn(
  317. 'uid',
  318. PairModel::where('stage_id', $pair->stage_id)
  319. ->whereNull('assoc_id')
  320. ->where('state', '>', 100)
  321. ->where('uid', '<>', $user->uid)
  322. ->whereNotIn('uid', $unlikes)
  323. ->pluck('uid')
  324. )
  325. ->get();
  326. dump($others->implode('uid', ','));
  327. foreach ($others as $other) {
  328. $other = $this->getUser($other->uid);
  329. if (in_array(5, $other->fpdx_like)) {
  330. if ($user->school != $other->school) {
  331. dump("{$other->uid}[学校]必须一致");
  332. continue;
  333. }
  334. }
  335. if (in_array(6, $other->fpdx_like)) {
  336. if ($user->city != $other->city) {
  337. dump("{$other->uid}[城市]必须一致");
  338. continue;
  339. }
  340. }
  341. if (in_array(7, $other->fpdx_like)) {
  342. if ($user->school == $other->school) {
  343. dump("{$other->uid}[学校]必须不一致");
  344. continue;
  345. }
  346. }
  347. if (in_array(9, $other->fpdx_like)) {
  348. if ($user->home != $other->home) {
  349. dump("{$other->uid}[家乡]必须一致");
  350. continue;
  351. }
  352. }
  353. if ($other->pair_province || in_array(8, $other->fpdx_like)) {
  354. if ($user->province != $other->province) {
  355. dump("{$other->uid}[省]必须一致");
  356. continue;
  357. }
  358. }
  359. dump("{$other->uid}条件全部满足");
  360. }
  361. }
  362. //跟我同校
  363. public function isAgeBigger($user, $other)
  364. {
  365. if ((int)($user->age) >= (int)($other->age)) {
  366. return true;
  367. } else {
  368. return false;
  369. }
  370. }
  371. public function isAgeSmaller($user, $other)
  372. {
  373. if ((int)($user->age) <= (int)($other->age)) {
  374. return true;
  375. } else {
  376. return false;
  377. }
  378. }
  379. //跟我同省
  380. public function isHeightHigher($user, $other)
  381. {
  382. if ($user->height <= $other->height) {
  383. return true;
  384. } else {
  385. return false;
  386. }
  387. }
  388. //跟我同城
  389. public function isHeightShorter($user, $other)
  390. {
  391. if ($user->height >= $other->height) {
  392. return true;
  393. } else {
  394. return false;
  395. }
  396. }
  397. public function isSchool($user, $other)
  398. {
  399. if ($user->schoool == $other->schoool) {
  400. return 1;
  401. } else {
  402. return 0;
  403. }
  404. }
  405. }