PullCalcBonusData.php 15 KB

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