Period.php 26 KB

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