CalcConsole.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace common\helpers\bonus\Calc;
  3. use common\helpers\bonus\BonusCalc;
  4. use common\helpers\Cache;
  5. use common\helpers\Form;
  6. use common\models\CalcOperation;
  7. use common\models\CalcRecord;
  8. use common\models\forms\PeriodForm;
  9. use common\models\Period;
  10. use Yii;
  11. /**
  12. * 业务端计算服务监听脚本
  13. */
  14. class CalcConsole extends BaseBusiness
  15. {
  16. const CALC_DB_NAME = 'dbCalcServer';
  17. /**监听计算服务
  18. * @return array|bool
  19. * @throws \yii\base\InvalidConfigException
  20. * @throws \yii\db\Exception
  21. */
  22. public static function listenCalcPeriod()
  23. {
  24. // 先检查redis的开关
  25. $calcingSwitch = Yii::$app->cache->get('isCalcing') ? 1 : 0;
  26. if(!$calcingSwitch){
  27. return true;
  28. }
  29. $db = self::CALC_DB_NAME;
  30. $allPeriod = \Yii::$app->$db->createCommand("SELECT * FROM AR_PERIOD order by PERIOD_NUM desc")->queryAll();
  31. $period = [];
  32. foreach ($allPeriod as $v) {
  33. if ($v['IS_PREPARE'] > 0) {
  34. $period = $v;
  35. break;
  36. }
  37. }
  38. if (empty($period)) {
  39. return true;
  40. }
  41. $businessPeriod = Period::find()->where(['PERIOD_NUM' => $period['PERIOD_NUM']])
  42. ->asArray()->one();
  43. if(!$period['CALC_ID'] || $period['CALC_ID'] != $businessPeriod['CALC_ID']) { // 如果计算ID不符,则退出
  44. return true;
  45. }
  46. if (empty($businessPeriod)) {
  47. return true;
  48. }
  49. if ($businessPeriod['SEND_STARTED_AT'] > 0 || $businessPeriod['IS_SENT'] > 0) {
  50. //开始挂网 或者 已挂网 则直接返回
  51. return true;
  52. }
  53. // if ($businessPeriod['IS_CLOSED']==0) {
  54. // // 如果没有封期 则直接返回
  55. // return true;
  56. // }
  57. //用户选择是否自动执行
  58. $autoExec = $businessPeriod['AUTO_EXEC'] > Period::MANUAL_EXEC_CALC;
  59. $businessIsCalculated = $businessPeriod['IS_CALCULATED'] == Period::CALCULATE_FINISH;
  60. $startExecTime = $businessPeriod['START_EXEC_TIME'] ?? 0;
  61. $businessIsProcessing = $businessPeriod['IS_PROCESSING'] ?? 0;
  62. $isPreparing = $businessPeriod['IS_PREPARING'];
  63. echo $businessPeriod['IS_CALCING'];
  64. //同步计算进度日志到业务系统
  65. if (4 == $period['IS_PREPARE'] && 1 == $period['IS_CALCULATED'] && 1 == $businessPeriod['IS_CALCING']) {
  66. self::syncLogRecord($period['PERIOD_NUM'], $db, $startExecTime);
  67. \Yii::$app->db->createCommand()->update('AR_PERIOD', ['IS_CALCING' => 0], 'PERIOD_NUM=' . $period['PERIOD_NUM'])->execute();
  68. }
  69. if (2 == $period['IS_PREPARE'] && 1 == $period['IS_PERFED'] && ($autoExec || $isPreparing) && !$businessIsProcessing) {
  70. //拉取期业绩
  71. CalcRecord::record($period['PERIOD_NUM'], '【期业绩】第' . $period['PERIOD_NUM'] . '期的业绩数据已生成');
  72. CalcRecord::record($period['PERIOD_NUM'], '【期业绩】开始获取第' . $period['PERIOD_NUM'] . '期的期业绩数据');
  73. Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::IS_PROCESSING);
  74. $res = (new PullPerfDataFromCalc($period['PERIOD_NUM']))->start();
  75. if (200 == $res['code']) {
  76. Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::NOT_PROCESSING);
  77. CalcRecord::record($period['PERIOD_NUM'], '【期业绩】第' . $period['PERIOD_NUM'] . '期的期业绩数据已获取');
  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. //自动执行且IS_PREPARE为3 直接开始结算流程
  85. CalcRecord::record($period['PERIOD_NUM'], '【奖金计算】开始计算第' . $period['PERIOD_NUM'] . '期的奖金');
  86. //同步周期表的值到业务系统
  87. self::pullPeriodForUpdate($period['PERIOD_NUM']);
  88. return \Yii::$app->$db->createCommand()->update('AR_PERIOD', ['IS_PREPARE' => 3], 'PERIOD_NUM=:PERIOD_NUM', ['PERIOD_NUM' => $period['PERIOD_NUM']])->execute();
  89. }
  90. if (4 == $period['IS_PREPARE'] && 1 == $period['IS_CALCULATED'] && ($autoExec || $isPreparing) && !$businessIsProcessing && !$businessIsCalculated) {
  91. CalcRecord::record($period['PERIOD_NUM'], '【奖金计算】第' . $period['PERIOD_NUM'] . '期的奖金已计算完成');
  92. CalcRecord::record($period['PERIOD_NUM'], '【奖金计算】开始拉取第' . $period['PERIOD_NUM'] . '期的奖金数据');
  93. Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::IS_PROCESSING);
  94. //自动执行且IS_CALCULATED为1 直接开始拉取结算数据
  95. $res = (new PullCalcBonusData($period['PERIOD_NUM']))->start();
  96. if (200 == $res['code']) {
  97. CalcRecord::record($period['PERIOD_NUM'], '【奖金计算】第' . $period['PERIOD_NUM'] . '期的奖金数据已全部拉取');
  98. //待拉取完成后再将计算完成标识改为已完成
  99. \Yii::$app->db->createCommand()->update('AR_PERIOD', ['IS_CALCULATED' => 1], 'PERIOD_NUM=' . $period['PERIOD_NUM'])->execute();
  100. //自动执行完成 更新对应字段
  101. //计算结束
  102. Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::NOT_PROCESSING);
  103. //流程结束
  104. //同步周期表的值到业务系统
  105. self::pullPeriodForUpdate($period['PERIOD_NUM']);
  106. \Yii::$app->$db->createCommand()->update('AR_PERIOD', ['CALC_ID' => ''], 'PERIOD_NUM=:PERIOD_NUM', ['PERIOD_NUM' => $period['PERIOD_NUM']])->execute();
  107. CalcOperation::closeOperation($businessPeriod['CALC_ID']);
  108. //拉取计算系统的进度日志
  109. self::syncLogRecord($period['PERIOD_NUM'], $db, $startExecTime);
  110. //设置计算进行中标识
  111. Period::updatePeriodIsPreparing($period['PERIOD_NUM'], Period::NOT_PREPARING);
  112. //设置redis开关
  113. Yii::$app->cache->set('isCalcing', 0);
  114. } else {
  115. //结束计算状态
  116. Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::NOT_PROCESSING);
  117. \Yii::$app->$db->createCommand()->update('AR_PERIOD', ['CALC_ID' => ''], 'PERIOD_NUM=:PERIOD_NUM', ['PERIOD_NUM' => $period['PERIOD_NUM']])->execute();
  118. CalcOperation::closeOperation($businessPeriod['CALC_ID']);
  119. // CalcRecord::record($period['PERIOD_NUM'], '【奖金计算】第' . $period['PERIOD_NUM'] . '期的奖金数据获取失败,原因:' . $res['msg']);
  120. CalcRecord::record($period['PERIOD_NUM'], '【奖金计算】第' . $period['PERIOD_NUM'] . '期的奖金数据获取失败');
  121. //设置计算进行中标识
  122. Period::updatePeriodIsPreparing($period['PERIOD_NUM'], Period::NOT_PREPARING);
  123. //设置redis开关
  124. Yii::$app->cache->set('isCalcing', 0);
  125. return $res;
  126. }
  127. return true;
  128. }
  129. return true;
  130. }
  131. // 定时计算
  132. public static function autoPrePerf()
  133. {
  134. $nowTs = time();
  135. $nowDate = date("d", $nowTs);
  136. $nowHour = date("H", $nowTs);
  137. $nowMinuter = date('i', $nowTs);
  138. // 如果不是整点,则跳过计算
  139. if ($nowMinuter != 0) {
  140. echo('自动计算未开始. 【非整点时间】' . date('Y-m-d H:i:s') . PHP_EOL);
  141. return;
  142. }
  143. $currentPeriod = Period::find()->where('START_TIME< :NOW_TIME',['NOW_TIME'=>$nowTs])->where('END_TIME>= :NOW_TIME',['NOW_TIME'=>$nowTs-6])->asArray()->one();
  144. $periodNum = $currentPeriod['PERIOD_NUM'];
  145. $prevPeriodNum = $periodNum - 1;
  146. if ($currentPeriod['IS_SENT'] != 0){
  147. echo(date('Y-m-d H:i:s') . ' 期数:' . $periodNum . ',当前期数已挂网,异常,中止计算' . PHP_EOL);
  148. return;
  149. }
  150. // 自动计算:次月1号凌晨计算一次上一期
  151. if ($nowDate == 1 && $nowHour == 0) {
  152. echo('自动计算开始(1). ' . date('Y-m-d H:i:s') . ' 期数:' . $prevPeriodNum . PHP_EOL);
  153. BonusCalc::instance()->calculateBonus($prevPeriodNum);
  154. return;
  155. }
  156. // 自动计算:每2小时自动计算一次
  157. if ($nowHour % 2 == 0) {
  158. echo('自动计算开始(2). ' . date('Y-m-d H:i:s') . ' 期数:' . $periodNum . PHP_EOL);
  159. BonusCalc::instance()->calculateBonus($periodNum);
  160. }
  161. }
  162. private static function processAutoExec($periodNum)
  163. {
  164. //设置计算进行中标识
  165. Period::updatePeriodIsPreparing($periodNum, Period::IS_PREPARING);
  166. //设置自动计算标识
  167. Period::updatePeriodIsAutoExec($periodNum, Period::AUTO_EXEC_CALC);
  168. //记录开始计算的时间
  169. $calc_id = uniqid('calc_');
  170. CalcOperation::record($calc_id, $periodNum);
  171. $formModel = new PeriodForm();
  172. $formModel->scenario = 'autoPerf';
  173. if($formModel->load(['periodNum'=>$periodNum], '') && $formModel->autoPrePerf()) {
  174. echo('【定时计算】第' . $periodNum . '期,定时计算开始' . PHP_EOL);
  175. } else {
  176. echo('【定时计算】第' . $periodNum . '期,定时计算未开始' . PHP_EOL);
  177. }
  178. }
  179. /**
  180. * 同步计算系统的计算进度日志到业务系统计算步骤表
  181. * @param $periodNum
  182. * @param $db
  183. * @param $startTime
  184. * @return bool|int
  185. * @throws \yii\base\InvalidConfigException
  186. * @throws \yii\db\Exception
  187. */
  188. public static function syncCalcRecord($periodNum, $db, $startTime = 0)
  189. {
  190. $type = 'bonus';
  191. $calcTaskDesc = \Yii::$app->$db->createCommand("SELECT
  192. ID,PERIOD_NUM,TASK_DESCRIBE,START_AT FROM AR_SERVE_PROCESS
  193. where TASK_TYPE='$type' AND PERIOD_NUM=$periodNum AND START_AT>=$startTime
  194. order by START_AT desc")->queryAll();
  195. $calcTaskDescKeys = array_column($calcTaskDesc, null, 'ID');
  196. $currentRecordList = CalcRecord::find()->where(['PERIOD_NUM' => $periodNum])->select('ID')->asArray()->column();
  197. $currentRecordListKey = array_flip($currentRecordList);
  198. $tableName = (new CalcRecord())->getTableSchema()->name;
  199. $result = array_diff_key($calcTaskDescKeys, $currentRecordListKey);
  200. if (!empty($result)) {
  201. return \Yii::$app->db->createCommand()->batchInsert($tableName, ['ID', 'PERIOD_NUM', 'TEXT', 'CREATED_AT'], $result)->execute();
  202. }
  203. return true;
  204. }
  205. public static function syncLogRecord($periodNum, $db, $startTime = 0)
  206. {
  207. $calcTaskDesc = \Yii::$app->$db->createCommand("SELECT
  208. ID,PERIOD_NUM,LOG_DESCRIBE,CREATE_AT FROM AR_SERVE_LOG
  209. where PERIOD_NUM=$periodNum AND CREATE_AT>=$startTime
  210. order by RECORD_SORT")->queryAll();
  211. $calcTaskDescKeys = array_column($calcTaskDesc, null, 'ID');
  212. $currentRecordList = CalcRecord::find()->where(['PERIOD_NUM' => $periodNum])->select('ID')->asArray()->column();
  213. $currentRecordListKey = array_flip($currentRecordList);
  214. $tableName = (new CalcRecord())->getTableSchema()->name;
  215. $result = array_diff_key($calcTaskDescKeys, $currentRecordListKey);
  216. if (!empty($result)) {
  217. return \Yii::$app->db->createCommand()->batchInsert($tableName, ['ID', 'PERIOD_NUM', 'TEXT', 'CREATED_AT'], $result)->execute();
  218. }
  219. return true;
  220. }
  221. }