Subject.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: qisse
  5. * Date: 2017/6/27
  6. * Time: 20:02
  7. */
  8. namespace app\core\model;
  9. use think\Config;
  10. use think\Model;
  11. use tool\Common;
  12. class Subject extends Model
  13. {
  14. protected $pk = 'subjectID';
  15. protected $resultSetType = 'collection';
  16. protected $updateTime = 'updateTime';
  17. protected $createTime = 'addTime';
  18. protected $autoWriteTimestamp = true;
  19. const STATUS_LOCK = 1;
  20. const STATUS_ONLINE_CHECK = 2;
  21. const STATUS_ONLINE = 3;
  22. const STATUS_FULL = 4;
  23. const STATUS_OVERTIME = 5;
  24. const STATUS_REPAY = 6;
  25. const STATUSS = [
  26. self::STATUS_LOCK=>'锁定',
  27. self::STATUS_ONLINE_CHECK=>'等待上架',
  28. self::STATUS_ONLINE=>'抢购中',
  29. self::STATUS_FULL=>'已卖光',
  30. self::STATUS_OVERTIME=>'已到期',
  31. self::STATUS_REPAY=>'已还款'
  32. ];
  33. const STATUS_LOAN_NULL = 1;
  34. const STATUS_LOAN_FANG_WAIT = 2;
  35. const STATUS_LOAN_FANG = 3;
  36. const STATUS_LOAN_FINISH = 4;
  37. const STATUS_LOANS = [
  38. self::STATUS_LOAN_NULL=>'未到放款时间',
  39. self::STATUS_LOAN_FANG_WAIT=>'待放款',
  40. self::STATUS_LOAN_FANG=>'收款中',
  41. self::STATUS_LOAN_FINISH=>'已收款'
  42. ];
  43. public function subjectType()
  44. {
  45. return $this->belongsTo('SubjectType', 'subjectTypeID');
  46. }
  47. public function interestType()
  48. {
  49. return $this->belongsTo('InterestType', 'interestTypeID');
  50. }
  51. public function interestTimeType()
  52. {
  53. return $this->belongsTo('InterestTimeType', 'interestTimeTypeID');
  54. }
  55. public function subjectStat()
  56. {
  57. return $this->hasOne('SubjectStat', 'subjectID');
  58. }
  59. public function cangList()
  60. {
  61. return $this->hasMany('Cang', 'subjectID');
  62. }
  63. public function getStatusTextAttr($value, $data) {
  64. return self::STATUSS[$data['status']];
  65. }
  66. public function getStatusLoanTextAttr($value, $data) {
  67. return self::STATUS_LOANS[$data['statusLoan']];
  68. }
  69. public function getUnitAttr($value, $data) {
  70. return '天';
  71. }
  72. public function getYearAttr($value) {
  73. return sprintf("%.1f",$value);
  74. }
  75. public function getYearSystemAttr($value) {
  76. return sprintf("%.1f",$value);
  77. }
  78. public function getYearExtAttr($value) {
  79. return sprintf("%.1f",$value);
  80. }
  81. public static function getStatusList() {
  82. return self::STATUSS;
  83. }
  84. public static function createAlias() {
  85. return Common::timetodate(THINK_START_TIME, 10).Common::token_create(6);
  86. }
  87. public function getReleaseTimeAttr($value) {
  88. return Common::timetodate($value, 6);
  89. }
  90. public function getBeginTimeAttr($value) {
  91. return Common::timetodate($value, 6);
  92. }
  93. public function getEndTimeAttr($value) {
  94. return Common::timetodate($value, 6);
  95. }
  96. public function getRepayInterestTimeAttr($value) {
  97. return Common::timetodate($value, 6);
  98. }
  99. public function getOverTimeAttr($value) {
  100. return Common::timetodate($value, 0);
  101. }
  102. public function getFullTimeAttr($value) {
  103. if(!$value) {
  104. return '';
  105. }
  106. return Common::timetodate($value);
  107. }
  108. public function getInterestBeginTimeAttr($value, $data) {
  109. return '购买日';
  110. }
  111. public function getSubjectTypeIconAttr($value, $data) {
  112. $icon = '';
  113. if($data['subjectTypeID'] == 1) {
  114. $icon = 'http://slb.dahengdian.com/jiaqiancaifu/2017/12/30/A3hShPxTCV.png';
  115. }
  116. else if($data['subjectTypeID'] == 2) {
  117. $icon = 'http://slb.dahengdian.com/jiaqiancaifu/2018/01/03/loLuc5moxc.png';
  118. //http://slb.dahengdian.com/jiaqiancaifu/2018/01/11/orrP4nOlQn.png
  119. }
  120. else if($data['subjectTypeID'] == 3) {
  121. $icon = 'http://slb.dahengdian.com/jiaqiancaifu/2018/01/03/QioKU4FSfn.png';
  122. }
  123. else {
  124. $icon = 'http://slb.dahengdian.com/jiaqiancaifu/2017/12/30/A3hShPxTCV.png';
  125. }
  126. return $icon;
  127. }
  128. public function setPriceAttr($value) {
  129. return $value * 100;
  130. }
  131. public function getPriceAttr($value) {
  132. return (int)($value / 100);
  133. }
  134. public function setBasePriceAttr($value) {
  135. return $value * 100;
  136. }
  137. public function getBasePriceAttr($value) {
  138. return Common::price2($value / 100);
  139. }
  140. public function setBaseMaxPriceAttr($value) {
  141. return $value * 100;
  142. }
  143. public function getBaseMaxPriceAttr($value) {
  144. return Common::price2($value / 100);
  145. }
  146. public function getRepayTimeAttr($value, $data) {
  147. $repaytimeDelaySpan = self::getRepaytimeDelaySpan();
  148. return Common::timetodate($repaytimeDelaySpan + $data['overTime'], 0);
  149. }
  150. public function getReachTimeAttr($value, $data) {
  151. $reachTimeDelaySpan = self::getReachtimeDelaySpan();
  152. return Common::timetodate($reachTimeDelaySpan * 86400 + $data['overTime'], 0);
  153. }
  154. public function getInvestDayAttr($value, $data) {
  155. $overtimeIsinterest = self::getOvertimeIsinterest();
  156. $investDay = ($data['overTime'] - Common::datetotime(Common::timetodate(THINK_START_TIME, 0))) / 86400;
  157. if($overtimeIsinterest == 1) {
  158. $investDay = $investDay - 1;
  159. }
  160. if($data['interestTimeTypeID'] == 2) {
  161. $investDay = $investDay + 1;
  162. }
  163. return $investDay;
  164. }
  165. public static function getRepaytimeDelaySpan() {
  166. return Config::get('interest.repaytime_delay_span');
  167. }
  168. public static function getDefaultInterestTimeType() {
  169. return Config::get('interest.default_interest_time_type');
  170. }
  171. public static function getOvertimeIsinterest() {
  172. return Config::get('interest.overtime_isinterest');
  173. }
  174. public static function getReachtimeDelaySpan() {
  175. return Config::get('interest.reachtime_delay_span');
  176. }
  177. public static function getDailyReleaseSpan() {
  178. return Config::get('interest.default_daily_release_span');
  179. }
  180. public static function setSubjectFull($subject) {
  181. //满标计息
  182. if($subject['interestTimeTypeID'] == 1) {
  183. $interestBeginTime = Common::datetotime(Common::timetodate(THINK_START_TIME, 0));
  184. if(self::getOvertimeIsinterest() == 1) {
  185. $interestEndTime = $subject->getData('overTime');
  186. }
  187. else {
  188. $interestEndTime = $subject->getData('overTime') - 86400;
  189. }
  190. $investDay = $subject['investDay'];//投资天数
  191. //批量更新数据cang
  192. Cang::where([
  193. 'subjectID'=>$subject['subjectID']
  194. ])->chunk(100, function($subjectList) use($subject, $interestBeginTime, $interestEndTime, $investDay) {
  195. foreach ($subjectList as $k=>$item) {
  196. $interest = $item['moneySubject'] * $subject['year'] / 100 / 365 * $investDay;//预付利息
  197. Cang::update([
  198. 'interestBeginTime'=>$interestBeginTime,
  199. 'interestEndTime'=>$interestEndTime,
  200. 'investDay'=>$investDay,
  201. 'interest'=>$interest,
  202. 'status'=>Cang::STATUS_INTEREST //计息状态开始
  203. ],[
  204. 'cangID'=>$item['cangID']
  205. ]);
  206. CangRepay::update([
  207. 'money'=>$interest
  208. //'status'=>Cang::STATUS_INTEREST //计息状态开始
  209. ],[
  210. 'cangID'=>$item['cangID'],
  211. 'repayTypeID'=>2
  212. ]);
  213. }
  214. });
  215. }
  216. //最后更新subject
  217. self::update([
  218. 'status'=> self::STATUS_FULL,
  219. 'statusLoan'=>self::STATUS_LOAN_FANG_WAIT,
  220. 'fullTime'=>THINK_START_TIME
  221. ],[
  222. 'subjectID'=> $subject['subjectID']
  223. ]);
  224. }
  225. }