CalcConsole.php 14 KB

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