Period.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. <?php
  2. namespace common\models;
  3. use common\helpers\bonus\Calc\CalcConsole;
  4. use common\helpers\Date;
  5. use common\helpers\LoggerTool;
  6. use common\helpers\Tool;
  7. use Yii;
  8. use yii\db\Exception;
  9. /**
  10. * This is the model class for table "{{%PERIOD}}".
  11. *
  12. * @property string $ID
  13. * @property int $PERIOD_NUM 期数
  14. * @property int $CALC_MONTH 所在结算月
  15. * @property int $CALC_YEAR 所在结算年
  16. * @property int $START_TIME 期数开始时间戳
  17. * @property int $END_TIME 期数结束时间戳
  18. * @property int $IS_MONTH 是否结算月节点
  19. * @property int $IS_YEAR 是否结算年节点
  20. * @property int $IS_CLOSED 是否已封期
  21. * @property int $IS_PERFED 是否已生成业绩单
  22. * @property int $IS_CALCULATED 是否已结算
  23. * @property int $IS_SENT 是否已发放
  24. * @property int $IS_PERFING 是否正在生成业绩单
  25. * @property int $IS_CALCING 是否正在计算状态
  26. * @property int $IS_SENDING 是否正在挂网状态
  27. * @property int $CALC_PERCENT 结算进度
  28. * @property int $SENT_PERCENT 发放进度
  29. * @property string $PERF_ADMIN_ID 生成业绩单管理员
  30. * @property string $CLOSE_ADMIN_ID 手动封期管理员ID
  31. * @property string $CALC_ADMIN_ID 结算管理员ID
  32. * @property string $SENT_ADMIN_ID 发放管理员ID
  33. * @property int $CLOSED_AT 发放管理员ID
  34. * @property int $PERF_STARTED_AT 生成业绩单开始时间
  35. * @property int $PERFED_AT 生成业绩单结束时间
  36. * @property int $CALCULATE_STARTED_AT 结算开始时间
  37. * @property int $CALCULATED_AT 结算完成时间
  38. * @property int $SEND_STARTED_AT 发放开始时间
  39. * @property int $SENT_AT 发放完成时间
  40. * @property int $CREATED_AT 创建时间
  41. */
  42. class Period extends \common\components\ActiveRecord
  43. {
  44. const CALCULATE_NONE = 0;
  45. const CALCULATE_FINISH = 1;
  46. const CALCULATE_FAIL = 2;
  47. const SEND_NONE = 0;
  48. const SEND_FINISH = 1;
  49. const SEND_FAIL = 2;
  50. const PERF_NONE = 0;
  51. const PERF_FINISH = 1;
  52. const PERF_FAIL = 2;
  53. const SYSTEM_START_PERIOD_NUM = 100;
  54. const AUTO_EXEC_CALC = 1;
  55. const MANUAL_EXEC_CALC = 0;
  56. const IS_PROCESSING = 1;
  57. const NOT_PROCESSING = 0;
  58. const IS_PREPARING = 1;
  59. const NOT_PREPARING = 0;
  60. public $nowPeriodArr = null;
  61. public $periodNum = null;
  62. public $periodArr = null;
  63. public function init()
  64. {
  65. parent::init();
  66. }
  67. /**
  68. * @inheritdoc
  69. */
  70. public static function tableName()
  71. {
  72. return '{{%PERIOD}}';
  73. }
  74. /**
  75. * @inheritdoc
  76. */
  77. public function rules()
  78. {
  79. return [
  80. [['PERIOD_NUM', 'CALC_MONTH', 'CALC_YEAR', 'START_TIME', 'END_TIME', 'CREATED_AT'], 'required'],
  81. [['PERIOD_NUM', 'CALC_MONTH', 'CALC_YEAR', 'START_TIME', 'END_TIME', 'IS_MONTH', 'IS_YEAR', 'IS_CLOSED', 'IS_PERFED', 'IS_CALCULATED', 'IS_SENT', 'IS_PERFING', 'IS_CALCING', 'IS_SENDING', 'CALC_PERCENT', 'SENT_PERCENT', 'CLOSED_AT', 'PERF_STARTED_AT', 'PERFED_AT', 'CALCULATE_STARTED_AT', 'CALCULATED_AT', 'SEND_STARTED_AT', 'SENT_AT', 'CREATED_AT'], 'integer'],
  82. [['ID', 'PERF_ADMIN_ID', 'CLOSE_ADMIN_ID', 'CALC_ADMIN_ID', 'SENT_ADMIN_ID'], 'string', 'max' => 32],
  83. [['PERIOD_NUM'], 'unique'],
  84. [['ID'], 'unique'],
  85. ];
  86. }
  87. /**
  88. * @inheritdoc
  89. */
  90. public function attributeLabels()
  91. {
  92. return [
  93. 'ID' => 'ID',
  94. 'PERIOD_NUM' => '期数',
  95. 'CALC_MONTH' => '所在结算月',
  96. 'CALC_YEAR' => '所在结算年',
  97. 'START_TIME' => '期数开始时间戳',
  98. 'END_TIME' => '期数结束时间戳',
  99. 'IS_MONTH' => '是否结算月节点',
  100. 'IS_YEAR' => '是否结算年节点',
  101. 'IS_CLOSED' => '是否已封期',
  102. 'IS_PERFED' => '是否已生成业绩单',
  103. 'IS_CALCULATED' => '是否已结算',
  104. 'IS_SENT' => '是否已发放',
  105. 'IS_PERFING' => '是否正在生成业绩单',
  106. 'IS_CALCING' => '是否正在计算状态',
  107. 'IS_SENDING' => '是否正在挂网状态',
  108. 'CALC_PERCENT' => '结算进度',
  109. 'SENT_PERCENT' => '发放进度',
  110. 'PERF_ADMIN_ID' => '生成业绩单管理员',
  111. 'CLOSE_ADMIN_ID' => '手动封期管理员ID',
  112. 'CALC_ADMIN_ID' => '结算管理员ID',
  113. 'SENT_ADMIN_ID' => '发放管理员ID',
  114. 'CLOSED_AT' => '发放管理员ID',
  115. 'PERF_STARTED_AT' => '生成业绩单开始时间',
  116. 'PERFED_AT' => '生成业绩单结束时间',
  117. 'CALCULATE_STARTED_AT' => '结算开始时间',
  118. 'CALCULATED_AT' => '结算完成时间',
  119. 'SEND_STARTED_AT' => '发放开始时间',
  120. 'SENT_AT' => '发放完成时间',
  121. 'CREATED_AT' => '创建时间',
  122. ];
  123. }
  124. /**
  125. * 期数赋值给属性
  126. * @param int $periodNum
  127. * @return array|null|\yii\db\ActiveRecord
  128. */
  129. public function setPeriodNum($periodNum = null){
  130. if($periodNum === 0 || $periodNum === null || $periodNum === ''){
  131. //if($this->nowPeriodArr === null){
  132. $this->nowPeriodArr = static::find()->where('IS_CLOSED=0')->orderBy('PERIOD_NUM ASC')->asArray()->one();
  133. //}
  134. $this->periodArr = $this->nowPeriodArr;
  135. $periodNum = $this->nowPeriodArr['PERIOD_NUM'];
  136. } else {
  137. if($periodNum !== $this->periodNum){
  138. $this->periodArr = static::findOneAsArray(['PERIOD_NUM'=>$periodNum]);
  139. }
  140. }
  141. $this->periodNum = $periodNum;
  142. return $this->periodArr;
  143. }
  144. // 通过期数,获取此期数据
  145. public static function getInfoByPeriodNum($periodNum) {
  146. return static::findOneAsArray(['PERIOD_NUM'=>$periodNum]);
  147. }
  148. /**
  149. * 是否存在所传期数
  150. * @param int $periodNum
  151. * @return bool
  152. */
  153. public static function isExistsPeriodNum(int $periodNum){
  154. return static::find()->where(['PERIOD_NUM'=>$periodNum])->exists();
  155. }
  156. /**
  157. * 获取当前未封期的最小期数
  158. * @return int
  159. */
  160. public function getNowPeriodNum(){
  161. $this->setPeriodNum();
  162. if($this->nowPeriodArr){
  163. return $this->nowPeriodArr['PERIOD_NUM'];
  164. } else {
  165. return self::SYSTEM_START_PERIOD_NUM;
  166. }
  167. }
  168. /**
  169. * 获取当前期的开始时间
  170. * @return int
  171. */
  172. public function getNowPeriodStart(){
  173. $this->setPeriodNum();
  174. if($this->nowPeriodArr){
  175. return $this->nowPeriodArr['START_TIME'];
  176. } else {
  177. return Date::nowTime();
  178. }
  179. }
  180. /**
  181. * 获取当前期的结束时间
  182. * @return int
  183. */
  184. public function getNowPeriodEnd(){
  185. $this->setPeriodNum();
  186. if($this->nowPeriodArr){
  187. return $this->nowPeriodArr['END_TIME'];
  188. } else {
  189. return Date::nowTime();
  190. }
  191. }
  192. /**
  193. * 获取当前所在的结算月
  194. * @return int
  195. */
  196. public function getNowMonth(){
  197. $this->setPeriodNum();
  198. if($this->nowPeriodArr){
  199. return $this->nowPeriodArr['CALC_MONTH'];
  200. } else {
  201. return 0;
  202. }
  203. }
  204. /**
  205. * 获取当前期数所在结算年
  206. * @return int
  207. */
  208. public function getNowYear(){
  209. $this->setPeriodNum();
  210. if($this->nowPeriodArr){
  211. return $this->nowPeriodArr['CALC_YEAR'];
  212. } else {
  213. return 0;
  214. }
  215. }
  216. /**
  217. * 当前期数所在年月
  218. * @return int|string
  219. */
  220. public function getNowYearMonth(){
  221. $this->setPeriodNum(122);
  222. if($this->nowPeriodArr){
  223. return $this->nowPeriodArr['CALC_YEAR'].Tool::numFix($this->nowPeriodArr['CALC_MONTH']);
  224. } else {
  225. return 0;
  226. }
  227. }
  228. /**
  229. * 获取期数所在的结算月
  230. * @param int $periodNum
  231. * @return mixed
  232. * @throws Exception
  233. */
  234. public function getMonth($periodNum = null){
  235. $period = $this->setPeriodNum($periodNum);
  236. if($period){
  237. return $period['CALC_MONTH'];
  238. } else {
  239. throw new Exception('期数不存在');
  240. }
  241. }
  242. /**
  243. * 获取期数所在的结算年
  244. * @param $periodNum
  245. * @return mixed
  246. * @throws Exception
  247. */
  248. public function getYear($periodNum = null){
  249. $period = $this->setPeriodNum($periodNum);
  250. if($period){
  251. return $period['CALC_YEAR'];
  252. } else {
  253. throw new Exception('期数不存在');
  254. }
  255. }
  256. /**
  257. * 期数所在年月
  258. * @param $periodNum
  259. * @return string
  260. * @throws Exception
  261. */
  262. public function getYearMonth($periodNum = null){
  263. $period = $this->setPeriodNum($periodNum);
  264. if($period){
  265. return $period['CALC_YEAR'].Tool::numFix($period['CALC_MONTH'], 2);
  266. } else {
  267. throw new Exception('期数不存在'.$periodNum);
  268. }
  269. }
  270. /**
  271. * 所传年、月所有期数的数量
  272. * @param $year
  273. * @param $month
  274. * @return int
  275. */
  276. public function getYearMonthAllPeriodNumCount($year, $month){
  277. return Period::find()->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH', [':CALC_YEAR'=>$year, ':CALC_MONTH'=>$month])->count(1);
  278. }
  279. /**
  280. * 是否是结算月节点
  281. * @param $periodNum
  282. * @return mixed
  283. * @throws Exception
  284. */
  285. public function isCalcMonth($periodNum = null){
  286. $period = $this->setPeriodNum($periodNum);
  287. if($period){
  288. return $period['IS_MONTH'];
  289. } else {
  290. throw new Exception('期数不存在');
  291. }
  292. }
  293. /**
  294. * 是否是结算年节点
  295. * @param $periodNum
  296. * @return mixed
  297. * @throws Exception
  298. */
  299. public function isCalcYear($periodNum = null){
  300. $period = $this->setPeriodNum($periodNum);
  301. if($period){
  302. return $period['IS_YEAR'];
  303. } else {
  304. throw new Exception('期数不存在');
  305. }
  306. }
  307. /**
  308. * 获取挂网的最大期数
  309. * @return int
  310. */
  311. public function getMaxIsSentPeriodNum() {
  312. $maxIsSentData = static::find()->where('IS_SENT=1')->orderBy('PERIOD_NUM DESC')->asArray()->one();
  313. $maxIsSentPeriodNum = self::SYSTEM_START_PERIOD_NUM;
  314. if( $maxIsSentData && $maxIsSentData["PERIOD_NUM"] ) {
  315. $maxIsSentPeriodNum = $maxIsSentData["PERIOD_NUM"];
  316. }
  317. return $maxIsSentPeriodNum;
  318. }
  319. /**
  320. * 返回所传结算月包含的所有期数
  321. * @param $year
  322. * @param $month
  323. * @return array|\yii\db\ActiveRecord[]
  324. */
  325. public static function getPeriodNumsFromMonth($year, $month){
  326. return static::find()->select('PERIOD_NUM')->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH', [':CALC_YEAR'=>$year, ':CALC_MONTH'=>$month])->orderBy('PERIOD_NUM ASC')->asArray()->all();
  327. }
  328. /**
  329. * 获取所传结算月的最小期数和最大期数
  330. * @param $year
  331. * @param $month
  332. * @return array
  333. */
  334. public static function getPeriodNumRangeFromMonth($year, $month){
  335. $allPeriod = self::getPeriodNumsFromMonth($year, $month);
  336. $tempPeriod = [];
  337. foreach($allPeriod as $period){
  338. $tempPeriod[] = $period['PERIOD_NUM'];
  339. }
  340. if(!$tempPeriod){
  341. return null;
  342. }
  343. $minPeriod = min($tempPeriod);
  344. $maxPeriod = max($tempPeriod);
  345. return ['min'=>$minPeriod, 'max'=>$maxPeriod];
  346. }
  347. /**
  348. * 返回结算月的节点期数
  349. * @param $year
  350. * @param $month
  351. * @return mixed|null
  352. */
  353. public static function getPeriodNumPointFromMonth($year, $month) {
  354. $data = static::find()->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH AND IS_MONTH=1', [':CALC_YEAR'=>$year, ':CALC_MONTH' => $month])->asArray()->one();
  355. if($data) return $data['PERIOD_NUM'];
  356. else return null;
  357. }
  358. /**
  359. * 返回结算月的节点期
  360. * @param $year
  361. * @param $month
  362. * @return mixed|null
  363. */
  364. public static function getPeriodPointFromMonth($year, $month) {
  365. $data = static::find()->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH AND IS_MONTH=1', [':CALC_YEAR'=>$year, ':CALC_MONTH' => $month])->asArray()->one();
  366. if($data) return $data;
  367. else return null;
  368. }
  369. /**
  370. * 是否已经封期
  371. * @param $periodNum
  372. * @return bool
  373. * @throws Exception
  374. */
  375. public function isClosed($periodNum = null){
  376. $period = $this->setPeriodNum($periodNum);
  377. if($period){
  378. return boolval($period['IS_CLOSED']);
  379. } else {
  380. throw new Exception('期数不存在');
  381. }
  382. }
  383. /**
  384. * 是否已经生成业绩单
  385. * @param $periodNum
  386. * @return bool
  387. * @throws Exception
  388. */
  389. public function isPerfed($periodNum = null){
  390. $period = $this->setPeriodNum($periodNum);
  391. if($period){
  392. return boolval($period['IS_PERFED']);
  393. } else {
  394. throw new Exception('期数不存在');
  395. }
  396. }
  397. /**
  398. * 是否已结算
  399. * @param null $periodNum
  400. * @return bool
  401. * @throws Exception
  402. */
  403. public function isCalculated($periodNum = null){
  404. $period = $this->setPeriodNum($periodNum);
  405. if($period){
  406. return boolval($period['IS_CALCULATED'] == self::CALCULATE_FINISH);
  407. } else {
  408. throw new Exception('期数不存在');
  409. }
  410. }
  411. /**
  412. * 是否可被封期
  413. * @param $periodNum
  414. * @return bool
  415. */
  416. public function isCanClose($periodNum = null){
  417. $period = $this->setPeriodNum($periodNum);
  418. if($period['IS_CLOSED']){
  419. return false;
  420. }
  421. $previousPeriod = static::findOne(['PERIOD_NUM'=>$this->periodNum-1]);
  422. if($previousPeriod){
  423. if(!$previousPeriod['IS_CLOSED']){
  424. return false;
  425. }
  426. // 只要上期封期了,下期就可以封期
  427. // if(!$previousPeriod['IS_CALCULATED']){
  428. // return false;
  429. // }
  430. // if(!$previousPeriod['IS_SENT']){
  431. // return false;
  432. // }
  433. }
  434. return true;
  435. }
  436. /**
  437. * 是否可以生成业绩单
  438. * @param $periodNum
  439. * @return bool
  440. */
  441. public function isCanPerf($periodNum = null){
  442. $period = $this->setPeriodNum($periodNum);
  443. // 查看该期是否已经封期
  444. if(!$period['IS_CLOSED']){
  445. return false;
  446. }
  447. // 已经挂网,不能生成业绩单
  448. if($period['IS_SENT'] == self::SEND_FINISH){
  449. return false;
  450. }
  451. //上1期奖金未发放,限制不能生成业绩单
  452. if(!$this->isLastSent($this->periodNum)){
  453. return false;
  454. }
  455. return true;
  456. }
  457. /**
  458. * 是否可以结算
  459. * @param $periodNum
  460. * @return bool
  461. */
  462. public function isCanCalc($periodNum = null){
  463. $period = $this->setPeriodNum($periodNum);
  464. // 查看该期是否已经封期
  465. if(!$period['IS_CLOSED']){
  466. return false;
  467. }
  468. // 还没有生成业绩单,不能封期
  469. if(!$period['IS_PERFED'] || $period['IS_PERFED'] == self::PERF_FAIL){
  470. return false;
  471. }
  472. if($period['IS_SENT'] == self::SEND_FINISH){
  473. return false;
  474. }
  475. // $previousPeriod = static::findOneAsArray(['PERIOD_NUM'=>$this->periodNum-1]);
  476. // if($previousPeriod){
  477. // // 查看上一期是否已经挂网,上一期挂网了,本期才能结算
  478. // if(!$previousPeriod['IS_SENT']){
  479. // return false;
  480. // }
  481. // }
  482. //上1期奖金未发放,限制不能结算
  483. if(!$this->isLastSent($this->periodNum)){
  484. return false;
  485. }
  486. return true;
  487. }
  488. /**
  489. * 是否可以挂网
  490. * @param $periodNum
  491. * @return bool
  492. */
  493. public function isCanSend($periodNum = null){
  494. $period = $this->setPeriodNum($periodNum);
  495. if(!$period['IS_CLOSED']){
  496. return false;
  497. }
  498. if(!$period['IS_CALCULATED'] || $period['IS_CALCULATED'] == self::CALCULATE_FAIL){
  499. return false;
  500. }
  501. if($period['IS_SENT'] == self::SEND_FINISH){
  502. return false;
  503. }
  504. //上1期奖金未发放,限制不能挂网
  505. if(!$this->isLastSent($this->periodNum)){
  506. return false;
  507. }
  508. return true;
  509. }
  510. /**
  511. * 是否已经挂网
  512. * @param $periodNum
  513. * @return bool
  514. */
  515. public function isSent($periodNum = null){
  516. $period = $this->setPeriodNum($periodNum);
  517. if($period['IS_SENT'] == self::SEND_FINISH) return true;
  518. return false;
  519. }
  520. /**
  521. * 上期是否挂网
  522. * @param null $periodNum
  523. * @return bool
  524. */
  525. public function isLastSent($periodNum){
  526. $period = $this->setPeriodNum($periodNum-1);
  527. if ($period) {
  528. if($period['IS_SENT'] == self::SEND_FINISH) {
  529. return true;
  530. } else {
  531. return false;
  532. }
  533. }
  534. return false;
  535. }
  536. /**
  537. * 获取上月的年月
  538. * @param $periodNum
  539. * @return array
  540. */
  541. public function getLastMonth($periodNum = null){
  542. $period = $this->setPeriodNum($periodNum);
  543. if($period){
  544. $year = $period['CALC_YEAR'];
  545. $month = $period['CALC_MONTH'];
  546. $lastYear = Date::lastMonth($year.'-'.$month, 'Y');
  547. $lastMonth = Date::lastMonth($year.'-'.$month, 'm');
  548. return [
  549. 'year' => $lastYear,
  550. 'month' => intval($lastMonth),
  551. 'yearMonth' => $lastYear.Tool::numFix($lastMonth, 2),
  552. ];
  553. } else {
  554. return [
  555. 'year' => 0,
  556. 'month' => 0,
  557. 'yearMonth' => 0,
  558. ];
  559. }
  560. }
  561. /**
  562. * 上几个月的年月
  563. * @param int $num
  564. * @param null $periodNum
  565. * @return array
  566. */
  567. public function getLastNumMonth($num=1,$periodNum = null){
  568. $period = $this->setPeriodNum($periodNum);
  569. if($period){
  570. $year = $period['CALC_YEAR'];
  571. $month = $period['CALC_MONTH'];
  572. $lastYear = Date::lastNumMonth($num,$year.'-'.$month, 'Y');
  573. $lastMonth = Date::lastNumMonth($num,$year.'-'.$month, 'm');
  574. return [
  575. 'year' => $lastYear,
  576. 'month' => intval($lastMonth),
  577. 'yearMonth' => $lastYear.Tool::numFix($lastMonth, 2),
  578. ];
  579. } else {
  580. return [
  581. 'year' => 0,
  582. 'month' => 0,
  583. 'yearMonth' => 0,
  584. ];
  585. }
  586. }
  587. /**
  588. * 下月的年月
  589. * @param null $periodNum
  590. * @return array
  591. */
  592. public function getNextMonth($periodNum = null){
  593. $period = $this->setPeriodNum($periodNum);
  594. if($period){
  595. $year = $period['CALC_YEAR'];
  596. $month = $period['CALC_MONTH'];
  597. $nextYear = Date::nextMonth($year.'-'.$month, 'Y');
  598. $nextMonth = Date::nextMonth($year.'-'.$month, 'm');
  599. return [
  600. 'year' => $nextYear,
  601. 'month' => intval($nextMonth),
  602. 'yearMonth' => $nextYear.Tool::numFix($nextMonth, 2),
  603. ];
  604. } else {
  605. return [
  606. 'year' => 0,
  607. 'month' => 0,
  608. 'yearMonth' => 0,
  609. ];
  610. }
  611. }
  612. /**
  613. * 通过时间戳获取期
  614. * @param $time
  615. * @return array|null|\yii\db\ActiveRecord
  616. */
  617. public static function getPeriodFromTime($time){
  618. return static::find()->where('START_TIME<=:TIME AND END_TIME>:TIME', [':TIME'=>$time])->asArray()->one();
  619. }
  620. /**
  621. * 通过时间戳范围获取期间内的期
  622. * @param $minTime
  623. * @param $maxTime
  624. * @return array|\yii\db\ActiveRecord[]
  625. */
  626. public static function getPeriodFromTimeRange($minTime, $maxTime){
  627. return static::find()->where('END_TIME>:MIN_TIME AND START_TIME<:MAX_TIME', [':MIN_TIME'=>$minTime, ':MAX_TIME'=>$maxTime])->orderBy('PERIOD_NUM ASC')->asArray()->all();
  628. }
  629. /**
  630. * 获取近几期的期数
  631. * @param $num
  632. * @return array
  633. */
  634. public static function getNearlyPeriodNum($num){
  635. // 获取当前期
  636. $period = self::instance();
  637. $periodNum = $period->getNowPeriodNum();
  638. // 获取前$num期的期数
  639. $periodNums = [
  640. $periodNum
  641. ];
  642. for($i=0;$i<$num;$i++){
  643. if($periodNum - $i > 0){
  644. // 查看该期是否存在
  645. if(self::isExistsPeriodNum($periodNum - $i)){
  646. $periodNums[] = $periodNum - $i;
  647. }
  648. }
  649. }
  650. return array_reverse($periodNums);
  651. }
  652. /**
  653. * 获取近几期的挂网期数
  654. * @param $num
  655. * @return array
  656. */
  657. public static function getNearlySendPeriodNum($num){
  658. // 获取当前期
  659. $period = self::instance();
  660. $periodNum = $period->sentMaxPeriodNum();
  661. // 获取前$num期的期数
  662. $periodNums = [
  663. $periodNum
  664. ];
  665. for($i=0;$i<$num;$i++){
  666. if($periodNum - $i > 0){
  667. // 查看该期是否存在
  668. if(self::isExistsPeriodNum($periodNum - $i)){
  669. $periodNums[] = $periodNum - $i;
  670. }
  671. }
  672. }
  673. return array_reverse($periodNums);
  674. }
  675. /**
  676. * 获取近几期的年月
  677. * @param $num
  678. * @return array
  679. * @throws Exception
  680. */
  681. public static function getNearlyPeriodYearMonth($num){
  682. // 获取当前期
  683. $period = self::instance();
  684. $periodNum = $period->getNowPeriodNum();
  685. $nowYearMonth = $period->getNowYearMonth();
  686. // 获取前$num期的期数
  687. $periodYearMonths = [];
  688. for($i=1;$i<=$num;$i++){
  689. if($periodNum - $i > 0){
  690. // 查看该期是否存在
  691. if(self::isExistsPeriodNum($periodNum - $i)){
  692. $yearMonth = $period->getYearMonth($periodNum - $i);
  693. $periodYearMonths[] = [
  694. 'yearMonth' => $yearMonth,
  695. 'periodNum' => $periodNum - $i,
  696. ];
  697. }
  698. }
  699. }
  700. return array_reverse($periodYearMonths);
  701. }
  702. /**
  703. * 获取本月所有已经挂网的期数
  704. * @param $yearMonth
  705. * @return array
  706. */
  707. public static function monthSentAllPeriodNum($yearMonth){
  708. $year = intval(substr($yearMonth, 0, 4));
  709. $month = intval(substr($yearMonth, 4, 2));
  710. $allPeriod = Period::find()->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH AND IS_SENT=1', [':CALC_YEAR'=>$year, ':CALC_MONTH'=>$month])->select('PERIOD_NUM')->asArray()->all();
  711. $result = [];
  712. if($allPeriod){
  713. foreach($allPeriod as $period){
  714. $result[] = $period['PERIOD_NUM'];
  715. }
  716. }
  717. return $result;
  718. }
  719. /**
  720. * 所传月份所有已结算的期数
  721. * @param $yearMonth
  722. * @return array
  723. */
  724. public static function monthCalcAllPeriodNum($yearMonth){
  725. $year = intval(substr($yearMonth, 0, 4));
  726. $month = intval(substr($yearMonth, 4, 2));
  727. $allPeriod = Period::find()->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH AND IS_CALCING=1', [':CALC_YEAR'=>$year, ':CALC_MONTH'=>$month])->select('PERIOD_NUM')->asArray()->all();
  728. $result = [];
  729. if($allPeriod){
  730. foreach($allPeriod as $period){
  731. $result[] = $period['PERIOD_NUM'];
  732. }
  733. }
  734. return $result;
  735. }
  736. /**
  737. * 获取已结算的最大期数
  738. * @return mixed
  739. */
  740. public static function calculatedMaxPeriodNum(){
  741. return static::find()->where('IS_CALCULATED=1')->max('PERIOD_NUM');
  742. }
  743. /**
  744. * 已挂网的最大期数
  745. * @return mixed
  746. */
  747. public static function sentMaxPeriodNum(){
  748. return static::find()->where('IS_SENT=1')->max('PERIOD_NUM');
  749. }
  750. /**
  751. * 期数结束时间
  752. * @param $periodNum
  753. * @return int|mixed
  754. */
  755. public static function getEndTime($periodNum) {
  756. $endTime = static::find()->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $periodNum])->select('END_TIME')->asArray()->one();
  757. return $endTime['END_TIME'] ?? 0;
  758. }
  759. /**
  760. * 到年底的剩余月份
  761. * @param null $periodNum
  762. * @return int
  763. * @throws Exception
  764. */
  765. public static function getMonthLeft($periodNum = null) {
  766. $period = self::instance();
  767. if ($periodNum) {
  768. $nowMonth = $period->getMonth($periodNum);
  769. } else {
  770. $nowMonth = $period->getNowMonth();
  771. }
  772. return 12 - (int)$nowMonth + 1;
  773. }
  774. /**
  775. * 起始期数和结束期数几个月
  776. * @param $startPeriodNum
  777. * @param $endPeriodNum
  778. * @return int|string
  779. */
  780. public static function getMonthNum($startPeriodNum,$endPeriodNum){
  781. return self::find()->select('CALC_MONTH')->groupBy('CALC_MONTH')->where('PERIOD_NUM>=:START_PERIOD AND PERIOD_NUM<=:END_PERIOD',[':START_PERIOD'=>$startPeriodNum,':END_PERIOD'=>$endPeriodNum])->count();
  782. }
  783. /**
  784. * 从期数获取当前月的所有期
  785. *
  786. */
  787. public static function getCurrentMonthPeriodByPeriodNum($periodNum){
  788. $currentPeriod = self::getInfoByPeriodNum($periodNum);
  789. $currentYear = $currentPeriod['CALC_YEAR'];
  790. $currentMonth = $currentPeriod['CALC_MONTH'];
  791. $periodsArray = self::findAllAsArray(['CALC_YEAR'=>$currentYear,'CALC_MONTH'=>$currentMonth]);
  792. $periods = [];
  793. foreach ($periodsArray as $p){
  794. $periods[] = $p['PERIOD_NUM'];
  795. }
  796. return $periods;
  797. }
  798. public static function getCurrentQuarterPeriodByPeriodNum($periodNum){
  799. $currentPeriod = self::getInfoByPeriodNum($periodNum);
  800. $currentYear = $currentPeriod['CALC_YEAR'];
  801. $currentMonth = $currentPeriod['CALC_MONTH'];
  802. $periodsArray = self::find()->select("*")
  803. ->where(['CALC_YEAR'=>$currentYear])
  804. ->andWhere(['in', 'CALC_MONTH', [$currentMonth-2, $currentMonth-1, $currentMonth]])
  805. ->asArray()->all();
  806. $periods = [];
  807. foreach ($periodsArray as $p){
  808. $periods[] = $p['PERIOD_NUM'];
  809. }
  810. return $periods;
  811. }
  812. public static function updatePeriodIsAutoExec($periodNum, $isAutoExec = 0): int
  813. {
  814. return Period::updateAll(['AUTO_EXEC' => $isAutoExec], ['PERIOD_NUM' => $periodNum]);
  815. }
  816. public static function updatePeriodIsProcessing($periodNum, $isProcessing = 1): int
  817. {
  818. return Period::updateAll(['IS_PROCESSING' => $isProcessing], ['PERIOD_NUM' => $periodNum]);
  819. }
  820. public static function updatePeriodIsPreparing($periodNum, $isPreparing = 1): int
  821. {
  822. return Period::updateAll(['IS_PREPARING' => $isPreparing], ['PERIOD_NUM' => $periodNum]);
  823. }
  824. /**
  825. * 是否计算操作中
  826. * @param $periodNum
  827. * @return bool
  828. */
  829. public static function isProcessing($periodNum = null): bool
  830. {
  831. $db = CalcConsole::CALC_DB_NAME;
  832. $allPeriod = \Yii::$app->$db->createCommand("SELECT * FROM AR_PERIOD order by PERIOD_NUM desc")->queryAll();
  833. $calcPeriod = [];
  834. foreach ($allPeriod as $v) {
  835. if ($v['IS_PREPARE'] > 0) {
  836. $calcPeriod = $v;
  837. break;
  838. }
  839. }
  840. LoggerTool::debug('calc-l');
  841. $period = static::findOneAsArray(['PERIOD_NUM' => $periodNum]);
  842. // 若计算服务中CALC_ID不为空,则不允许计算
  843. LoggerTool::debug('calc-2');
  844. if ($calcPeriod) {
  845. if ($calcPeriod['CALC_ID']) return true;
  846. }
  847. LoggerTool::debug('calc-3');
  848. //挂网则拒绝操作,返回true则拒绝操作
  849. if ($period['IS_SENT'] == self::SEND_FINISH) return true;
  850. LoggerTool::debug('calc-4');
  851. if ($period['IS_PROCESSING'] == self::IS_PROCESSING) return true;
  852. LoggerTool::debug('calc-5');
  853. return false;
  854. }
  855. /**
  856. * 是否预计算操作中
  857. * @param $periodNum
  858. * @return bool
  859. */
  860. public static function isPreparing($periodNum = null): bool
  861. {
  862. $period = static::findOneAsArray(['PERIOD_NUM' => $periodNum]);
  863. //挂网则拒绝操作,返回true则拒绝操作
  864. if ($period['IS_SENT'] == self::SEND_FINISH) return true;
  865. if ($period['IS_PREPARING'] == self::IS_PREPARING) return true;
  866. $nowTs = time();
  867. if ($nowTs-6 > $period['END_TIME']) return true;
  868. return false;
  869. }
  870. public static function checkPerf($periodNum){
  871. $result = static::find()
  872. ->where(
  873. 'PERIOD_NUM=:PERIOD_NUM ', [':PERIOD_NUM' => $periodNum]
  874. )
  875. ->asArray()
  876. ->one();
  877. if (isset($result['IS_PERFED']) && $result['IS_PERFED'] == 1) {
  878. return true;
  879. }
  880. return false;
  881. }
  882. public static function checkClose($periodNum){
  883. $period = static::findOneAsArray(['PERIOD_NUM' => $periodNum]);
  884. if ($period['IS_CLOSED'] == 1) {
  885. return true;
  886. }
  887. return false;
  888. }
  889. }