Perf.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/10/9
  6. * Time: 下午3:25
  7. */
  8. namespace common\helpers\user;
  9. use common\helpers\Date;
  10. use common\helpers\Tool;
  11. use common\models\PerfMonth;
  12. use common\models\PerfPeriod;
  13. use common\models\Period;
  14. use common\models\DecOrder;
  15. use common\models\UserNetwork;
  16. use common\models\UserPerf;
  17. use common\models\UserRelation;
  18. use yii\helpers\Json;
  19. class Perf
  20. {
  21. const CACHE_PERIOD_NEW_PERF_PREFIX = 'periodNewPerf_';
  22. const CACHE_PERIOD_DEEP_IN_NEW_PERF_PREFIX = 'periodDeepInNewPerf_';
  23. /**
  24. * 获取该会员当期新增业绩
  25. * @param $userId
  26. * @param null $relationDeep
  27. * 传推荐网络代数,如果传了此值,则只计算这个代数的所有子会员给的业绩
  28. * @return array
  29. */
  30. public static function getPeriodNewPerf($userId, $periodNum = null, $relationDeep = null){
  31. $period = Period::instance();
  32. if(!$periodNum) {
  33. $periodNum = $period->getNowPeriodNum();
  34. }
  35. $redisKey = self::CACHE_PERIOD_NEW_PERF_PREFIX.$userId;
  36. $perfArr = \Yii::$app->redis->get($redisKey);
  37. if($perfArr){
  38. $perfArr = Json::decode($perfArr);
  39. if($perfArr['periodNum'] == $periodNum){
  40. return $perfArr['perf'];
  41. }
  42. }
  43. $perf = [
  44. 'PV_TOTAL' => 0,
  45. 'PV_1L' => 0,
  46. 'PV_1L_ZC' => 0,
  47. 'PV_1L_YH' => 0,
  48. 'PV_1L_ZG' => 0,
  49. 'PV_1L_LS' => 0,
  50. 'PV_1L_FX' => 0,
  51. 'PV_2L' => 0,
  52. 'PV_2L_ZC' => 0,
  53. 'PV_2L_YH' => 0,
  54. 'PV_2L_ZG' => 0,
  55. 'PV_2L_LS' => 0,
  56. 'PV_2L_FX' => 0,
  57. 'PV_3L' => 0,
  58. 'PV_3L_ZC' => 0,
  59. 'PV_3L_YH' => 0,
  60. 'PV_3L_ZG' => 0,
  61. 'PV_3L_LS' => 0,
  62. 'PV_3L_FX' => 0,
  63. 'PV_4L' => 0,
  64. 'PV_4L_ZC' => 0,
  65. 'PV_4L_YH' => 0,
  66. 'PV_4L_ZG' => 0,
  67. 'PV_4L_LS' => 0,
  68. 'PV_4L_FX' => 0,
  69. 'PV_5L' => 0,
  70. 'PV_5L_ZC' => 0,
  71. 'PV_5L_YH' => 0,
  72. 'PV_5L_ZG' => 0,
  73. 'PV_5L_LS' => 0,
  74. 'PV_5L_FX' => 0,
  75. 'PV_PCS' => 0,
  76. 'PV_PSS' => 0,
  77. ];
  78. if ($period->isSent($periodNum)) {
  79. if ($perf = PerfPeriod::find()->select('PV_1L,PV_1L_ZC,PV_1L_YH,PV_1L_ZG,PV_1L_LS,PV_1L_FX,PV_2L,PV_2L_ZC,PV_2L_YH,PV_2L_ZG,PV_2L_LS,PV_2L_FX,PV_3L,PV_3L_ZC,PV_3L_YH,PV_3L_ZG,PV_3L_LS,PV_3L_FX,PV_4L,PV_4L_ZC,PV_4L_YH,PV_4L_ZG,PV_4L_LS,PV_4L_FX,PV_5L,PV_5L_ZC,PV_5L_YH,PV_5L_ZG,PV_5L_LS,PV_5L_FX,PV_PCS,PV_PSS')->where('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [':USER_ID'=>$userId, ':PERIOD_NUM' => $periodNum])->asArray()->one()) {
  80. $perf['PV_TOTAL'] = $perf['PV_1L'] + $perf['PV_2L'] + $perf['PV_3L'] + $perf['PV_4L'] + $perf['PV_5L'];
  81. }
  82. } else {
  83. foreach ($perf as $k => $item) {
  84. $perf[$k] = Tool::formatPrice($item);
  85. }
  86. }
  87. // 把业绩加入缓存中5分钟(防止频繁获取)
  88. \Yii::$app->redis->set($redisKey, Json::encode(['periodNum'=>$periodNum, 'perf'=>$perf]));
  89. \Yii::$app->redis->expire($redisKey, 5 * 60);
  90. return $perf;
  91. }
  92. /**
  93. * 获取期数业绩
  94. * @param $userId
  95. * @param $periodNum
  96. * @return array
  97. */
  98. public static function getPeriodPerf($userId, $periodNum){
  99. $period = Period::instance();
  100. $yearMonth = $period->getYearMonth($periodNum);
  101. $result = PerfPeriod::findUseSlaves()->yearMonth($yearMonth)->where('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [':USER_ID'=>$userId, ':PERIOD_NUM'=>$periodNum])->asArray()->one();
  102. $userStatus = Status::getStatus($userId, $periodNum);
  103. if(!$result || $userStatus == \Yii::$app->params['userStatus'][2]['value'] || $userStatus == \Yii::$app->params['userStatus'][9]['value']){
  104. $result = [
  105. 'PV_PCS' => 0,
  106. 'PV_PCS_ZC' => 0,
  107. 'PV_PCS_YH' => 0,
  108. 'PV_PCS_ZG' => 0,
  109. 'PV_PCS_LS' => 0,
  110. 'PV_PCS_FX' => 0,
  111. 'PV_1L' => 0,
  112. 'PV_1L_ZC' => 0,
  113. 'PV_1L_YH' => 0,
  114. 'PV_1L_ZG' => 0,
  115. 'PV_1L_LS' => 0,
  116. 'PV_1L_FX' => 0,
  117. 'PV_2L' => 0,
  118. 'PV_2L_ZC' => 0,
  119. 'PV_2L_YH' => 0,
  120. 'PV_2L_ZG' => 0,
  121. 'PV_2L_LS' => 0,
  122. 'PV_2L_FX' => 0,
  123. 'PV_3L' => 0,
  124. 'PV_3L_ZC' => 0,
  125. 'PV_3L_YH' => 0,
  126. 'PV_3L_ZG' => 0,
  127. 'PV_3L_LS' => 0,
  128. 'PV_3L_FX' => 0,
  129. 'PV_4L' => 0,
  130. 'PV_4L_ZC' => 0,
  131. 'PV_4L_YH' => 0,
  132. 'PV_4L_ZG' => 0,
  133. 'PV_4L_LS' => 0,
  134. 'PV_4L_FX' => 0,
  135. 'PV_5L' => 0,
  136. 'PV_5L_ZC' => 0,
  137. 'PV_5L_YH' => 0,
  138. 'PV_5L_ZG' => 0,
  139. 'PV_5L_LS' => 0,
  140. 'PV_5L_FX' => 0,
  141. 'PV_LS_TOUCH' => 0,
  142. 'SURPLUS_1L' => 0,
  143. 'SURPLUS_2L' => 0,
  144. 'SURPLUS_3L' => 0,
  145. 'SURPLUS_4L' => 0,
  146. 'SURPLUS_5L' => 0,
  147. 'SURPLUS_LS' => 0,
  148. ];
  149. }
  150. return $result;
  151. }
  152. /**
  153. * 获取指定深度这一代会员给我的业绩
  154. * @param $userId
  155. * @param $relationDeep
  156. * @return int|mixed
  157. */
  158. public static function getChildrenDeepInNewPerf($userId, $relationDeep){
  159. $period = Period::instance();
  160. $yearMonth = $period->getNowYearMonth();
  161. $periodNum = $period->getNowPeriodNum();
  162. $redisKey = self::CACHE_PERIOD_DEEP_IN_NEW_PERF_PREFIX.$relationDeep.'_'.$userId;
  163. $perfArr = \Yii::$app->redis->get($redisKey);
  164. if($perfArr){
  165. $perfArr = Json::decode($perfArr);
  166. if($perfArr['periodNum'] == $periodNum){
  167. return $perfArr['perf'];
  168. }
  169. }
  170. static $allData = [];
  171. if(!$allData){
  172. $allData = DecOrder::find()->where('PERIOD_NUM=:PERIOD_NUM AND IS_DEL=0', [':PERIOD_NUM'=>$periodNum])->asArray()->all();
  173. }
  174. $perf = [
  175. 'PV_ZC' => 0,
  176. 'PV_YH' => 0,
  177. 'PV_ZG' => 0,
  178. 'PV_LS' => 0,
  179. 'PV_FX' => 0,
  180. ];
  181. foreach($allData as $data){
  182. if(UserRelation::deepWithParent($data['TO_USER_ID'], $userId) != $relationDeep){
  183. continue;
  184. }
  185. $perf['PV_'.$data['TYPE']] += $data['DEC_PV'];
  186. }
  187. // 把业绩加入缓存中5分钟(防止频繁获取)
  188. \Yii::$app->redis->set($redisKey, Json::encode(['periodNum'=>$periodNum, 'perf'=>$perf]));
  189. \Yii::$app->redis->expire($redisKey, 5 * 60);
  190. return $perf;
  191. }
  192. /**
  193. * 获取月业绩
  194. * @param $userId
  195. * @param null $yearMonth
  196. * @return array
  197. */
  198. public static function getMonthPerf($userId, $yearMonth = null){
  199. $result = [
  200. 'PV_PCS'=>0,//新增个人业绩
  201. 'PV_1L'=>0,//当月新增
  202. 'PV_2L'=>0,
  203. 'PV_3L'=>0,
  204. 'PV_4L'=>0,
  205. 'PV_5L'=>0,
  206. 'PV_1L_TOTAL'=>0,//总业绩
  207. 'PV_2L_TOTAL'=>0,
  208. 'PV_3L_TOTAL'=>0,
  209. 'PV_4L_TOTAL'=>0,
  210. 'PV_5L_TOTAL'=>0,
  211. 'PV_TOTAL' => 0,//当月新增
  212. 'PV_TOTAL_TOTAL' => 0,//总业绩
  213. 'PV_PSS' => 0,//新增团队业绩
  214. 'PV_PSS_TOTAL' => 0,//团队总业绩
  215. ];
  216. $userStatus = Status::getStatus($userId);
  217. if($userStatus == \Yii::$app->params['userStatus'][2]['value'] || $userStatus == \Yii::$app->params['userStatus'][9]['value']){
  218. return $result;
  219. }
  220. // 当月业绩
  221. if($yearMonth === null){
  222. $period = Period::instance();
  223. $yearMonth = $period->getNowYearMonth();
  224. // 获取本月已挂网的其他期业绩
  225. // 该月所有已经挂网的期数
  226. $allSentPeriod = Period::monthSentAllPeriodNum($yearMonth);
  227. $sentPeriodWhere = '';
  228. if($allSentPeriod){
  229. $sentPeriodWhere = implode("','", $allSentPeriod);
  230. $sentPeriodWhere = " AND PERIOD_NUM IN ('$sentPeriodWhere')";
  231. }
  232. $perfPeriodSum = PerfPeriod::find()->yearMonth($yearMonth)->select('SUM(PV_PCS) AS PV_PCS,SUM(PV_1L) AS PV_1L,SUM(PV_2L) AS PV_2L,SUM(PV_3L) AS PV_3L,SUM(PV_4L) AS PV_4L,SUM(PV_5L) AS PV_5L,SUM(PV_PSS) AS PV_PSS')->where('USER_ID=:USER_ID'.$sentPeriodWhere, [':USER_ID'=>$userId])->asArray()->one();
  233. if($perfPeriodSum){
  234. $result['PV_PCS'] += $perfPeriodSum['PV_PCS'];
  235. $result['PV_1L'] += $perfPeriodSum['PV_1L'];
  236. $result['PV_2L'] += $perfPeriodSum['PV_2L'];
  237. $result['PV_3L'] += $perfPeriodSum['PV_3L'];
  238. $result['PV_4L'] += $perfPeriodSum['PV_4L'];
  239. $result['PV_5L'] += $perfPeriodSum['PV_5L'];
  240. $result['PV_PSS'] += $perfPeriodSum['PV_PSS'];
  241. $result['PV_TOTAL'] += ($perfPeriodSum['PV_1L'] + $perfPeriodSum['PV_2L'] + $perfPeriodSum['PV_3L'] + $perfPeriodSum['PV_4L'] + $perfPeriodSum['PV_5L']);
  242. }
  243. // 获取本期业绩
  244. $nowPeriodPerf = self::getPeriodNewPerf($userId);
  245. if($nowPeriodPerf){
  246. $result['PV_PCS'] += $nowPeriodPerf['PV_PCS'];
  247. $result['PV_1L'] += $nowPeriodPerf['PV_1L'];
  248. $result['PV_2L'] += $nowPeriodPerf['PV_2L'];
  249. $result['PV_3L'] += $nowPeriodPerf['PV_3L'];
  250. $result['PV_4L'] += $nowPeriodPerf['PV_4L'];
  251. $result['PV_5L'] += $nowPeriodPerf['PV_5L'];
  252. $result['PV_PSS'] += $nowPeriodPerf['PV_PSS'];
  253. $result['PV_TOTAL'] += ($nowPeriodPerf['PV_1L'] + $nowPeriodPerf['PV_2L'] + $nowPeriodPerf['PV_3L'] + $nowPeriodPerf['PV_4L'] + $nowPeriodPerf['PV_5L']);
  254. }
  255. return $result;
  256. }
  257. // 往月业绩
  258. $perfMonth = PerfMonth::find()->yearMonth($yearMonth)->select('PV_PCS,PV_1L,PV_2L,PV_3L,PV_4L,PV_5L,PV_PSS,PV_PSS_TOTAL')->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [':USER_ID'=>$userId, ':CALC_MONTH' => $yearMonth])->asArray()->one();
  259. if($perfMonth){
  260. $result['PV_PCS'] = $result['PV_PCS'] + $perfMonth['PV_PCS'];
  261. $result['PV_1L_TOTAL'] = $result['PV_1L'] + $perfMonth['PV_1L'];
  262. $result['PV_2L_TOTAL'] = $result['PV_2L'] + $perfMonth['PV_2L'];
  263. $result['PV_3L_TOTAL'] = $result['PV_3L'] + $perfMonth['PV_3L'];
  264. $result['PV_4L_TOTAL'] = $result['PV_4L'] + $perfMonth['PV_4L'];
  265. $result['PV_5L_TOTAL'] = $result['PV_5L'] + $perfMonth['PV_5L'];
  266. $result['PV_PSS'] = $result['PV_PSS'] + $perfMonth['PV_PSS'];
  267. $result['PV_PSS_TOTAL'] = $result['PV_PSS_TOTAL'] + $perfMonth['PV_PSS_TOTAL'];
  268. $result['PV_TOTAL'] = ($result['PV_1L_TOTAL'] + $result['PV_2L_TOTAL'] + $result['PV_3L_TOTAL'] + $result['PV_4L_TOTAL'] + $result['PV_5L_TOTAL']);
  269. }
  270. foreach ($result as $k=>$item) {
  271. $result[$k] = Tool::formatPrice($item);
  272. }
  273. return $result;
  274. }
  275. /**
  276. * 获取到会员的当前的总业绩,含未挂网
  277. * @param $userId
  278. * @return array
  279. */
  280. public static function getMonthAndNewPeriodPerf($userId){
  281. $perf = self::getMonthPerf($userId);
  282. // 当期业绩
  283. $newPerf = self::getPeriodNewPerf($userId);
  284. return [
  285. 'PV_1L' => $perf['PV_1L'] + $newPerf['PV_1L'],
  286. 'PV_2L' => $perf['PV_2L'] + $newPerf['PV_2L'],
  287. 'PV_3L' => $perf['PV_3L'] + $newPerf['PV_3L'],
  288. 'PV_4L' => $perf['PV_4L'] + $newPerf['PV_4L'],
  289. 'PV_5L' => $perf['PV_5L'] + $newPerf['PV_5L'],
  290. 'PV_TOTAL' => $perf['PV_TOTAL'] + $newPerf['PV_TOTAL'],
  291. ];
  292. }
  293. /**
  294. * 所传期数的业绩
  295. * @param $userId
  296. * @param $periodNum
  297. * @return array
  298. * @throws \yii\db\Exception
  299. */
  300. public static function getPeriodTotalPerf($userId, $periodNum){
  301. // 获取所传期数的上一个结算月
  302. $period = Period::instance();
  303. $lastMonthArr = $period->getLastMonth($periodNum);
  304. $yearMonth = $period->getYearMonth($periodNum);
  305. $lastYearMonth = $lastMonthArr['yearMonth'];
  306. // 获取上一个结算月的业绩
  307. $lastYearMonthPerf = self::getMonthPerf($userId, $lastYearMonth);
  308. $result = [
  309. 'PV_1L' => $lastYearMonthPerf['PV_1L'],
  310. 'PV_2L' => $lastYearMonthPerf['PV_2L'],
  311. 'PV_3L' => $lastYearMonthPerf['PV_3L'],
  312. 'PV_4L' => $lastYearMonthPerf['PV_4L'],
  313. 'PV_5L' => $lastYearMonthPerf['PV_5L'],
  314. 'PV_TOTAL' => $lastYearMonthPerf['PV_TOTAL'],
  315. ];
  316. // 获取所传期数的本月的已结算的全部期数
  317. $allCalcPeriodNumArr = Period::monthCalcAllPeriodNum($yearMonth);
  318. foreach($allCalcPeriodNumArr as $calcPeriodNum){
  319. // 达到所传期数停止
  320. if($calcPeriodNum > $periodNum) break;
  321. $periodPerf = self::getPeriodPerf($userId, $periodNum);
  322. $result['PV_1L'] += $periodPerf['PV_1L'];
  323. $result['PV_2L'] += $periodPerf['PV_2L'];
  324. $result['PV_3L'] += $periodPerf['PV_3L'];
  325. $result['PV_4L'] += $periodPerf['PV_4L'];
  326. $result['PV_5L'] += $periodPerf['PV_5L'];
  327. $result['PV_TOTAL'] += ($periodPerf['PV_1L'] + $periodPerf['PV_2L'] + $periodPerf['PV_3L'] + $periodPerf['PV_4L'] + $periodPerf['PV_5L']);
  328. }
  329. return $result;
  330. }
  331. /**
  332. * 获取总业绩
  333. * @param $userId
  334. * @return array
  335. */
  336. public static function getTotalPerf($userId){
  337. $result = [
  338. 'PV_1L' => 0,
  339. 'PV_2L' => 0,
  340. 'PV_3L' => 0,
  341. 'PV_4L' => 0,
  342. 'PV_5L' => 0,
  343. 'PV_TOTAL' => 0,
  344. ];
  345. $userStatus = Status::getStatus($userId);
  346. if($userStatus == \Yii::$app->params['userStatus'][2]['value'] || $userStatus == \Yii::$app->params['userStatus'][9]['value']){
  347. return $result;
  348. }
  349. // 历史已挂网业绩
  350. $perf = UserPerf::findOneAsArray('USER_ID=:USER_ID', [':USER_ID'=>$userId], 'PV_1L,PV_2L,PV_3L,PV_4L,PV_5L');
  351. if($perf){
  352. $result['PV_1L'] += $perf['PV_1L'];
  353. $result['PV_2L'] += $perf['PV_2L'];
  354. $result['PV_3L'] += $perf['PV_3L'];
  355. $result['PV_4L'] += $perf['PV_4L'];
  356. $result['PV_5L'] += $perf['PV_5L'];
  357. $result['PV_TOTAL'] += ($perf['PV_1L'] + $perf['PV_2L'] + $perf['PV_3L'] + $perf['PV_4L'] + $perf['PV_5L']);
  358. }
  359. // 获取本期业绩
  360. $nowPeriodPerf = self::getPeriodNewPerf($userId);
  361. if($nowPeriodPerf){
  362. $result['PV_1L'] += $nowPeriodPerf['PV_1L'];
  363. $result['PV_2L'] += $nowPeriodPerf['PV_2L'];
  364. $result['PV_3L'] += $nowPeriodPerf['PV_3L'];
  365. $result['PV_4L'] += $nowPeriodPerf['PV_4L'];
  366. $result['PV_5L'] += $nowPeriodPerf['PV_5L'];
  367. $result['PV_TOTAL'] += ($nowPeriodPerf['PV_1L'] + $nowPeriodPerf['PV_2L'] + $nowPeriodPerf['PV_3L'] + $nowPeriodPerf['PV_4L'] + $nowPeriodPerf['PV_5L']);
  368. }
  369. return $result;
  370. }
  371. /**
  372. * 获取用户业绩
  373. * @param $userId
  374. * @return array|\yii\db\ActiveRecord
  375. */
  376. public static function getUserPerf( $userId ) {
  377. $data = UserPerf::find()->where('USER_ID=:USER_ID', ['USER_ID'=>$userId])->asArray()->one();
  378. return $data ?? [];
  379. }
  380. }