CalcConsole.php 8.8 KB

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