Period.php 25 KB

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