PullCalcBonusData.php 20 KB

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