PullCalcBonusData.php 11 KB

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