Period.php 29 KB

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