PullCalcBonusData.php 17 KB

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