PullCalcBonusData.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace common\helpers\bonus\Calc;
  3. use common\helpers\Date;
  4. use common\models\CalcBonus;
  5. use common\models\CalcBonusBD;
  6. use common\models\CalcBonusBS;
  7. use common\models\CalcBonusGarage;
  8. use common\models\CalcBonusGL;
  9. use common\models\CalcBonusGX;
  10. use common\models\CalcBonusQuarter;
  11. use common\models\CalcBonusQY;
  12. use common\models\CalcBonusStandard;
  13. use common\models\CalcBonusTG;
  14. use common\models\CalcBonusTourism;
  15. use common\models\CalcBonusVilla;
  16. use common\models\CalcBonusVIP;
  17. use common\models\CalcBonusYC;
  18. use common\models\CalcBonusYJ;
  19. use common\models\CalcRecord;
  20. use common\models\ScoreMonth;
  21. use common\models\User;
  22. use common\models\UserBonus;
  23. use common\models\UserInfo;
  24. class PullCalcBonusData extends BaseBusiness
  25. {
  26. const BASE_INFO_METHODS = [
  27. 'calcBonus' => ['type' => 'separately', 'table' => 'AR_CALC_BONUS'],
  28. 'calcBonusBs' => ['type' => 'same', 'table' => 'AR_CALC_BONUS_BS'],
  29. 'calcBonusQuarter' => ['type' => 'same', 'table' => 'AR_CALC_BONUS_QUARTER'],
  30. 'calcBonusTourism' => ['type' => 'same', 'table' => 'AR_CALC_BONUS_TOURISM'],
  31. 'calcBonusBd' => ['type' => 'calc_month', 'table' => 'AR_CALC_BONUS_BD'],
  32. 'calcBonusQy' => ['type' => 'calc_month', 'table' => 'AR_CALC_BONUS_QY'],
  33. 'calcBonusTg' => ['type' => 'calc_month', 'table' => 'AR_CALC_BONUS_TG'],
  34. 'calcBonusVilla' => ['type' => 'calc_month', 'table' => 'AR_CALC_BONUS_VILLA'],
  35. 'calcBonusGarage' => ['type' => 'calc_month', 'table' => 'AR_CALC_BONUS_GARAGE'],
  36. ];
  37. public function __construct($periodNum)
  38. {
  39. parent::__construct($periodNum);
  40. }
  41. public function start(): array
  42. {
  43. $db = $this->_calc_db_name;
  44. try {
  45. //清除本期原有数据,可多次拉取
  46. // \Yii::$app->$db->createCommand()->delete($table, "PERIOD_NUM = $this->_periodNum")->execute();
  47. $this->clearCalcTableData();
  48. foreach (self::BASE_INFO_METHODS as $method => $info) {
  49. $table = $info['table'];
  50. if ('same' == $info['type']) {
  51. $_offset = 0;
  52. sameBonus:
  53. $offset = $_offset * $this->_limit;
  54. $data = \Yii::$app->$db->createCommand("SELECT * from $table where PERIOD_NUM = $this->_periodNum limit $this->_limit offset $offset;")->queryAll();
  55. if (!empty($data)) {
  56. $fieldArray = array_keys($data[0]);
  57. $_offset += 1;
  58. \Yii::$app->db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  59. $data = null;
  60. goto sameBonus;
  61. }
  62. $data = null;
  63. continue;
  64. }
  65. if ('calc_month' == $info['type']) {
  66. $_offset = 0;
  67. calcMonth:
  68. $offset = $_offset * $this->_limit;
  69. $data = \Yii::$app->$db->createCommand("SELECT *,FROM_UNIXTIME(CREATED_AT,'%Y-%m-%d') as P_CALC_MONTH from $table where PERIOD_NUM = $this->_periodNum limit $this->_limit offset $offset;")->queryAll();
  70. if (!empty($data)) {
  71. $fieldArray = array_keys($data[0]);
  72. $_offset += 1;
  73. \Yii::$app->db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  74. $data = null;
  75. goto calcMonth;
  76. }
  77. $data = null;
  78. continue;
  79. }
  80. if ('separately' == $info['type'] && method_exists($this, $method)) {
  81. $this->$method();
  82. continue;//必要,避免增加逻辑代码后忘记此处
  83. }
  84. }
  85. //拉取期业绩
  86. $res = (new PullPerfDataFromCalc($this->_periodNum))->start();
  87. if (200 != $res['code']) {
  88. CalcRecord::record($this->_periodNum, '第' . $this->_periodNum . '期的奖金计算后的期业绩数据获取失败,原因:' . $res['msg']);
  89. }
  90. } catch (\Exception $e) {
  91. return $this->fail('msg:' . $e->getMessage() . 'line:' . $e->getLine());
  92. }
  93. //同步周期表的值到业务系统
  94. self::pullPeriodForUpdate($this->_periodNum);
  95. return $this->success();
  96. }
  97. public function calcBonus()
  98. {
  99. $repeatField = [
  100. // 'ORI_CAPPED_BONUS_QY' => ['CAPPED_BONUS_QY'],
  101. 'ORI_BONUS_QY' => ['BONUS_QY', 'SHOULD_QY'],
  102. 'ORI_BONUS_BS' => ['BONUS_BS', 'REAL_BONUS_BS', 'BONUS_BS_ABBR', 'ORI_BONUS_BS_ABBR', 'REAL_BONUS_BS_ABBR'],//
  103. 'ORI_BONUS_QUARTER' => ['BONUS_QUARTER'],
  104. 'ORI_BONUS_TOURISM' => ['BONUS_TOURISM'],
  105. 'ORI_BONUS_GARAGE' => ['BONUS_GARAGE'],
  106. 'ORI_BONUS_VILLA' => ['BONUS_VILLA'],
  107. 'ORI_BONUS_BD' => ['BONUS_BD'],
  108. 'ORI_BONUS_TG' => ['BONUS_TG'],
  109. ];
  110. $db = $this->_calc_db_name;
  111. $table = self::BASE_INFO_METHODS['calcBonus']['table'];
  112. $pCalcMonth = Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  113. $calcPeriod = $this->getCalcPeriod();
  114. $calculatedAt = $calcPeriod['CALCULATED_AT'];
  115. $_offset = 0;
  116. $this->_limit = 2000;
  117. calcBonus:
  118. $offset = $_offset * $this->_limit;
  119. $data = \Yii::$app->$db->createCommand("SELECT
  120. b.*,
  121. 1 as LAST_LOCATION,
  122. $calculatedAt as CALCULATED_AT,
  123. '$pCalcMonth' as P_CALC_MONTH
  124. from $table as b
  125. where b.PERIOD_NUM = $this->_periodNum limit $this->_limit offset $offset;")->queryAll();
  126. if (!empty($data)) {
  127. $_offset += 1;
  128. $data = array_column($data, null, 'USER_ID');
  129. $userIds = array_keys($data);
  130. //获取用户信息相关的数据 在下方循环内以名为$user的变量整合
  131. $userInfo = array_column(self::getUserInfoByUserIds($userIds), null, 'ID');
  132. //获取用户积分相关的数据
  133. $userPointsInfo = array_column(self::getUserPointsByUserIds($userIds), null, 'ID');
  134. foreach ($data as $userId => $value) {
  135. if (isset($userInfo[$userId])) {
  136. $user = $userInfo[$userId];
  137. $user['LAST_REC_USER_NAME'] = $user['recUserName']['USER_NAME'] ?? '';
  138. $user['LAST_REC_REAL_NAME'] = $user['recRealName']['REAL_NAME'] ?? '';
  139. $user['LAST_CON_USER_NAME'] = $user['conUserName']['USER_NAME'] ?? '';
  140. $user['LAST_CON_REAL_NAME'] = $user['conRealName']['REAL_NAME'] ?? '';
  141. $user['LAST_SYSTEM_ID'] = $user['LAST_SYSTEM_ID'] ?? '';
  142. $user['EXCHANGE_POINTS'] = $userPointsInfo[$userId] ?? 0;
  143. //将ori开头的奖金项目赋值到实发奖金字段
  144. $value = self::formatDataByParams($value, $repeatField);
  145. unset($user['ID'], $user['REC_UID'], $user['CON_UID'], $user['recUserName'], $user['recRealName'],
  146. $user['conUserName'], $user['conRealName'],
  147. $value['ORI_BONUS_TOURISM'], $value['ORI_BONUS_GARAGE'], $value['ORI_BONUS_VILLA']);
  148. //整合数据
  149. $data[$userId] = array_merge($value, $user);
  150. }
  151. }
  152. $fieldArray = array_keys(current($data));
  153. \Yii::$app->db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  154. $userInfo = null;
  155. $data = null;
  156. goto calcBonus;
  157. }
  158. $data = null;
  159. return true;
  160. }
  161. /**
  162. * @param array $userIds
  163. * @return array|\yii\db\ActiveRecord[]
  164. */
  165. public static function getUserInfoByUserIds(array $userIds): array
  166. {
  167. if (empty($userIds)) {
  168. return [];
  169. }
  170. return UserInfo::find()->alias('UI')
  171. ->join('INNER JOIN', User::tableName() . ' AS U', 'U.ID = UI.USER_ID')
  172. ->where(['in', 'U.ID', $userIds])
  173. ->select('
  174. U.ID,U.STATUS AS LAST_STATUS,U.MOBILE AS LAST_MOBILE,U.PERIOD_AT AS LAST_PERIOD_AT,U.CREATED_AT AS LAST_CREATED_AT,
  175. U.SUB_COM_ID AS LAST_SUB_COM_ID,U.PROVINCE AS LAST_PROVINCE,U.CITY AS LAST_CITY,U.COUNTY AS LAST_COUNTY,
  176. U.IS_DIRECT_SELLER AS LAST_IS_DIRECT_SELLER,UI.SYSTEM_ID AS LAST_SYSTEM_ID,UI.REC_UID,UI.CON_UID')
  177. ->with([
  178. 'recUserName' => function ($query) {
  179. $query->select('USER_ID,USER_NAME');
  180. },
  181. 'recRealName' => function ($query) {
  182. $query->select('ID,REAL_NAME');
  183. },
  184. 'conUserName' => function ($query) {
  185. $query->select('USER_ID,USER_NAME');
  186. },
  187. 'conRealName' => function ($query) {
  188. $query->select('ID,REAL_NAME');
  189. }
  190. ])
  191. ->asArray()
  192. ->all();
  193. }
  194. /**
  195. * @param array $userIds
  196. * @return array|\yii\db\ActiveRecord[]
  197. */
  198. public static function getUserPointsByUserIds(array $userIds): array
  199. {
  200. if (empty($userIds)) {
  201. return [];
  202. }
  203. return UserBonus::find()->where(['in', 'USER_ID', $userIds])
  204. ->select('EXCHANGE_POINTS,USER_ID')
  205. ->asArray()
  206. ->all();
  207. }
  208. /**
  209. * @param array $data
  210. * @param array $format
  211. * @return array
  212. */
  213. public static function formatDataByParams(array $data, array $format): array
  214. {
  215. if (empty($data) || empty($format)) {
  216. return [];
  217. }
  218. foreach ($data as $field => $v) {
  219. if (isset($format[$field])) {
  220. foreach ($format[$field] as $formatField) {
  221. $data[$formatField] = $v;
  222. }
  223. }
  224. }
  225. return $data;
  226. }
  227. /**
  228. * 清空相关表数据
  229. */
  230. public function clearCalcTableData()
  231. {
  232. // 奖金表
  233. CalcBonus::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  234. CalcBonusBS::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  235. CalcBonusQuarter::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  236. CalcBonusBD::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  237. CalcBonusQY::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  238. CalcBonusTG::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  239. // CalcBonusYJ::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  240. // CalcBonusGX::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  241. // CalcBonusGL::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  242. // 月结时要清空的数据
  243. if ($this->_isCalcMonth) {
  244. // CalcBonusYC::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  245. // CalcBonusVIP::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  246. // CalcBonusStandard::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  247. // ScoreMonth::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  248. CalcBonusTourism::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  249. CalcBonusGarage::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  250. CalcBonusVilla::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  251. }
  252. }
  253. }