Demands.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\common\model;
  3. use app\common\service\CommonService;
  4. use app\common\service\DemandBiddingService;
  5. use think\Model;
  6. class Demands extends Model
  7. {
  8. // 表名
  9. protected $name = 'demands';
  10. // 自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'integer';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. protected $updateTime = 'updatetime';
  15. protected $deleteTime = false;
  16. // 追加属性
  17. protected $append = [
  18. 'type_text',
  19. 'limit_time_text',
  20. 'createtime_ago',
  21. 'status_text',
  22. 'work_status_text',
  23. 'accept_status_text',
  24. 'settle_status_text',
  25. 'publish_status_text'
  26. ];
  27. protected $type = [
  28. 'images' => 'array',
  29. 'files' => 'array',
  30. ];
  31. protected static function init()
  32. {
  33. self::afterInsert(function ($row) {
  34. if (!empty($row['weigh'])) {
  35. $pk = $row->getPk();
  36. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  37. }
  38. });
  39. }
  40. public function getTypeList()
  41. {
  42. return ['0' => __('Type 0'), '1' => __('Type 1')];
  43. }
  44. public function getStatusList()
  45. {
  46. return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4'), '5' => __('Status 5'), '6' => __('Status 6'), '7' => __('Status 7')];
  47. }
  48. public function getWorkStatusList()
  49. {
  50. return ['0' => __('Work_status 0'), '1' => __('Work_status 1'), '2' => __('Work_status 2'), '3' => __('Work_status 3')];
  51. }
  52. public function getAcceptStatusList()
  53. {
  54. return ['0' => __('Accept_status 0'), '1' => __('Accept_status 1'), '2' => __('Accept_status 2'), '3' => __('Accept_status 3')];
  55. }
  56. public function getSettleStatusList()
  57. {
  58. return ['0' => __('Settle_status 0'), '1' => __('Settle_status 1'), '2' => __('Settle_status 2'), '3' => __('Settle_status 3')];
  59. }
  60. public function getPublishStatusList()
  61. {
  62. return ['0' => __('Publish_status 0'), '1' => __('Publish_status 1'), '2' => __('Publish_status 2'), '3' => __('Publish_status 3')];
  63. }
  64. public function getBiddingStatusList()
  65. {
  66. return ['0' => __('Bidding_status 0'), '1' => __('Bidding_status 1')];
  67. }
  68. public function getTypeTextAttr($value, $data)
  69. {
  70. $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
  71. $list = $this->getTypeList();
  72. return isset($list[$value]) ? $list[$value] : '';
  73. }
  74. public function getLimitTimeTextAttr($value, $data)
  75. {
  76. return $data['limit_time'];
  77. }
  78. public function getStatusTextAttr($value, $data)
  79. {
  80. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  81. $list = $this->getStatusList();
  82. return isset($list[$value]) ? $list[$value] : '';
  83. }
  84. public function getWorkStatusTextAttr($value, $data)
  85. {
  86. $value = $value ? $value : (isset($data['work_status']) ? $data['work_status'] : '');
  87. $list = $this->getWorkStatusList();
  88. return isset($list[$value]) ? $list[$value] : '';
  89. }
  90. public function getAcceptStatusTextAttr($value, $data)
  91. {
  92. $value = $value ? $value : (isset($data['accept_status']) ? $data['accept_status'] : '');
  93. $list = $this->getAcceptStatusList();
  94. return isset($list[$value]) ? $list[$value] : '';
  95. }
  96. public function getSettleStatusTextAttr($value, $data)
  97. {
  98. $value = $value ? $value : (isset($data['settle_status']) ? $data['settle_status'] : '');
  99. $list = $this->getSettleStatusList();
  100. return isset($list[$value]) ? $list[$value] : '';
  101. }
  102. public function getCreatetimeAgoAttr($value, $data)
  103. {
  104. return isset($data['createtime']) ? time2ago($data['createtime']) : '';
  105. }
  106. public function getPublishStatusTextAttr($value, $data)
  107. {
  108. $value = $value ? $value : (isset($data['publish_status']) ? $data['publish_status'] : '');
  109. $list = $this->getPublishStatusList();
  110. return isset($list[$value]) ? $list[$value] : '';
  111. }
  112. protected function setLimitTimeAttr($value)
  113. {
  114. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  115. }
  116. public function user()
  117. {
  118. return $this->hasOne('User', 'id', 'user_id')->field('id,nickname,avatar');
  119. }
  120. public function demandBiddings()
  121. {
  122. return $this->hasMany('app\common\model\demand\Biddings', 'demand_id', 'id')->field('id,demand_id,user_id,quoted_price,need_days,remark,bidding_status,process_status,createtime');
  123. }
  124. public function orders()
  125. {
  126. return $this->hasMany('Orders', 'parent_order_no', 'parent_order_no');
  127. }
  128. //
  129. public function demandContracts()
  130. {
  131. return $this->hasOne('app\common\model\demand\Contracts', 'demand_id', 'id');
  132. }
  133. public function winBidding()
  134. {
  135. return $this->hasOne('app\common\model\demand\Biddings', 'demand_id', 'id')->where('bidding_status', DemandBiddingService::STATUS_BIDDING_WIN);
  136. }
  137. public function demandGroupChat()
  138. {
  139. return $this->hasOne('app\common\model\demand\group\Chat', 'demand_id', 'id')->where('status', CommonService::STATUS_ON);
  140. }
  141. }