Period.php 25 KB

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