Period.php 28 KB

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