CalcConsole.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace common\helpers\bonus\Calc;
  3. use common\models\CalcRecord;
  4. use common\models\Period;
  5. class CalcConsole extends BaseBusiness
  6. {
  7. const CALC_DB_NAME = 'dbCalcServer';
  8. public static function listenCalcPeriod()
  9. {
  10. $db = self::CALC_DB_NAME;
  11. $allPeriod = \Yii::$app->$db->createCommand("SELECT * FROM AR_PERIOD order by PERIOD_NUM desc")->queryAll();
  12. $period = [];
  13. foreach ($allPeriod as $v) {
  14. if ($v['IS_PREPARE'] > 0) {
  15. $period = $v;
  16. break;
  17. }
  18. }
  19. if (empty($period)) {
  20. return true;
  21. }
  22. $businessPeriod = Period::find()->where(['PERIOD_NUM' => $period['PERIOD_NUM']])
  23. ->asArray()->one();
  24. if ($businessPeriod['SEND_STARTED_AT'] > 0 || $businessPeriod['IS_SENT'] > 0) {
  25. //开始挂网 或者 已挂网 则直接返回
  26. return true;
  27. }
  28. //用户选择是否自动执行
  29. $autoExec = $businessPeriod['AUTO_EXEC'] > Period::MANUAL_EXEC_CALC;
  30. $businessIsCalculated = $businessPeriod['IS_CALCULATED'] == Period::CALCULATE_FINISH;
  31. $startExecTime = $businessPeriod['START_EXEC_TIME'] ?? 0;
  32. echo $businessPeriod['IS_CALCING'];
  33. //同步计算进度日志到业务系统
  34. if (4 == $period['IS_PREPARE'] && 1 == $period['IS_CALCULATED'] && 1 == $businessPeriod['IS_CALCING']) {
  35. self::syncCalcRecord($period['PERIOD_NUM'], $db, $startExecTime);
  36. \Yii::$app->db->createCommand()->update('AR_PERIOD', ['IS_CALCING' => 0], 'PERIOD_NUM=' . $period['PERIOD_NUM'])->execute();
  37. }
  38. if (2 == $period['IS_PREPARE'] && 1 == $period['IS_PERFED'] && $autoExec) {
  39. //拉取期业绩
  40. CalcRecord::record($period['PERIOD_NUM'], '第' . $period['PERIOD_NUM'] . '期的业绩数据已生成');
  41. CalcRecord::record($period['PERIOD_NUM'], '开始获取第' . $period['PERIOD_NUM'] . '期的期业绩数据');
  42. Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::IS_PROCESSING);
  43. $res = (new PullPerfDataFromCalc($period['PERIOD_NUM']))->start();
  44. if (200 == $res['code']) {
  45. Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::NOT_PROCESSING);
  46. CalcRecord::record($period['PERIOD_NUM'], '第' . $period['PERIOD_NUM'] . '期的期业绩数据已获取');
  47. } else {
  48. //结束计算状态
  49. Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::NOT_PROCESSING);
  50. CalcRecord::record($period['PERIOD_NUM'], '第' . $period['PERIOD_NUM'] . '期的期业绩数据获取失败,原因:' . $res['msg']);
  51. return $res;
  52. }
  53. //自动执行且IS_PREPARE为3 直接开始结算流程
  54. CalcRecord::record($period['PERIOD_NUM'], '开始计算第' . $period['PERIOD_NUM'] . '期的奖金');
  55. //同步周期表的值到业务系统
  56. self::pullPeriodForUpdate($period['PERIOD_NUM']);
  57. return \Yii::$app->$db->createCommand()->update('AR_PERIOD', ['IS_PREPARE' => 3], 'PERIOD_NUM=:PERIOD_NUM', ['PERIOD_NUM' => $period['PERIOD_NUM']])->execute();
  58. }
  59. if (4 == $period['IS_PREPARE'] && 1 == $period['IS_CALCULATED'] && $autoExec && !$businessIsCalculated) {
  60. CalcRecord::record($period['PERIOD_NUM'], '第' . $period['PERIOD_NUM'] . '期的奖金已计算完成');
  61. CalcRecord::record($period['PERIOD_NUM'], '开始拉取第' . $period['PERIOD_NUM'] . '期的奖金数据');
  62. Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::IS_PROCESSING);
  63. //自动执行且IS_CALCULATED为1 直接开始拉取结算数据
  64. $res = (new PullCalcBonusData($period['PERIOD_NUM']))->start();
  65. if (200 == $res['code']) {
  66. CalcRecord::record($period['PERIOD_NUM'], '第' . $period['PERIOD_NUM'] . '期的奖金数据已全部拉取');
  67. //待拉取完成后再将计算完成标识改为已完成
  68. \Yii::$app->db->createCommand()->update('AR_PERIOD', ['IS_CALCULATED' => 1], 'PERIOD_NUM=' . $period['PERIOD_NUM'])->execute();
  69. //自动执行完成 更新对应字段
  70. //计算结束
  71. Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::NOT_PROCESSING);
  72. //流程结束
  73. //同步周期表的值到业务系统
  74. self::pullPeriodForUpdate($period['PERIOD_NUM']);
  75. //拉取计算系统的进度日志
  76. self::syncCalcRecord($period['PERIOD_NUM'], $db, $startExecTime);
  77. } else {
  78. //结束计算状态
  79. Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::NOT_PROCESSING);
  80. CalcRecord::record($period['PERIOD_NUM'], '第' . $period['PERIOD_NUM'] . '期的奖金数据获取失败,原因:' . $res['msg']);
  81. return $res;
  82. }
  83. return true;
  84. }
  85. return true;
  86. }
  87. public static function listenAutoPerfPeriod()
  88. {
  89. $db = self::CALC_DB_NAME;
  90. $allPeriod = \Yii::$app->$db->createCommand("SELECT * FROM AR_PERIOD order by PERIOD_NUM desc")->queryAll();
  91. $period = [];
  92. foreach ($allPeriod as $v) {
  93. if ($v['IS_PREPARE'] > 0) {
  94. $period = $v;
  95. break;
  96. }
  97. }
  98. if (empty($period)) {
  99. return true;
  100. }
  101. $businessPeriod = Period::find()->where(['PERIOD_NUM' => $period['PERIOD_NUM']])
  102. ->asArray()->one();
  103. if ($businessPeriod['SEND_STARTED_AT'] > 0 || $businessPeriod['IS_SENT'] > 0) {
  104. //开始挂网 或者 已挂网 则直接返回
  105. return true;
  106. }
  107. if (2 == $period['IS_PREPARE'] && 1 == $period['IS_PERFED']) {
  108. //拉取期业绩
  109. CalcRecord::record($period['PERIOD_NUM'], '第' . $period['PERIOD_NUM'] . '期的预计算业绩数据已生成');
  110. CalcRecord::record($period['PERIOD_NUM'], '开始获取第' . $period['PERIOD_NUM'] . '期的预计算期业绩数据');
  111. Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::IS_PROCESSING);
  112. $res = (new PullPerfDataFromCalc($period['PERIOD_NUM']))->start();
  113. if (200 == $res['code']) {
  114. Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::NOT_PROCESSING);
  115. CalcRecord::record($period['PERIOD_NUM'], '第' . $period['PERIOD_NUM'] . '期的预计算期业绩数据已获取');
  116. return $res;
  117. } else {
  118. //结束计算状态
  119. Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::NOT_PROCESSING);
  120. CalcRecord::record($period['PERIOD_NUM'], '第' . $period['PERIOD_NUM'] . '期的预计算期业绩数据获取失败,原因:' . $res['msg']);
  121. return $res;
  122. }
  123. }
  124. return true;
  125. }
  126. /**
  127. * 同步计算系统的计算进度日志到业务系统计算步骤表
  128. * @param $periodNum
  129. * @param $db
  130. * @param $startTime
  131. * @return bool|int
  132. * @throws \yii\base\InvalidConfigException
  133. * @throws \yii\db\Exception
  134. */
  135. public static function syncCalcRecord($periodNum, $db, $startTime = 0)
  136. {
  137. $type = 'bonus';
  138. $calcTaskDesc = \Yii::$app->$db->createCommand("SELECT
  139. ID,PERIOD_NUM,TASK_DESCRIBE,START_AT FROM AR_SERVE_PROCESS
  140. where TASK_TYPE='$type' AND PERIOD_NUM=$periodNum AND START_AT>=$startTime
  141. order by START_AT desc")->queryAll();
  142. $calcTaskDescKeys = array_column($calcTaskDesc, null, 'ID');
  143. $currentRecordList = CalcRecord::find()->where(['PERIOD_NUM' => $periodNum])->select('ID')->asArray()->column();
  144. $currentRecordListKey = array_flip($currentRecordList);
  145. $tableName = (new CalcRecord())->getTableSchema()->name;
  146. $result = array_diff_key($calcTaskDescKeys, $currentRecordListKey);
  147. if (!empty($result)) {
  148. return \Yii::$app->db->createCommand()->batchInsert($tableName, ['ID', 'PERIOD_NUM', 'TEXT', 'CREATED_AT'], $result)->execute();
  149. }
  150. return true;
  151. }
  152. }