PullCalcBonusData.php 17 KB

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