Period.php 26 KB

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