CalcConsole.php 8.6 KB

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