Period.php 30 KB

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