CalcConsole.php 9.6 KB

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