PushBaseDataToCalc.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/8/2
  6. * Time: 上午10:38
  7. */
  8. namespace common\helpers\bonus\Calc;
  9. use common\components\ActiveRecord;
  10. use common\models\CalcBonus;
  11. use common\models\CalcBonusBS;
  12. use common\models\CalcBonusQY;
  13. use common\models\CalcRecord;
  14. use common\models\EmployLevel;
  15. use common\models\forms\DeclarationForm;
  16. use common\models\PerfMonth;
  17. use common\models\PerfOrder;
  18. use common\models\PerfPeriod;
  19. use common\models\PerfStandard;
  20. use common\models\Period;
  21. /**
  22. * 推送基础数据供计算服务使用
  23. */
  24. class PushBaseDataToCalc extends BaseBusiness
  25. {
  26. const BASE_INFO_METHODS = [
  27. //--- 用户表
  28. 'user' => ['table' => 'AR_USER', 'field' => [
  29. 'ID',
  30. 'USER_NAME',
  31. 'REAL_NAME',
  32. 'CREATED_AT',
  33. 'STATUS',
  34. 'DEC_LV',
  35. 'EMP_LV',
  36. 'CROWN_LV',
  37. 'IS_DEC',
  38. 'DEC_ID',
  39. 'DEC_ROLE_ID',
  40. 'LAST_DEC_LV',
  41. 'LAST_EMP_LV',
  42. 'IS_STUDIO',
  43. 'LAST_CROWN_LV',
  44. 'DELETED',
  45. ]],
  46. //--- 用户信息表
  47. 'userInfo' => ['table' => 'AR_USER_INFO', 'field' => [
  48. 'ID',
  49. 'USER_ID',
  50. 'USER_NAME',
  51. 'CON_UID',
  52. 'REC_UID',
  53. 'CREATED_AT',
  54. ]],
  55. //---配置表初始化数据见数据库是否开启管理奖,使用的是openbs进行控制
  56. 'config' => ['table' => 'AR_CONFIG', 'field' => [
  57. 'CONFIG_NAME',
  58. 'TITLE',
  59. 'UNIT',
  60. 'INPUT_TYPE',
  61. 'OPTIONS',
  62. 'VALUE',
  63. 'TYPE',
  64. 'SORT',
  65. 'CREATED_AT',
  66. 'UPDATED_AT',
  67. ]],
  68. //--- 报单级别配置表
  69. 'declarationLevel' => ['table' => 'AR_DECLARATION_LEVEL', 'field' => [
  70. 'ID',
  71. 'LEVEL_NAME',
  72. 'PERF',
  73. 'QY_PERCENT',
  74. 'QY_TOUCH_CAP',
  75. 'QY_BIG_CAP',
  76. 'SORT',
  77. 'CREATED_AT',
  78. 'INCOME_CAP',
  79. ]],
  80. //--- 级别配置表
  81. 'employLevel' => ['table' => 'AR_EMPLOY_LEVEL', 'field' => [
  82. 'ID',
  83. 'LEVEL_NAME',
  84. 'CREATED_AT',
  85. 'BS_PERCENT',
  86. 'LEVEL_SORT',
  87. 'TOURISM_PERCENT',
  88. 'GARAGE_PERCENT',
  89. 'ACHIEVE_PV',
  90. 'ACHIEVE_MEMBER_NUM',
  91. 'ACHIEVE_PERF_PV',
  92. ], 'alias' => [
  93. 'LEVEL_SORT' => 'SORT',
  94. ]],
  95. //--- 报单中心级别配置表
  96. 'decRole' => ['table' => 'AR_DEC_ROLE', 'field' => [
  97. 'ID',
  98. 'ROLE_NAME',
  99. 'FW_BONUS_PERCENT',
  100. 'SORT',
  101. 'CREATED_AT',
  102. ]],
  103. //--- 安置网络表
  104. 'userNetwork' => ['table' => 'AR_USER_NETWORK_NEW', 'field' => [
  105. 'ID',
  106. 'USER_ID',
  107. 'PARENT_UID',
  108. // 'LOCATION_TAG',
  109. 'RELATIVE_LOCATION',
  110. // 'TOP_UID',
  111. // 'TOP_DEEP',
  112. // 'PARENT_UIDS',
  113. 'CREATED_AT',
  114. // 'UPDATED_AT',
  115. ]],
  116. //--- 星级配置表
  117. 'starCrownLevel' => ['table' => 'AR_CROWN_LEVEL', 'field' => [
  118. 'ID',
  119. 'LEVEL_NAME',
  120. 'MIN_LEVEL_ID',
  121. 'LEVEL_SCORE',
  122. 'UPGRADE_SCORE',
  123. 'SORT',
  124. 'TOURISM_PERCENT',
  125. 'VILLA_PERCENT',
  126. 'GARAGE_PERCENT',
  127. 'CREATED_AT',
  128. ]],
  129. //--- 业绩单表
  130. 'perfOrder' => ['separately' => true, 'table' => 'AR_PERF_ORDER', 'field' => [
  131. 'ID',
  132. 'SN',
  133. // 'PERF_TYPE',
  134. 'DEC_TYPE',
  135. 'USER_ID',
  136. 'PV',
  137. 'PERIOD_NUM',
  138. 'CALC_MONTH',
  139. 'DEC_USER_ID',
  140. 'CREATED_AT',
  141. 'ORDER_AMOUNT',
  142. 'PAY_TYPE',
  143. 'FROM_TABLES',
  144. 'PAY_TYPE'
  145. ]],
  146. //--- 用户结余业绩表
  147. 'userPerf' => ['table' => 'AR_USER_PERF', 'field' => [
  148. 'ID',
  149. 'USER_ID',
  150. 'PV_PCS_ZC',
  151. 'PV_PCS_FX',
  152. 'PV_1L',
  153. 'PV_2L',
  154. 'PV_3L',
  155. 'PV_4L',
  156. 'PV_5L',
  157. 'CREATED_AT',
  158. 'PV_PSS',
  159. 'SURPLUS_1L',
  160. 'SURPLUS_1L_ZC',
  161. 'SURPLUS_1L_FX',
  162. 'SURPLUS_2L',
  163. 'SURPLUS_2L_ZC',
  164. 'SURPLUS_2L_FX',
  165. 'SURPLUS_3L',
  166. 'SURPLUS_3L_ZC',
  167. 'SURPLUS_3L_FX',
  168. 'SURPLUS_4L',
  169. 'SURPLUS_4L_ZC',
  170. 'SURPLUS_4L_FX',
  171. 'SURPLUS_5L',
  172. 'SURPLUS_5L_ZC',
  173. 'SURPLUS_5L_FX',
  174. 'PV_PSS_TOTAL',
  175. ]],
  176. //--- 团队奖明细表, 月节点的时候,需要将这个结算月的所有数据都同步过来
  177. 'calcBonusQy' => ['separately' => true, 'table' => 'AR_CALC_BONUS_QY', 'field' => [
  178. 'ID',
  179. 'USER_ID',
  180. 'LAST_DEC_LV',
  181. 'LAST_EMP_LV',
  182. 'LAST_CROWN_LV',
  183. 'LAST_STATUS',
  184. 'AMOUNT',
  185. 'PERIOD_NUM',
  186. 'CALC_YEAR',
  187. 'CALC_MONTH',
  188. 'LOGS',
  189. 'CREATED_AT',
  190. 'ORI_BONUS',
  191. 'RECONSUME_POINTS',
  192. 'MANAGE_TAX',
  193. 'ORI_CAPPED_BONUS_QY',
  194. 'IS_ACTIVE',
  195. 'HOPE_CROWN_LV',
  196. 'HOPE_BONUS',
  197. ]],
  198. //--- 用户结算月累计复消积分表,此表为新表,不传数据即代表没有扣除复消积分。 统计此结算月扣除的复消费积分总数,根据calc_bonus表 sum 一下复消积分字段即可。
  199. 'deductReconsume' => ['separately' => true, 'table' => 'AR_DEDUCT_RECONSUME', 'field' => [
  200. 'ID',
  201. 'USER_ID',
  202. 'CALC_MONTH',
  203. 'RECONSUME_POINTS_SUM',
  204. 'CREATED_AT',
  205. ]],
  206. //--- 期业绩表数据. 月节点的时候,计算月业绩,需要业务系统将ar_perf_period表此结算月的所有数据同步过来。
  207. 'perfPeriod' => ['separately' => true, 'table' => 'AR_PERF_PERIOD', 'field' => [
  208. 'ID',
  209. 'USER_ID',
  210. 'FX_AMOUNT_CASH',
  211. 'PV_PCS',
  212. 'PV_PCS_ZC',
  213. 'PV_PCS_FX',
  214. 'PV_PCS_FX_CASH',
  215. 'PV_PCS_FX_POINT',
  216. 'PV_1L',
  217. 'PV_1L_TOUCH',
  218. 'PV_1L_ZC',
  219. 'PV_1L_FX',
  220. 'PV_2L',
  221. 'PV_2L_TOUCH',
  222. 'PV_2L_ZC',
  223. 'PV_2L_FX',
  224. 'PV_3L',
  225. 'PV_3L_TOUCH',
  226. 'PV_3L_ZC',
  227. 'PV_3L_FX',
  228. 'PV_4L',
  229. 'PV_4L_TOUCH',
  230. 'PV_4L_ZC',
  231. 'PV_4L_FX',
  232. 'PV_5L',
  233. 'PV_5L_TOUCH',
  234. 'PV_5L_ZC',
  235. 'PV_5L_FX',
  236. 'SURPLUS_1L',
  237. 'SURPLUS_1L_ZC',
  238. 'SURPLUS_1L_FX',
  239. 'SURPLUS_2L',
  240. 'SURPLUS_2L_ZC',
  241. 'SURPLUS_2L_FX',
  242. 'SURPLUS_3L',
  243. 'SURPLUS_3L_ZC',
  244. 'SURPLUS_3L_FX',
  245. 'SURPLUS_4L',
  246. 'SURPLUS_4L_ZC',
  247. 'SURPLUS_4L_FX',
  248. 'SURPLUS_5L',
  249. 'SURPLUS_5L_ZC',
  250. 'SURPLUS_5L_FX',
  251. 'PERIOD_NUM',
  252. 'CALC_MONTH',
  253. 'CREATED_AT',
  254. 'PV_PSS',
  255. ]],
  256. //--- 期数配置表
  257. 'period' => ['separately' => true, 'table' => 'AR_PERIOD', 'field' => [
  258. 'ID',
  259. 'PERIOD_NUM',
  260. 'CALC_YEAR',
  261. 'CALC_MONTH',
  262. 'IS_MONTH',
  263. 'IS_PERFED',
  264. 'IS_CALCULATED',
  265. 'IS_PERFING',
  266. 'IS_CALCING',
  267. 'PERF_PERCENT',
  268. 'CALC_PERCENT',
  269. 'PERF_STARTED_AT',
  270. 'PERFED_AT',
  271. 'CALCULATE_STARTED_AT',
  272. 'CALCULATED_AT',
  273. 'CREATED_AT',
  274. ]],
  275. //--- 月业绩表 只有当前结算月为季度最终月时才推
  276. 'perfMonth' => ['separately' => true, 'table' => 'AR_PERF_MONTH', 'field' => [
  277. 'ID',
  278. 'USER_ID',
  279. 'FX_AMOUNT_CASH',
  280. 'PV_PCS',
  281. 'PV_PCS_FX',
  282. 'PV_1L',
  283. 'PV_2L',
  284. 'PV_3L',
  285. 'PV_4L',
  286. 'PV_5L',
  287. 'PV_1L_TOTAL',
  288. 'PV_2L_TOTAL',
  289. 'PV_3L_TOTAL',
  290. 'PV_4L_TOTAL',
  291. 'PV_5L_TOTAL',
  292. 'CALC_MONTH',
  293. 'CREATED_AT',
  294. 'PV_PSS',
  295. 'PV_PSS_TOTAL',
  296. ]],
  297. //--- 推荐网络表
  298. 'userRelation' => ['table' => 'AR_USER_RELATION_NEW', 'field' => [
  299. 'ID',
  300. 'USER_ID',
  301. 'PARENT_UID',
  302. 'TOP_DEEP',
  303. 'CREATED_AT',
  304. ]],
  305. ];
  306. public function __construct($periodNum)
  307. {
  308. parent::__construct($periodNum);
  309. }
  310. public function start(): array
  311. {
  312. try {
  313. //清除业务系统本地数据(业绩相关)
  314. $this->clearBusinessData();
  315. //清理计算服务系统的基本数据表
  316. $this->clearSyncCalcTableData();
  317. //从业务系统推送数据到计算服务系统
  318. $this->initializeBaseInfo();
  319. // $db = $this->_calc_db_name;
  320. //todo 更新进度
  321. // \Yii::$app->$db->createCommand()->update('AR_PERIOD', ['IS_PREPARE' => 1], 'PERIOD_NUM=:PERIOD_NUM', ['PERIOD_NUM' => $this->_periodNum])->execute();
  322. } catch (\Exception $e) {
  323. return $this->fail('msg:' . $e->getMessage() . 'line:' . $e->getLine());
  324. }
  325. return $this->success();
  326. }
  327. public function clearBusinessData()
  328. {
  329. // 周业绩
  330. PerfPeriod::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  331. CalcRecord::record($this->_periodNum, "【基础数据】业务系统,AR_PERF_PERIOD表,已清除当期所有记录");
  332. // 业绩单
  333. // PerfOrder::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  334. // 删除活跃用户
  335. // PerfActiveUser::pageDeleteAll('PERIOD_NUM='.$this->_periodNum.' AND IS_SENT=0 ');
  336. // 月结时要清空的数据
  337. if ($this->_isCalcMonth) {
  338. // 月业绩表
  339. PerfMonth::pageDeleteAll("CALC_MONTH='{$this->_calcYearMonth}'");
  340. CalcRecord::record($this->_periodNum, "【基础数据】业务系统,是月结点,AR_PERF_MONTH表,已清除当月所有记录");
  341. //达标业绩表
  342. PerfStandard::pageDeleteAll("CALC_MONTH='{$this->_calcYearMonth}'");
  343. CalcRecord::record($this->_periodNum, "【基础数据】业务系统,是月结点,AR_PERF_STANDARD表,已清除当月所有记录");
  344. }
  345. }
  346. /**
  347. * 清理配置中的数据表数据
  348. * @return array
  349. */
  350. public function clearSyncCalcTableData(): array
  351. {
  352. $db = $this->_calc_db_name;
  353. $res = [];
  354. foreach (self::BASE_INFO_METHODS as $method => $val) {
  355. if (\Yii::$app->$db->createCommand("TRUNCATE TABLE {$val['table']}")->execute() !== false) {
  356. $res[] = $val['table'];
  357. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$val['table']."表,已清空");
  358. }
  359. }
  360. return $res;
  361. }
  362. public function initializeBaseInfo()
  363. {
  364. $db = $this->_calc_db_name;
  365. foreach (self::BASE_INFO_METHODS as $method => $val) {
  366. $table = self::BASE_INFO_METHODS[$method]['table'] ?? '';
  367. $fieldArray = self::BASE_INFO_METHODS[$method]['field'] ?? [];
  368. if (!empty($table) && !empty($fieldArray)) {
  369. if (!isset($val['separately'])) {
  370. $_offset = 0;
  371. A:
  372. if (isset($val['alias'])) {
  373. //如果业务表字段和计算服务表不同,则配置alias
  374. foreach ($val['alias'] as $org => $v) {
  375. $index = array_search($org, $fieldArray);
  376. $fieldArray[$index] = $v;
  377. }
  378. }
  379. $field = implode(',', $fieldArray);
  380. $model = 'common\models\\' . ucfirst($method);
  381. $data = $model::find()->select($field)->limit($this->_limit)->offset($_offset * $this->_limit)->asArray()->all();
  382. if (!empty($data)) {
  383. $_offset += 1;
  384. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  385. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入".count($data)."条记录");
  386. $data = null;
  387. goto A;
  388. }
  389. $data = null;
  390. } elseif (method_exists($this, $method) && $val['separately']) {
  391. $this->$method($table, $fieldArray, $db);
  392. }
  393. }
  394. }
  395. return true;
  396. }
  397. public function period($table, $fieldArray, $db)
  398. {
  399. $field = implode(',', $fieldArray);
  400. $where = ['PERIOD_NUM' => $this->_periodNum];
  401. if ($this->_isCalcMonth) {
  402. $where = ['CALC_YEAR' => $this->_calcYear, 'CALC_MONTH' => $this->_calcMonth];
  403. if (in_array($this->_calcMonth, [3, 6, 9, 12])) {
  404. $where['CALC_MONTH'] = [$this->_calcMonth, $this->_calcMonth - 1, $this->_calcMonth - 2];
  405. }
  406. }
  407. $data = Period::find()->select($field)
  408. ->where($where)
  409. ->asArray()->all();
  410. if (!empty($data)) {
  411. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  412. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入 ".count($data)."条记录");
  413. }
  414. return true;
  415. }
  416. // public function user($table, $fieldArray, $db)
  417. // {
  418. // $field = '`' . implode('`, `', $fieldArray) . '`,null as LAST_EMP_LV';
  419. // $fieldArray[] = 'LAST_EMP_LV';
  420. // $_offset = 0;
  421. //
  422. // $forwardMonthPeriod = Period::find()
  423. // ->where('IS_MONTH=1 AND PERIOD_NUM<:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  424. // ->orderBy('PERIOD_NUM DESC')->asArray()->one();
  425. //
  426. // $forwardPeriodNum = $forwardMonthPeriod['PERIOD_NUM'];
  427. //
  428. //// $employeeLevelIds = CalcBonusBS::findAllAsArray(
  429. //// 'PERIOD_NUM=:PERIOD_NUM ', [':PERIOD_NUM' => $forwardPeriodNum]
  430. //// );
  431. //
  432. // $employeeLevelIds = CalcBonusBS::find()->where(
  433. // 'PERIOD_NUM=:PERIOD_NUM ', [':PERIOD_NUM' => $forwardPeriodNum]
  434. // )->select('USER_ID,LEVEL_ID')->asArray()->all();
  435. //
  436. // if (!empty($employeeLevelIds)) {
  437. // $employeeLevelIds = array_column($employeeLevelIds, null, 'USER_ID'); // 当前蓝星奖计算(即管理奖) 的等级
  438. // }
  439. //
  440. // $noLevelId = EmployLevel::NO_LEVEL_ID;
  441. //
  442. // user:
  443. // $offset = $_offset * $this->_limit;
  444. // $data = ActiveRecord::findBySql("SELECT $field from AR_USER limit $this->_limit offset $offset;")->asArray()->all();
  445. //
  446. // if (!empty($data)) {
  447. // foreach ($data as $k => $v) {
  448. // $data[$k]['LAST_EMP_LV'] = $noLevelId;
  449. // if (isset($employeeLevelIds[$v['ID']])) {
  450. // $data[$k]['LAST_EMP_LV'] = $employeeLevelIds[$v['ID']]['LEVEL_ID'] ?? 0;
  451. // }
  452. // }
  453. // $_offset += 1;
  454. // \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  455. //
  456. // $data = null;
  457. // goto user;
  458. // }
  459. // $data = null;
  460. // $employeeLevelIds = null;
  461. //
  462. // return true;
  463. // }
  464. public function calcBonusQy($table, $fieldArray, $db): bool
  465. {
  466. if (!$this->_isCalcMonth) {
  467. return true;
  468. }
  469. $periodNum = $this->_periodNum;
  470. $currentPeriod = Period::getInfoByPeriodNum($periodNum);
  471. //判断是否月节点
  472. if ($currentPeriod['IS_MONTH']) {
  473. if (in_array($currentPeriod['CALC_MONTH'], [3,6,9,12])){ // 如果是季度尾
  474. $periodNum = Period::getCurrentQuarterPeriodByPeriodNum($periodNum);
  475. }else{
  476. $periodNum = Period::getCurrentMonthPeriodByPeriodNum($periodNum);
  477. }
  478. }
  479. $field = implode(',', $fieldArray);
  480. $data = CalcBonusQY::find()->select($field)->where(['PERIOD_NUM' => $periodNum])->asArray()->all();
  481. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  482. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入 ".count($data)."条记录");
  483. $data = null;
  484. return true;
  485. }
  486. public function deductReconsume($table, $fieldArray, $db): bool
  487. {
  488. $data = CalcBonus::find()
  489. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  490. ->select(['ID', 'USER_ID', 'CALC_MONTH', 'SUM(RECONSUME_POINTS)', 'CREATED_AT'])
  491. ->groupBy(['USER_ID'])->having(['>', 'SUM(RECONSUME_POINTS)', 0])
  492. ->asArray()
  493. ->all();
  494. if (!empty($data)) {
  495. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  496. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入 ".count($data)."条记录");
  497. }
  498. $data = null;
  499. return true;
  500. }
  501. public function perfPeriod($table, $fieldArray, $db): bool
  502. {
  503. if (!$this->_isCalcMonth) {
  504. return true;
  505. }
  506. $field = implode(',', $fieldArray);
  507. $data = PerfPeriod::find()
  508. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  509. ->select($field)
  510. ->asArray()
  511. ->all();
  512. if (!empty($data)) {
  513. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  514. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入 ".count($data)."条记录");
  515. }
  516. $data = null;
  517. return true;
  518. }
  519. public function perfOrder($table, $fieldArray, $db): bool
  520. {
  521. $_offset = 0;
  522. $countPerfOrder = 0;
  523. $field = "ID,SN,DEC_TYPE, USER_ID,PV,
  524. PERIOD_NUM, CALC_MONTH,DEC_USER_ID,
  525. CREATED_AT,DEC_AMOUNT as ORDER_AMOUNT,PAY_TYPE,FROM_TABLES,PAY_TYPE";
  526. $periodNum = $this->_periodNum;
  527. $currentPeriod = Period::getInfoByPeriodNum($periodNum);
  528. //判断是否月节点
  529. if ($currentPeriod['IS_MONTH']) {
  530. $periodNum = Period::getCurrentMonthPeriodByPeriodNum($periodNum);
  531. }
  532. //结算月
  533. perfOrder:
  534. $offset = $_offset * $this->_limit;
  535. $data = PerfOrder::find()->select($field)->where(['PERIOD_NUM' => $periodNum])
  536. ->offset($offset)->limit($this->_limit)->asArray()->all();
  537. if (!empty($data)) {
  538. $_offset += 1;
  539. $countPerfOrder += count($data);
  540. $fieldArray = array_keys(current($data));
  541. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  542. $data = null;
  543. goto perfOrder;
  544. }
  545. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入 ".$countPerfOrder."条记录");
  546. $data = null;
  547. return true;
  548. }
  549. protected function decOrder($calcMonth, $offset = 0): bool
  550. {
  551. $field = "ID,ORDER_SN as SN,'ZC' as PERF_TYPE, TO_USER_ID as USER_ID,DEC_PV as PV,
  552. PERIOD_NUM, '$calcMonth' as CALC_MONTH,USER_ID as DEC_USER_ID,
  553. CREATED_AT,DEC_AMOUNT as ORDER_AMOUNT, PAID_WALLET as PAY_TYPE,'AR_DEC_ORDER'";
  554. $data = ActiveRecord::findBySql("SELECT $field from AR_DEC_ORDER;")
  555. ->where("PERIOD_NUM=:PERIOD_NUM AND IS_DEL=0 AND TYPE='ZC'", [':PERIOD_NUM' => $this->_periodNum])->asArray()->all();
  556. $this->perfOrderInsert($data);
  557. $data = null;
  558. return true;
  559. }
  560. protected function orderDec($calcMonth, $offset = 0): bool
  561. {
  562. $field = "od.ID,od.SN,'ZC' as TYPE,od.USER_ID,od.PAY_PV,
  563. od.PERIOD_NUM, '$calcMonth' as CALC_MONTH,
  564. u.DEC_ID as DEC_USER_ID,
  565. od.CREATED_AT,od.PAY_AMOUNT as ORDER_AMOUNT, od.PAY_TYPE,'AR_ORDER_DEC'";
  566. $data = ActiveRecord::findBySql("SELECT $field from AR_ORDER_DEC as od left join AR_USER as u on od.USER_ID=u.ID;")
  567. ->where("PERIOD_NUM=:PERIOD_NUM AND IS_DEL=0 AND TYPE='ZC'", [':PERIOD_NUM' => $this->_periodNum])->asArray()->all();
  568. $this->perfOrderInsert($data);
  569. $data = null;
  570. return true;
  571. }
  572. protected function order($calcMonth, $offset = 0): bool
  573. {
  574. $field = "od.ID,od.SN,'FX' as TYPE,od.USER_ID,od.PAY_PV,
  575. od.PERIOD_NUM, '$calcMonth' as CALC_MONTH,
  576. '' as DEC_USER_ID,
  577. od.CREATED_AT,od.PAY_AMOUNT as ORDER_AMOUNT, od.PAY_TYPE,'AR_ORDER'";
  578. $data = ActiveRecord::findBySql("SELECT $field from AR_ORDER as od;")
  579. ->where("PERIOD_NUM=:PERIOD_NUM AND IS_DELETE=0 AND ORDER_TYPE=:ORDER_TYPE",
  580. [':PERIOD_NUM' => $this->_periodNum, ':ORDER_TYPE' => DeclarationForm::TYPE_FX])
  581. ->asArray()->all();
  582. $this->perfOrderInsert($data);
  583. $data = null;
  584. return true;
  585. }
  586. protected function orderShop($calcMonth, $offset = 0): bool
  587. {
  588. $field = "od.ID,od.SN,'FX' as TYPE,od.USER_ID,od.PAY_PV,
  589. od.PERIOD_NUM, '$calcMonth' as CALC_MONTH,
  590. '' as DEC_USER_ID,
  591. od.CREATED_AT,od.PAY_AMOUNT as ORDER_AMOUNT, od.PAY_TYPE,'AR_ORDER_SHOP'";
  592. $data = ActiveRecord::findBySql("SELECT $field from AR_ORDER_SHOP as od;")
  593. ->where("PERIOD_NUM=:PERIOD_NUM AND IS_DELETE=0 AND ORDER_TYPE=:ORDER_TYPE",
  594. [':PERIOD_NUM' => $this->_periodNum, ':ORDER_TYPE' => DeclarationForm::TYPE_FX])
  595. ->asArray()->all();
  596. $this->perfOrderInsert($data);
  597. $data = null;
  598. return true;
  599. }
  600. /**
  601. * @param $data
  602. * @return void
  603. */
  604. private function perfOrderInsert($data): void
  605. {
  606. $db = $this->_calc_db_name;
  607. $table = self::BASE_INFO_METHODS['perfOrder']['table'] ?? '';
  608. $fieldArray = self::BASE_INFO_METHODS['perfOrder']['field'] ?? [];
  609. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  610. }
  611. public function perfMonth($table, $fieldArray, $db)
  612. {
  613. if (!$this->_isCalcMonth && !in_array($this->_calcMonth, [3, 6, 9, 12])) {
  614. return true;
  615. }
  616. $_offset = 0;
  617. $countPerfMonth = 0;
  618. $field = implode(',', $fieldArray);
  619. $months = $this->getMonthsByPeriodNum($this->_periodNum);
  620. //结算月
  621. perfMonth:
  622. $offset = $_offset * $this->_limit;
  623. $data = PerfMonth::find()->where(['CALC_MONTH' => $months])
  624. ->offset($offset)->limit($this->_limit)->select($field)->asArray()->all();
  625. if (!empty($data)) {
  626. $_offset += 1;
  627. $countPerfMonth += count($data);
  628. $fieldArray = array_keys(current($data));
  629. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  630. $data = null;
  631. goto perfMonth;
  632. }
  633. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入 ".$countPerfMonth."条记录");
  634. $data = null;
  635. return true;
  636. }
  637. }