PushBaseDataToCalc.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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. 'CALC_ID',
  275. ]],
  276. //--- 月业绩表 只有当前结算月为季度最终月时才推
  277. 'perfMonth' => ['separately' => true, 'table' => 'AR_PERF_MONTH', 'field' => [
  278. 'ID',
  279. 'USER_ID',
  280. 'FX_AMOUNT_CASH',
  281. 'PV_PCS',
  282. 'PV_PCS_FX',
  283. 'PV_1L',
  284. 'PV_2L',
  285. 'PV_3L',
  286. 'PV_4L',
  287. 'PV_5L',
  288. 'PV_1L_TOTAL',
  289. 'PV_2L_TOTAL',
  290. 'PV_3L_TOTAL',
  291. 'PV_4L_TOTAL',
  292. 'PV_5L_TOTAL',
  293. 'CALC_MONTH',
  294. 'CREATED_AT',
  295. 'PV_PSS',
  296. 'PV_PSS_TOTAL',
  297. ]],
  298. //--- 推荐网络表
  299. 'userRelation' => ['table' => 'AR_USER_RELATION_NEW', 'field' => [
  300. 'ID',
  301. 'USER_ID',
  302. 'PARENT_UID',
  303. 'TOP_DEEP',
  304. 'CREATED_AT',
  305. ]],
  306. ];
  307. public function __construct($periodNum)
  308. {
  309. parent::__construct($periodNum);
  310. }
  311. public function start(): array
  312. {
  313. try {
  314. //清除业务系统本地数据(业绩相关)
  315. $this->clearBusinessData();
  316. //清理计算服务系统的基本数据表
  317. $this->clearSyncCalcTableData();
  318. //从业务系统推送数据到计算服务系统
  319. $this->initializeBaseInfo();
  320. // $db = $this->_calc_db_name;
  321. //todo 更新进度
  322. // \Yii::$app->$db->createCommand()->update('AR_PERIOD', ['IS_PREPARE' => 1], 'PERIOD_NUM=:PERIOD_NUM', ['PERIOD_NUM' => $this->_periodNum])->execute();
  323. } catch (\Exception $e) {
  324. return $this->fail('msg:' . $e->getMessage() . 'line:' . $e->getLine());
  325. }
  326. return $this->success();
  327. }
  328. public function clearBusinessData()
  329. {
  330. // 周业绩
  331. PerfPeriod::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  332. CalcRecord::record($this->_periodNum, "【基础数据】业务系统,AR_PERF_PERIOD表,已清除当期所有记录");
  333. // 业绩单
  334. // PerfOrder::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  335. // 删除活跃用户
  336. // PerfActiveUser::pageDeleteAll('PERIOD_NUM='.$this->_periodNum.' AND IS_SENT=0 ');
  337. // 月结时要清空的数据
  338. if ($this->_isCalcMonth) {
  339. // 月业绩表
  340. PerfMonth::pageDeleteAll("CALC_MONTH='{$this->_calcYearMonth}'");
  341. CalcRecord::record($this->_periodNum, "【基础数据】业务系统,是月结点,AR_PERF_MONTH表,已清除当月所有记录");
  342. //达标业绩表
  343. PerfStandard::pageDeleteAll("CALC_MONTH='{$this->_calcYearMonth}'");
  344. CalcRecord::record($this->_periodNum, "【基础数据】业务系统,是月结点,AR_PERF_STANDARD表,已清除当月所有记录");
  345. }
  346. }
  347. /**
  348. * 清理配置中的数据表数据
  349. * @return array
  350. */
  351. public function clearSyncCalcTableData(): array
  352. {
  353. $db = $this->_calc_db_name;
  354. $res = [];
  355. foreach (self::BASE_INFO_METHODS as $method => $val) {
  356. if (\Yii::$app->$db->createCommand("TRUNCATE TABLE {$val['table']}")->execute() !== false) {
  357. $res[] = $val['table'];
  358. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$val['table']."表,已清空");
  359. }
  360. }
  361. return $res;
  362. }
  363. public function initializeBaseInfo()
  364. {
  365. $db = $this->_calc_db_name;
  366. foreach (self::BASE_INFO_METHODS as $method => $val) {
  367. $table = self::BASE_INFO_METHODS[$method]['table'] ?? '';
  368. $fieldArray = self::BASE_INFO_METHODS[$method]['field'] ?? [];
  369. if (!empty($table) && !empty($fieldArray)) {
  370. if (!isset($val['separately'])) {
  371. $_offset = 0;
  372. A:
  373. if (isset($val['alias'])) {
  374. //如果业务表字段和计算服务表不同,则配置alias
  375. foreach ($val['alias'] as $org => $v) {
  376. $index = array_search($org, $fieldArray);
  377. $fieldArray[$index] = $v;
  378. }
  379. }
  380. $field = implode(',', $fieldArray);
  381. $model = 'common\models\\' . ucfirst($method);
  382. $data = $model::find()->select($field)->limit($this->_limit)->offset($_offset * $this->_limit)->asArray()->all();
  383. if (!empty($data)) {
  384. $_offset += 1;
  385. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  386. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入".count($data)."条记录");
  387. $data = null;
  388. goto A;
  389. }
  390. $data = null;
  391. } elseif (method_exists($this, $method) && $val['separately']) {
  392. $this->$method($table, $fieldArray, $db);
  393. }
  394. }
  395. }
  396. return true;
  397. }
  398. public function period($table, $fieldArray, $db)
  399. {
  400. $field = implode(',', $fieldArray);
  401. $where = ['PERIOD_NUM' => $this->_periodNum];
  402. if ($this->_isCalcMonth) {
  403. $where = ['CALC_YEAR' => $this->_calcYear, 'CALC_MONTH' => $this->_calcMonth];
  404. if (in_array($this->_calcMonth, [3, 6, 9, 12])) {
  405. $where['CALC_MONTH'] = [$this->_calcMonth, $this->_calcMonth - 1, $this->_calcMonth - 2];
  406. }
  407. }
  408. $data = Period::find()->select($field)
  409. ->where($where)
  410. ->asArray()->all();
  411. if (!empty($data)) {
  412. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  413. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入 ".count($data)."条记录");
  414. }
  415. return true;
  416. }
  417. // public function user($table, $fieldArray, $db)
  418. // {
  419. // $field = '`' . implode('`, `', $fieldArray) . '`,null as LAST_EMP_LV';
  420. // $fieldArray[] = 'LAST_EMP_LV';
  421. // $_offset = 0;
  422. //
  423. // $forwardMonthPeriod = Period::find()
  424. // ->where('IS_MONTH=1 AND PERIOD_NUM<:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  425. // ->orderBy('PERIOD_NUM DESC')->asArray()->one();
  426. //
  427. // $forwardPeriodNum = $forwardMonthPeriod['PERIOD_NUM'];
  428. //
  429. //// $employeeLevelIds = CalcBonusBS::findAllAsArray(
  430. //// 'PERIOD_NUM=:PERIOD_NUM ', [':PERIOD_NUM' => $forwardPeriodNum]
  431. //// );
  432. //
  433. // $employeeLevelIds = CalcBonusBS::find()->where(
  434. // 'PERIOD_NUM=:PERIOD_NUM ', [':PERIOD_NUM' => $forwardPeriodNum]
  435. // )->select('USER_ID,LEVEL_ID')->asArray()->all();
  436. //
  437. // if (!empty($employeeLevelIds)) {
  438. // $employeeLevelIds = array_column($employeeLevelIds, null, 'USER_ID'); // 当前蓝星奖计算(即管理奖) 的等级
  439. // }
  440. //
  441. // $noLevelId = EmployLevel::NO_LEVEL_ID;
  442. //
  443. // user:
  444. // $offset = $_offset * $this->_limit;
  445. // $data = ActiveRecord::findBySql("SELECT $field from AR_USER limit $this->_limit offset $offset;")->asArray()->all();
  446. //
  447. // if (!empty($data)) {
  448. // foreach ($data as $k => $v) {
  449. // $data[$k]['LAST_EMP_LV'] = $noLevelId;
  450. // if (isset($employeeLevelIds[$v['ID']])) {
  451. // $data[$k]['LAST_EMP_LV'] = $employeeLevelIds[$v['ID']]['LEVEL_ID'] ?? 0;
  452. // }
  453. // }
  454. // $_offset += 1;
  455. // \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  456. //
  457. // $data = null;
  458. // goto user;
  459. // }
  460. // $data = null;
  461. // $employeeLevelIds = null;
  462. //
  463. // return true;
  464. // }
  465. public function calcBonusQy($table, $fieldArray, $db): bool
  466. {
  467. if (!$this->_isCalcMonth) {
  468. return true;
  469. }
  470. $periodNum = $this->_periodNum;
  471. $currentPeriod = Period::getInfoByPeriodNum($periodNum);
  472. //判断是否月节点
  473. if ($currentPeriod['IS_MONTH']) {
  474. if (in_array($currentPeriod['CALC_MONTH'], [3,6,9,12])){ // 如果是季度尾
  475. $periodNum = Period::getCurrentQuarterPeriodByPeriodNum($periodNum);
  476. }else{
  477. $periodNum = Period::getCurrentMonthPeriodByPeriodNum($periodNum);
  478. }
  479. }
  480. $field = implode(',', $fieldArray);
  481. $data = CalcBonusQY::find()->select($field)->where(['PERIOD_NUM' => $periodNum])->asArray()->all();
  482. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  483. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入 ".count($data)."条记录");
  484. $data = null;
  485. return true;
  486. }
  487. public function deductReconsume($table, $fieldArray, $db): bool
  488. {
  489. $data = CalcBonus::find()
  490. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  491. ->select(['ID', 'USER_ID', 'CALC_MONTH', 'SUM(RECONSUME_POINTS)', 'CREATED_AT'])
  492. ->groupBy(['USER_ID'])->having(['>', 'SUM(RECONSUME_POINTS)', 0])
  493. ->asArray()
  494. ->all();
  495. if (!empty($data)) {
  496. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  497. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入 ".count($data)."条记录");
  498. }
  499. $data = null;
  500. return true;
  501. }
  502. public function perfPeriod($table, $fieldArray, $db): bool
  503. {
  504. if (!$this->_isCalcMonth) {
  505. return true;
  506. }
  507. $field = implode(',', $fieldArray);
  508. $data = PerfPeriod::find()
  509. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  510. ->select($field)
  511. ->asArray()
  512. ->all();
  513. if (!empty($data)) {
  514. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  515. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入 ".count($data)."条记录");
  516. }
  517. $data = null;
  518. return true;
  519. }
  520. public function perfOrder($table, $fieldArray, $db): bool
  521. {
  522. $_offset = 0;
  523. $countPerfOrder = 0;
  524. $field = "ID,SN,DEC_TYPE, USER_ID,PV,
  525. PERIOD_NUM, CALC_MONTH,DEC_USER_ID,
  526. CREATED_AT,DEC_AMOUNT as ORDER_AMOUNT,PAY_TYPE,FROM_TABLES,PAY_TYPE";
  527. $periodNum = $this->_periodNum;
  528. $currentPeriod = Period::getInfoByPeriodNum($periodNum);
  529. //判断是否月节点
  530. if ($currentPeriod['IS_MONTH']) {
  531. $periodNum = Period::getCurrentMonthPeriodByPeriodNum($periodNum);
  532. }
  533. //结算月
  534. perfOrder:
  535. $offset = $_offset * $this->_limit;
  536. $data = PerfOrder::find()->select($field)->where(['PERIOD_NUM' => $periodNum])
  537. ->offset($offset)->limit($this->_limit)->asArray()->all();
  538. if (!empty($data)) {
  539. $_offset += 1;
  540. $countPerfOrder += count($data);
  541. $fieldArray = array_keys(current($data));
  542. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  543. $data = null;
  544. goto perfOrder;
  545. }
  546. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入 ".$countPerfOrder."条记录");
  547. $data = null;
  548. return true;
  549. }
  550. protected function decOrder($calcMonth, $offset = 0): bool
  551. {
  552. $field = "ID,ORDER_SN as SN,'ZC' as PERF_TYPE, TO_USER_ID as USER_ID,DEC_PV as PV,
  553. PERIOD_NUM, '$calcMonth' as CALC_MONTH,USER_ID as DEC_USER_ID,
  554. CREATED_AT,DEC_AMOUNT as ORDER_AMOUNT, PAID_WALLET as PAY_TYPE,'AR_DEC_ORDER'";
  555. $data = ActiveRecord::findBySql("SELECT $field from AR_DEC_ORDER;")
  556. ->where("PERIOD_NUM=:PERIOD_NUM AND IS_DEL=0 AND TYPE='ZC'", [':PERIOD_NUM' => $this->_periodNum])->asArray()->all();
  557. $this->perfOrderInsert($data);
  558. $data = null;
  559. return true;
  560. }
  561. protected function orderDec($calcMonth, $offset = 0): bool
  562. {
  563. $field = "od.ID,od.SN,'ZC' as TYPE,od.USER_ID,od.PAY_PV,
  564. od.PERIOD_NUM, '$calcMonth' as CALC_MONTH,
  565. u.DEC_ID as DEC_USER_ID,
  566. od.CREATED_AT,od.PAY_AMOUNT as ORDER_AMOUNT, od.PAY_TYPE,'AR_ORDER_DEC'";
  567. $data = ActiveRecord::findBySql("SELECT $field from AR_ORDER_DEC as od left join AR_USER as u on od.USER_ID=u.ID;")
  568. ->where("PERIOD_NUM=:PERIOD_NUM AND IS_DEL=0 AND TYPE='ZC'", [':PERIOD_NUM' => $this->_periodNum])->asArray()->all();
  569. $this->perfOrderInsert($data);
  570. $data = null;
  571. return true;
  572. }
  573. protected function order($calcMonth, $offset = 0): bool
  574. {
  575. $field = "od.ID,od.SN,'FX' as TYPE,od.USER_ID,od.PAY_PV,
  576. od.PERIOD_NUM, '$calcMonth' as CALC_MONTH,
  577. '' as DEC_USER_ID,
  578. od.CREATED_AT,od.PAY_AMOUNT as ORDER_AMOUNT, od.PAY_TYPE,'AR_ORDER'";
  579. $data = ActiveRecord::findBySql("SELECT $field from AR_ORDER as od;")
  580. ->where("PERIOD_NUM=:PERIOD_NUM AND IS_DELETE=0 AND ORDER_TYPE=:ORDER_TYPE",
  581. [':PERIOD_NUM' => $this->_periodNum, ':ORDER_TYPE' => DeclarationForm::TYPE_FX])
  582. ->asArray()->all();
  583. $this->perfOrderInsert($data);
  584. $data = null;
  585. return true;
  586. }
  587. protected function orderShop($calcMonth, $offset = 0): bool
  588. {
  589. $field = "od.ID,od.SN,'FX' as TYPE,od.USER_ID,od.PAY_PV,
  590. od.PERIOD_NUM, '$calcMonth' as CALC_MONTH,
  591. '' as DEC_USER_ID,
  592. od.CREATED_AT,od.PAY_AMOUNT as ORDER_AMOUNT, od.PAY_TYPE,'AR_ORDER_SHOP'";
  593. $data = ActiveRecord::findBySql("SELECT $field from AR_ORDER_SHOP as od;")
  594. ->where("PERIOD_NUM=:PERIOD_NUM AND IS_DELETE=0 AND ORDER_TYPE=:ORDER_TYPE",
  595. [':PERIOD_NUM' => $this->_periodNum, ':ORDER_TYPE' => DeclarationForm::TYPE_FX])
  596. ->asArray()->all();
  597. $this->perfOrderInsert($data);
  598. $data = null;
  599. return true;
  600. }
  601. /**
  602. * @param $data
  603. * @return void
  604. */
  605. private function perfOrderInsert($data): void
  606. {
  607. $db = $this->_calc_db_name;
  608. $table = self::BASE_INFO_METHODS['perfOrder']['table'] ?? '';
  609. $fieldArray = self::BASE_INFO_METHODS['perfOrder']['field'] ?? [];
  610. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  611. }
  612. public function perfMonth($table, $fieldArray, $db)
  613. {
  614. if (!$this->_isCalcMonth && !in_array($this->_calcMonth, [3, 6, 9, 12])) {
  615. return true;
  616. }
  617. $_offset = 0;
  618. $countPerfMonth = 0;
  619. $field = implode(',', $fieldArray);
  620. $months = $this->getMonthsByPeriodNum($this->_periodNum);
  621. //结算月
  622. perfMonth:
  623. $offset = $_offset * $this->_limit;
  624. $data = PerfMonth::find()->where(['CALC_MONTH' => $months])
  625. ->offset($offset)->limit($this->_limit)->select($field)->asArray()->all();
  626. if (!empty($data)) {
  627. $_offset += 1;
  628. $countPerfMonth += count($data);
  629. $fieldArray = array_keys(current($data));
  630. \Yii::$app->$db->createCommand()->batchInsert($table, $fieldArray, $data)->execute();
  631. $data = null;
  632. goto perfMonth;
  633. }
  634. CalcRecord::record($this->_periodNum, "【基础数据】计算服务系统,".$table."表,插入 ".$countPerfMonth."条记录");
  635. $data = null;
  636. return true;
  637. }
  638. }