| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- namespace common\helpers\bonus\Calc;
- use common\models\CalcRecord;
- use common\models\Period;
- class CalcConsole extends BaseBusiness
- {
- const CALC_DB_NAME = 'dbCalcServer';
- public static function listenCalcPeriod()
- {
- $db = self::CALC_DB_NAME;
- $allPeriod = \Yii::$app->$db->createCommand("SELECT * FROM AR_PERIOD order by PERIOD_NUM desc")->queryAll();
- $period = [];
- foreach ($allPeriod as $v) {
- if ($v['IS_PREPARE'] > 0) {
- $period = $v;
- break;
- }
- }
- if (empty($period)) {
- return true;
- }
- $businessPeriod = Period::find()->where(['PERIOD_NUM' => $period['PERIOD_NUM']])
- ->asArray()->one();
- if (empty($businessPeriod)) {
- return true;
- }
- if ($businessPeriod['SEND_STARTED_AT'] > 0 || $businessPeriod['IS_SENT'] > 0) {
- //开始挂网 或者 已挂网 则直接返回
- return true;
- }
- //用户选择是否自动执行
- $autoExec = $businessPeriod['AUTO_EXEC'] > Period::MANUAL_EXEC_CALC;
- $businessIsCalculated = $businessPeriod['IS_CALCULATED'] == Period::CALCULATE_FINISH;
- $startExecTime = $businessPeriod['START_EXEC_TIME'] ?? 0;
- $businessIsProcessing = $businessPeriod['IS_PROCESSING'] ?? 0;
- echo $businessPeriod['IS_CALCING'];
- //同步计算进度日志到业务系统
- if (4 == $period['IS_PREPARE'] && 1 == $period['IS_CALCULATED'] && 1 == $businessPeriod['IS_CALCING']) {
- self::syncCalcRecord($period['PERIOD_NUM'], $db, $startExecTime);
- \Yii::$app->db->createCommand()->update('AR_PERIOD', ['IS_CALCING' => 0], 'PERIOD_NUM=' . $period['PERIOD_NUM'])->execute();
- }
- if (2 == $period['IS_PREPARE'] && 1 == $period['IS_PERFED'] && $autoExec && !$businessIsProcessing) {
- //拉取期业绩
- CalcRecord::record($period['PERIOD_NUM'], '第' . $period['PERIOD_NUM'] . '期的业绩数据已生成');
- CalcRecord::record($period['PERIOD_NUM'], '开始获取第' . $period['PERIOD_NUM'] . '期的期业绩数据');
- Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::IS_PROCESSING);
- $res = (new PullPerfDataFromCalc($period['PERIOD_NUM']))->start();
- if (200 == $res['code']) {
- Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::NOT_PROCESSING);
- CalcRecord::record($period['PERIOD_NUM'], '第' . $period['PERIOD_NUM'] . '期的期业绩数据已获取');
- } else {
- //结束计算状态
- Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::NOT_PROCESSING);
- CalcRecord::record($period['PERIOD_NUM'], '第' . $period['PERIOD_NUM'] . '期的期业绩数据获取失败,原因:' . $res['msg']);
- return $res;
- }
- //自动执行且IS_PREPARE为3 直接开始结算流程
- CalcRecord::record($period['PERIOD_NUM'], '开始计算第' . $period['PERIOD_NUM'] . '期的奖金');
- //同步周期表的值到业务系统
- self::pullPeriodForUpdate($period['PERIOD_NUM']);
- return \Yii::$app->$db->createCommand()->update('AR_PERIOD', ['IS_PREPARE' => 3], 'PERIOD_NUM=:PERIOD_NUM', ['PERIOD_NUM' => $period['PERIOD_NUM']])->execute();
- }
- if (4 == $period['IS_PREPARE'] && 1 == $period['IS_CALCULATED'] && $autoExec && !$businessIsProcessing && !$businessIsCalculated) {
- CalcRecord::record($period['PERIOD_NUM'], '第' . $period['PERIOD_NUM'] . '期的奖金已计算完成');
- CalcRecord::record($period['PERIOD_NUM'], '开始拉取第' . $period['PERIOD_NUM'] . '期的奖金数据');
- Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::IS_PROCESSING);
- //自动执行且IS_CALCULATED为1 直接开始拉取结算数据
- $res = (new PullCalcBonusData($period['PERIOD_NUM']))->start();
- if (200 == $res['code']) {
- CalcRecord::record($period['PERIOD_NUM'], '第' . $period['PERIOD_NUM'] . '期的奖金数据已全部拉取');
- //待拉取完成后再将计算完成标识改为已完成
- \Yii::$app->db->createCommand()->update('AR_PERIOD', ['IS_CALCULATED' => 1], 'PERIOD_NUM=' . $period['PERIOD_NUM'])->execute();
- //自动执行完成 更新对应字段
- //计算结束
- Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::NOT_PROCESSING);
- //流程结束
- //同步周期表的值到业务系统
- self::pullPeriodForUpdate($period['PERIOD_NUM']);
- //拉取计算系统的进度日志
- self::syncCalcRecord($period['PERIOD_NUM'], $db, $startExecTime);
- } else {
- //结束计算状态
- Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::NOT_PROCESSING);
- CalcRecord::record($period['PERIOD_NUM'], '第' . $period['PERIOD_NUM'] . '期的奖金数据获取失败,原因:' . $res['msg']);
- return $res;
- }
- return true;
- }
- return true;
- }
- public static function listenAutoPerfPeriod()
- {
- $db = self::CALC_DB_NAME;
- $allPeriod = \Yii::$app->$db->createCommand("SELECT * FROM AR_PERIOD order by PERIOD_NUM desc")->queryAll();
- $period = [];
- foreach ($allPeriod as $v) {
- if ($v['IS_PREPARE'] > 0) {
- $period = $v;
- break;
- }
- }
- if (empty($period)) {
- return true;
- }
- $businessPeriod = Period::find()->where(['PERIOD_NUM' => $period['PERIOD_NUM']])
- ->asArray()->one();
- if ($businessPeriod['SEND_STARTED_AT'] > 0 || $businessPeriod['IS_SENT'] > 0) {
- //开始挂网 或者 已挂网 则直接返回
- return true;
- }
- if (2 == $period['IS_PREPARE'] && 1 == $period['IS_PERFED']) {
- //拉取期业绩
- CalcRecord::record($period['PERIOD_NUM'], '第' . $period['PERIOD_NUM'] . '期的预计算业绩数据已生成');
- CalcRecord::record($period['PERIOD_NUM'], '开始获取第' . $period['PERIOD_NUM'] . '期的预计算期业绩数据');
- Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::IS_PROCESSING);
- $res = (new PullPerfDataFromCalc($period['PERIOD_NUM']))->start();
- if (200 == $res['code']) {
- Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::NOT_PROCESSING);
- CalcRecord::record($period['PERIOD_NUM'], '第' . $period['PERIOD_NUM'] . '期的预计算期业绩数据已获取');
- return $res;
- } else {
- //结束计算状态
- Period::updatePeriodIsProcessing($period['PERIOD_NUM'], Period::NOT_PROCESSING);
- CalcRecord::record($period['PERIOD_NUM'], '第' . $period['PERIOD_NUM'] . '期的预计算期业绩数据获取失败,原因:' . $res['msg']);
- return $res;
- }
- }
- return true;
- }
- /**
- * 同步计算系统的计算进度日志到业务系统计算步骤表
- * @param $periodNum
- * @param $db
- * @param $startTime
- * @return bool|int
- * @throws \yii\base\InvalidConfigException
- * @throws \yii\db\Exception
- */
- public static function syncCalcRecord($periodNum, $db, $startTime = 0)
- {
- $type = 'bonus';
- $calcTaskDesc = \Yii::$app->$db->createCommand("SELECT
- ID,PERIOD_NUM,TASK_DESCRIBE,START_AT FROM AR_SERVE_PROCESS
- where TASK_TYPE='$type' AND PERIOD_NUM=$periodNum AND START_AT>=$startTime
- order by START_AT desc")->queryAll();
- $calcTaskDescKeys = array_column($calcTaskDesc, null, 'ID');
- $currentRecordList = CalcRecord::find()->where(['PERIOD_NUM' => $periodNum])->select('ID')->asArray()->column();
- $currentRecordListKey = array_flip($currentRecordList);
- $tableName = (new CalcRecord())->getTableSchema()->name;
- $result = array_diff_key($calcTaskDescKeys, $currentRecordListKey);
- if (!empty($result)) {
- return \Yii::$app->db->createCommand()->batchInsert($tableName, ['ID', 'PERIOD_NUM', 'TEXT', 'CREATED_AT'], $result)->execute();
- }
- return true;
- }
- }
|