PushBaseDataToCalc.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. 'decOrder' => ['table' => 'AR_DEC_ORDER', 'field' => [
  178. 'ID',
  179. 'DEC_SN',
  180. 'ORDER_SN',
  181. 'USER_ID',
  182. 'TO_USER_ID',
  183. 'TYPE',
  184. 'IS_ADMIN',
  185. 'DEC_AMOUNT',
  186. 'PAID_WALLET',
  187. 'IS_BATCH',
  188. 'REMARK',
  189. 'REC_USER_ID',
  190. 'CON_USER_ID',
  191. 'PERIOD_NUM',
  192. 'P_CALC_MONTH',
  193. 'CALC_MONTH',
  194. 'CREATED_AT',
  195. 'UPDATER',
  196. 'UPDATED_AT',
  197. 'IS_DEL',
  198. 'DELETED_AT',
  199. 'DEC_ID',
  200. 'DETAIL_TYPE',
  201. 'UPGRADE_TYPE',
  202. 'ORI_LV',
  203. 'UPGRADE_LV',
  204. ]],
  205. //--- 团队奖明细表, 月节点的时候,需要将这个结算月的所有数据都同步过来
  206. 'calcBonusQy' => ['separately' => true, 'table' => 'AR_CALC_BONUS_QY', 'field' => [
  207. 'ID',
  208. 'USER_ID',
  209. 'LAST_DEC_LV',
  210. 'LAST_EMP_LV',
  211. 'LAST_CROWN_LV',
  212. 'LAST_STATUS',
  213. 'AMOUNT',
  214. 'PERIOD_NUM',
  215. 'CALC_YEAR',
  216. 'CALC_MONTH',
  217. 'LOGS',
  218. 'CREATED_AT',
  219. 'ORI_BONUS',
  220. 'RECONSUME_POINTS',
  221. 'MANAGE_TAX',
  222. 'ORI_CAPPED_BONUS_QY',
  223. 'IS_ACTIVE',
  224. 'HOPE_CROWN_LV',
  225. 'HOPE_BONUS',
  226. ]],
  227. //--- 用户结算月累计复消积分表,此表为新表,不传数据即代表没有扣除复消积分。 统计此结算月扣除的复消费积分总数,根据calc_bonus表 sum 一下复消积分字段即可。
  228. 'deductReconsume' => ['separately' => true, 'table' => 'AR_DEDUCT_RECONSUME', 'field' => [
  229. 'ID',
  230. 'USER_ID',
  231. 'CALC_MONTH',
  232. 'RECONSUME_POINTS_SUM',
  233. 'CREATED_AT',
  234. ]],
  235. //--- 期业绩表数据. 月节点的时候,计算月业绩,需要业务系统将ar_perf_period表此结算月的所有数据同步过来。
  236. 'perfPeriod' => ['separately' => true, 'table' => 'AR_PERF_PERIOD', 'field' => [
  237. 'ID',
  238. 'USER_ID',
  239. 'FX_AMOUNT_CASH',
  240. 'PV_PCS',
  241. 'PV_PCS_ZC',
  242. 'PV_PCS_FX',
  243. 'PV_PCS_FX_CASH',
  244. 'PV_PCS_FX_POINT',
  245. 'PV_1L',
  246. 'PV_1L_TOUCH',
  247. 'PV_1L_ZC',
  248. 'PV_1L_FX',
  249. 'PV_2L',
  250. 'PV_2L_TOUCH',
  251. 'PV_2L_ZC',
  252. 'PV_2L_FX',
  253. 'PV_3L',
  254. 'PV_3L_TOUCH',
  255. 'PV_3L_ZC',
  256. 'PV_3L_FX',
  257. 'PV_4L',
  258. 'PV_4L_TOUCH',
  259. 'PV_4L_ZC',
  260. 'PV_4L_FX',
  261. 'PV_5L',
  262. 'PV_5L_TOUCH',
  263. 'PV_5L_ZC',
  264. 'PV_5L_FX',
  265. 'SURPLUS_1L',
  266. 'SURPLUS_1L_ZC',
  267. 'SURPLUS_1L_FX',
  268. 'SURPLUS_2L',
  269. 'SURPLUS_2L_ZC',
  270. 'SURPLUS_2L_FX',
  271. 'SURPLUS_3L',
  272. 'SURPLUS_3L_ZC',
  273. 'SURPLUS_3L_FX',
  274. 'SURPLUS_4L',
  275. 'SURPLUS_4L_ZC',
  276. 'SURPLUS_4L_FX',
  277. 'SURPLUS_5L',
  278. 'SURPLUS_5L_ZC',
  279. 'SURPLUS_5L_FX',
  280. 'PERIOD_NUM',
  281. 'CALC_MONTH',
  282. 'CREATED_AT',
  283. 'PV_PSS',
  284. ]],
  285. //--- 期数配置表
  286. 'period' => ['separately' => true, 'table' => 'AR_PERIOD', 'field' => [
  287. 'ID',
  288. 'PERIOD_NUM',
  289. 'CALC_YEAR',
  290. 'CALC_MONTH',
  291. 'IS_MONTH',
  292. 'IS_PERFED',
  293. 'IS_CALCULATED',
  294. 'IS_PERFING',
  295. 'IS_CALCING',
  296. 'PERF_PERCENT',
  297. 'CALC_PERCENT',
  298. 'PERF_STARTED_AT',
  299. 'PERFED_AT',
  300. 'CALCULATE_STARTED_AT',
  301. 'CALCULATED_AT',
  302. 'CREATED_AT',
  303. 'CALC_ID',
  304. ]],
  305. //--- 月业绩表 只有当前结算月为季度最终月时才推
  306. 'perfMonth' => ['separately' => true, 'table' => 'AR_PERF_MONTH', 'field' => [
  307. 'ID',
  308. 'USER_ID',
  309. 'FX_AMOUNT_CASH',
  310. 'PV_PCS',
  311. 'PV_PCS_FX',
  312. 'PV_1L',
  313. 'PV_2L',
  314. 'PV_3L',
  315. 'PV_4L',
  316. 'PV_5L',
  317. 'PV_1L_TOTAL',
  318. 'PV_2L_TOTAL',
  319. 'PV_3L_TOTAL',
  320. 'PV_4L_TOTAL',
  321. 'PV_5L_TOTAL',
  322. 'CALC_MONTH',
  323. 'CREATED_AT',
  324. 'PV_PSS',
  325. 'PV_PSS_TOTAL',
  326. ]],
  327. //--- 推荐网络表
  328. 'userRelation' => ['table' => 'AR_USER_RELATION_NEW', 'field' => [
  329. 'ID',
  330. 'USER_ID',
  331. 'PARENT_UID',
  332. 'TOP_DEEP',
  333. 'CREATED_AT',
  334. ]],
  335. ];
  336. public function __construct($periodNum)
  337. {
  338. parent::__construct($periodNum);
  339. }
  340. public function start(): array
  341. {
  342. try {
  343. //清除业务系统本地数据(业绩相关)
  344. $this->clearBusinessData();
  345. //清理计算服务系统的基本数据表
  346. $this->clearSyncCalcTableData();
  347. //从业务系统推送数据到计算服务系统
  348. $this->initializeBaseInfo();
  349. // $db = $this->_calc_db_name;
  350. //todo 更新进度
  351. // \Yii::$app->$db->createCommand()->update('AR_PERIOD', ['IS_PREPARE' => 1], 'PERIOD_NUM=:PERIOD_NUM', ['PERIOD_NUM' => $this->_periodNum])->execute();
  352. } catch (\Exception $e) {
  353. return $this->fail('msg:' . $e->getMessage() . 'line:' . $e->getLine());
  354. }
  355. return $this->success();
  356. }
  357. public function clearBusinessData()
  358. {
  359. // 周业绩
  360. PerfPeriod::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  361. CalcRecord::record($this->_periodNum, "【基础数据】业务系统,AR_PERF_PERIOD表,已清除当期所有记录");
  362. // 业绩单
  363. // PerfOrder::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  364. // 删除活跃用户
  365. // PerfActiveUser::pageDeleteAll('PERIOD_NUM='.$this->_periodNum.' AND IS_SENT=0 ');
  366. // 月结时要清空的数据
  367. if ($this->_isCalcMonth) {
  368. // 月业绩表
  369. PerfMonth::pageDeleteAll("CALC_MONTH='{$this->_calcYearMonth}'");
  370. CalcRecord::record($this->_periodNum, "【基础数据】业务系统,是月结点,AR_PERF_MONTH表,已清除当月所有记录");
  371. //达标业绩表
  372. PerfStandard::pageDeleteAll("CALC_MONTH='{$this->_calcYearMonth}'");
  373. CalcRecord::record($this->_periodNum, "【基础数据】业务系统,是月结点,AR_PERF_STANDARD表,已清除当月所有记录");
  374. }
  375. }
  376. /**
  377. * 清理配置中的数据表数据
  378. * @return array
  379. */
  380. public function clearSyncCalcTableData(): array
  381. {
  382. $db = $this->_calc_db_name;
  383. $res = [];
  384. foreach (self::BASE_INFO_METHODS as $method => $val) {
  385. if (\Yii::$app->$db->createCommand("TRUNCATE TABLE {$val['table']}")->execute() !== false) {
  386. $res[] = $val['table'];
  387. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$val['table']."表,已清空");
  388. }
  389. }
  390. return $res;
  391. }
  392. public function initializeBaseInfo()
  393. {
  394. $db = $this->_calc_db_name;
  395. foreach (self::BASE_INFO_METHODS as $method => $val) {
  396. $table = self::BASE_INFO_METHODS[$method]['table'] ?? '';
  397. $fieldArray = self::BASE_INFO_METHODS[$method]['field'] ?? [];
  398. if (!empty($table) && !empty($fieldArray)) {
  399. if (!isset($val['separately'])) {
  400. $_offset = 0;
  401. A:
  402. if (isset($val['alias'])) {
  403. //如果业务表字段和计算服务表不同,则配置alias
  404. foreach ($val['alias'] as $org => $v) {
  405. $index = array_search($org, $fieldArray);
  406. $fieldArray[$index] = $v;
  407. }
  408. }
  409. $field = implode(',', $fieldArray);
  410. $model = 'common\models\\' . ucfirst($method);
  411. $data = $model::find()->select($field)->limit($this->_limit)->offset($_offset * $this->_limit)->asArray()->all();
  412. if (!empty($data)) {
  413. $_offset += 1;
  414. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  415. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入".count($data)."条记录");
  416. $data = null;
  417. goto A;
  418. }
  419. $data = null;
  420. } elseif (method_exists($this, $method) && $val['separately']) {
  421. $this->$method($table, $fieldArray, $db);
  422. }
  423. }
  424. }
  425. return true;
  426. }
  427. public function period($table, $fieldArray, $db)
  428. {
  429. $field = implode(',', $fieldArray);
  430. $where = ['PERIOD_NUM' => $this->_periodNum];
  431. if ($this->_isCalcMonth) {
  432. $where = ['CALC_YEAR' => $this->_calcYear, 'CALC_MONTH' => $this->_calcMonth];
  433. if (in_array($this->_calcMonth, [3, 6, 9, 12])) {
  434. $where['CALC_MONTH'] = [$this->_calcMonth, $this->_calcMonth - 1, $this->_calcMonth - 2];
  435. }
  436. }
  437. $data = Period::find()->select($field)
  438. ->where($where)
  439. ->asArray()->all();
  440. if (!empty($data)) {
  441. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  442. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入 ".count($data)."条记录");
  443. }
  444. return true;
  445. }
  446. // public function user($table, $fieldArray, $db)
  447. // {
  448. // $field = '`' . implode('`, `', $fieldArray) . '`,null as LAST_EMP_LV';
  449. // $fieldArray[] = 'LAST_EMP_LV';
  450. // $_offset = 0;
  451. //
  452. // $forwardMonthPeriod = Period::find()
  453. // ->where('IS_MONTH=1 AND PERIOD_NUM<:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  454. // ->orderBy('PERIOD_NUM DESC')->asArray()->one();
  455. //
  456. // $forwardPeriodNum = $forwardMonthPeriod['PERIOD_NUM'];
  457. //
  458. //// $employeeLevelIds = CalcBonusBS::findAllAsArray(
  459. //// 'PERIOD_NUM=:PERIOD_NUM ', [':PERIOD_NUM' => $forwardPeriodNum]
  460. //// );
  461. //
  462. // $employeeLevelIds = CalcBonusBS::find()->where(
  463. // 'PERIOD_NUM=:PERIOD_NUM ', [':PERIOD_NUM' => $forwardPeriodNum]
  464. // )->select('USER_ID,LEVEL_ID')->asArray()->all();
  465. //
  466. // if (!empty($employeeLevelIds)) {
  467. // $employeeLevelIds = array_column($employeeLevelIds, null, 'USER_ID'); // 当前蓝星奖计算(即管理奖) 的等级
  468. // }
  469. //
  470. // $noLevelId = EmployLevel::NO_LEVEL_ID;
  471. //
  472. // user:
  473. // $offset = $_offset * $this->_limit;
  474. // $data = ActiveRecord::findBySql("SELECT $field from AR_USER limit $this->_limit offset $offset;")->asArray()->all();
  475. //
  476. // if (!empty($data)) {
  477. // foreach ($data as $k => $v) {
  478. // $data[$k]['LAST_EMP_LV'] = $noLevelId;
  479. // if (isset($employeeLevelIds[$v['ID']])) {
  480. // $data[$k]['LAST_EMP_LV'] = $employeeLevelIds[$v['ID']]['LEVEL_ID'] ?? 0;
  481. // }
  482. // }
  483. // $_offset += 1;
  484. // \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  485. //
  486. // $data = null;
  487. // goto user;
  488. // }
  489. // $data = null;
  490. // $employeeLevelIds = null;
  491. //
  492. // return true;
  493. // }
  494. public function calcBonusQy($table, $fieldArray, $db): bool
  495. {
  496. if (!$this->_isCalcMonth) {
  497. return true;
  498. }
  499. $periodNum = $this->_periodNum;
  500. $currentPeriod = Period::getInfoByPeriodNum($periodNum);
  501. //判断是否月节点
  502. if ($currentPeriod['IS_MONTH']) {
  503. if (in_array($currentPeriod['CALC_MONTH'], [3,6,9,12])){ // 如果是季度尾
  504. $periodNum = Period::getCurrentQuarterPeriodByPeriodNum($periodNum);
  505. }else{
  506. $periodNum = Period::getCurrentMonthPeriodByPeriodNum($periodNum);
  507. }
  508. }
  509. $field = implode(',', $fieldArray);
  510. $data = CalcBonusQY::find()->select($field)->where(['PERIOD_NUM' => $periodNum])->asArray()->all();
  511. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  512. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入 ".count($data)."条记录");
  513. $data = null;
  514. return true;
  515. }
  516. public function deductReconsume($table, $fieldArray, $db): bool
  517. {
  518. $data = CalcBonus::find()
  519. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  520. ->select(['ID', 'USER_ID', 'CALC_MONTH', 'SUM(RECONSUME_POINTS)', 'CREATED_AT'])
  521. ->groupBy(['USER_ID'])->having(['>', 'SUM(RECONSUME_POINTS)', 0])
  522. ->asArray()
  523. ->all();
  524. if (!empty($data)) {
  525. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  526. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入 ".count($data)."条记录");
  527. }
  528. $data = null;
  529. return true;
  530. }
  531. public function perfPeriod($table, $fieldArray, $db): bool
  532. {
  533. if (!$this->_isCalcMonth) {
  534. return true;
  535. }
  536. $field = implode(',', $fieldArray);
  537. $data = PerfPeriod::find()
  538. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  539. ->select($field)
  540. ->asArray()
  541. ->all();
  542. if (!empty($data)) {
  543. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  544. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入 ".count($data)."条记录");
  545. }
  546. $data = null;
  547. return true;
  548. }
  549. public function perfOrder($table, $fieldArray, $db): bool
  550. {
  551. $_offset = 0;
  552. $countPerfOrder = 0;
  553. $field = "ID,SN,DEC_TYPE, USER_ID,PV,
  554. PERIOD_NUM, CALC_MONTH,DEC_USER_ID,
  555. CREATED_AT,DEC_AMOUNT as ORDER_AMOUNT,PAY_TYPE,FROM_TABLES,PAY_TYPE";
  556. $periodNum = $this->_periodNum;
  557. $currentPeriod = Period::getInfoByPeriodNum($periodNum);
  558. //判断是否月节点
  559. if ($currentPeriod['IS_MONTH']) {
  560. $periodNum = Period::getCurrentMonthPeriodByPeriodNum($periodNum);
  561. }
  562. //结算月
  563. perfOrder:
  564. $offset = $_offset * $this->_limit;
  565. $data = PerfOrder::find()->select($field)->where(['PERIOD_NUM' => $periodNum])
  566. ->offset($offset)->limit($this->_limit)->asArray()->all();
  567. if (!empty($data)) {
  568. $_offset += 1;
  569. $countPerfOrder += count($data);
  570. $fieldArray = array_keys(current($data));
  571. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  572. $data = null;
  573. goto perfOrder;
  574. }
  575. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入 ".$countPerfOrder."条记录");
  576. $data = null;
  577. return true;
  578. }
  579. protected function decOrder($calcMonth, $offset = 0): bool
  580. {
  581. $field = "ID,ORDER_SN as SN,'ZC' as PERF_TYPE, TO_USER_ID as USER_ID,DEC_PV as PV,
  582. PERIOD_NUM, '$calcMonth' as CALC_MONTH,USER_ID as DEC_USER_ID,
  583. CREATED_AT,DEC_AMOUNT as ORDER_AMOUNT, PAID_WALLET as PAY_TYPE,'AR_DEC_ORDER'";
  584. $data = ActiveRecord::findBySql("SELECT $field from AR_DEC_ORDER;")
  585. ->where("PERIOD_NUM=:PERIOD_NUM AND IS_DEL=0 AND TYPE='ZC'", [':PERIOD_NUM' => $this->_periodNum])->asArray()->all();
  586. $this->perfOrderInsert($data);
  587. $data = null;
  588. return true;
  589. }
  590. protected function orderDec($calcMonth, $offset = 0): bool
  591. {
  592. $field = "od.ID,od.SN,'ZC' as TYPE,od.USER_ID,od.PAY_PV,
  593. od.PERIOD_NUM, '$calcMonth' as CALC_MONTH,
  594. u.DEC_ID as DEC_USER_ID,
  595. od.CREATED_AT,od.PAY_AMOUNT as ORDER_AMOUNT, od.PAY_TYPE,'AR_ORDER_DEC'";
  596. $data = ActiveRecord::findBySql("SELECT $field from AR_ORDER_DEC as od left join AR_USER as u on od.USER_ID=u.ID;")
  597. ->where("PERIOD_NUM=:PERIOD_NUM AND IS_DEL=0 AND TYPE='ZC'", [':PERIOD_NUM' => $this->_periodNum])->asArray()->all();
  598. $this->perfOrderInsert($data);
  599. $data = null;
  600. return true;
  601. }
  602. protected function order($calcMonth, $offset = 0): bool
  603. {
  604. $field = "od.ID,od.SN,'FX' as TYPE,od.USER_ID,od.PAY_PV,
  605. od.PERIOD_NUM, '$calcMonth' as CALC_MONTH,
  606. '' as DEC_USER_ID,
  607. od.CREATED_AT,od.PAY_AMOUNT as ORDER_AMOUNT, od.PAY_TYPE,'AR_ORDER'";
  608. $data = ActiveRecord::findBySql("SELECT $field from AR_ORDER as od;")
  609. ->where("PERIOD_NUM=:PERIOD_NUM AND IS_DELETE=0 AND ORDER_TYPE=:ORDER_TYPE",
  610. [':PERIOD_NUM' => $this->_periodNum, ':ORDER_TYPE' => DeclarationForm::TYPE_FX])
  611. ->asArray()->all();
  612. $this->perfOrderInsert($data);
  613. $data = null;
  614. return true;
  615. }
  616. protected function orderShop($calcMonth, $offset = 0): bool
  617. {
  618. $field = "od.ID,od.SN,'FX' as TYPE,od.USER_ID,od.PAY_PV,
  619. od.PERIOD_NUM, '$calcMonth' as CALC_MONTH,
  620. '' as DEC_USER_ID,
  621. od.CREATED_AT,od.PAY_AMOUNT as ORDER_AMOUNT, od.PAY_TYPE,'AR_ORDER_SHOP'";
  622. $data = ActiveRecord::findBySql("SELECT $field from AR_ORDER_SHOP as od;")
  623. ->where("PERIOD_NUM=:PERIOD_NUM AND IS_DELETE=0 AND ORDER_TYPE=:ORDER_TYPE",
  624. [':PERIOD_NUM' => $this->_periodNum, ':ORDER_TYPE' => DeclarationForm::TYPE_FX])
  625. ->asArray()->all();
  626. $this->perfOrderInsert($data);
  627. $data = null;
  628. return true;
  629. }
  630. /**
  631. * @param $data
  632. * @return void
  633. */
  634. private function perfOrderInsert($data): void
  635. {
  636. $db = $this->_calc_db_name;
  637. $table = self::BASE_INFO_METHODS['perfOrder']['table'] ?? '';
  638. $fieldArray = self::BASE_INFO_METHODS['perfOrder']['field'] ?? [];
  639. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  640. }
  641. public function perfMonth($table, $fieldArray, $db)
  642. {
  643. if (!$this->_isCalcMonth && !in_array($this->_calcMonth, [3, 6, 9, 12])) {
  644. return true;
  645. }
  646. $_offset = 0;
  647. $countPerfMonth = 0;
  648. $field = implode(',', $fieldArray);
  649. $months = $this->getMonthsByPeriodNum($this->_periodNum);
  650. //结算月
  651. perfMonth:
  652. $offset = $_offset * $this->_limit;
  653. $data = PerfMonth::find()->where(['CALC_MONTH' => $months])
  654. ->offset($offset)->limit($this->_limit)->select($field)->asArray()->all();
  655. if (!empty($data)) {
  656. $_offset += 1;
  657. $countPerfMonth += count($data);
  658. $fieldArray = array_keys(current($data));
  659. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  660. $data = null;
  661. goto perfMonth;
  662. }
  663. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入 ".$countPerfMonth."条记录");
  664. $data = null;
  665. return true;
  666. }
  667. }