PullCalcBonusData.php 15 KB

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