InspectionApplication.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace app\common\model\inspection;
  3. use think\Model;
  4. use traits\model\SoftDelete;
  5. /**
  6. * 验货员申请模型
  7. */
  8. class InspectionApplication extends Model
  9. {
  10. use SoftDelete;
  11. // 表名
  12. protected $table = 'inspection_application';
  13. // 开启自动写入时间戳字段
  14. protected $autoWriteTimestamp = 'int';
  15. // 定义时间戳字段名
  16. protected $createTime = 'createtime';
  17. protected $updateTime = 'updatetime';
  18. protected $deleteTime = 'deletetime';
  19. // 追加属性
  20. protected $append = [
  21. 'audit_status_text',
  22. 'apply_time_text',
  23. 'audit_time_text',
  24. 'province_name',
  25. 'city_name',
  26. 'district_name',
  27. ];
  28. // 审核状态常量
  29. const AUDIT_STATUS_PENDING = 1; // 审核中
  30. const AUDIT_STATUS_PASSED = 2; // 通过
  31. const AUDIT_STATUS_REJECTED = 3; // 驳回
  32. /**
  33. * 获取审核状态列表
  34. */
  35. public function getAuditStatusList()
  36. {
  37. return [
  38. self::AUDIT_STATUS_PENDING => '审核中',
  39. self::AUDIT_STATUS_PASSED => '审核通过',
  40. self::AUDIT_STATUS_REJECTED => '审核驳回'
  41. ];
  42. }
  43. /**
  44. * 获取审核状态文本
  45. */
  46. public function getAuditStatusTextAttr($value, $data)
  47. {
  48. $status = $value ?: ($data['audit_status'] ?? self::AUDIT_STATUS_PENDING);
  49. $list = $this->getAuditStatusList();
  50. return $list[$status] ?? '未知状态';
  51. }
  52. /**
  53. * 获取申请时间文本
  54. */
  55. public function getApplyTimeTextAttr($value, $data)
  56. {
  57. $time = $value ?: ($data['apply_time'] ?? 0);
  58. return $time > 0 ? date('Y-m-d H:i:s', $time) : '';
  59. }
  60. /**
  61. * 获取审核时间文本
  62. */
  63. public function getAuditTimeTextAttr($value, $data)
  64. {
  65. $time = $value ?: ($data['audit_time'] ?? 0);
  66. return $time > 0 ? date('Y-m-d H:i:s', $time) : '';
  67. }
  68. /**
  69. * 获取身份证正面图片完整URL
  70. */
  71. public function getIdCardFrontAttr($value)
  72. {
  73. return $value ? cdnurl($value, true) : '';
  74. }
  75. /**
  76. * 获取身份证反面图片完整URL
  77. */
  78. public function getIdCardBackAttr($value)
  79. {
  80. return $value ? cdnurl($value, true) : '';
  81. }
  82. /**
  83. * 设置身份证正面图片
  84. */
  85. public function setIdCardFrontAttr($value)
  86. {
  87. return $value ? str_replace(request()->domain(), '', $value) : '';
  88. }
  89. /**
  90. * 设置身份证反面图片
  91. */
  92. public function setIdCardBackAttr($value)
  93. {
  94. return $value ? str_replace(request()->domain(), '', $value) : '';
  95. }
  96. /**
  97. * 关联用户表
  98. */
  99. public function user()
  100. {
  101. return $this->belongsTo('app\\common\\model\\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
  102. }
  103. /**
  104. * 关联地区表(省)
  105. */
  106. public function province()
  107. {
  108. return $this->belongsTo('app\\common\\model\\Area', 'province_adcode', 'adcode', [], 'LEFT')->setEagerlyType(0);
  109. }
  110. /**
  111. * 关联地区表(市)
  112. */
  113. public function city()
  114. {
  115. return $this->belongsTo('app\\common\\model\\Area', 'city_adcode', 'adcode', [], 'LEFT')->setEagerlyType(0);
  116. }
  117. /**
  118. * 关联地区表(区县)
  119. */
  120. public function district()
  121. {
  122. return $this->belongsTo('app\\common\\model\\Area', 'district_adcode', 'adcode', [], 'LEFT')->setEagerlyType(0);
  123. }
  124. /**
  125. * 获取省级名称
  126. */
  127. public function getProvinceNameAttr($value, $data)
  128. {
  129. if (!empty($data['province_adcode'])) {
  130. $area = \app\common\model\Area::where('adcode', $data['province_adcode'])->find();
  131. return $area ? $area['name'] : '';
  132. }
  133. return '';
  134. }
  135. /**
  136. * 获取市级名称
  137. */
  138. public function getCityNameAttr($value, $data)
  139. {
  140. if (!empty($data['city_adcode'])) {
  141. $area = \app\common\model\Area::where('adcode', $data['city_adcode'])->find();
  142. return $area ? $area['name'] : '';
  143. }
  144. return '';
  145. }
  146. /**
  147. * 获取区域名称
  148. */
  149. public function getDistrictNameAttr($value, $data)
  150. {
  151. if (!empty($data['district_adcode'])) {
  152. $area = \app\common\model\Area::where('adcode', $data['district_adcode'])->find();
  153. return $area ? $area['name'] : '';
  154. }
  155. return '';
  156. }
  157. /**
  158. * 检查用户是否可以申请
  159. */
  160. public static function canApply($userId)
  161. {
  162. // 检查是否已有审核中或已通过的申请
  163. $existingApplication = self::where('user_id', $userId)
  164. ->where('audit_status', 'in', [self::AUDIT_STATUS_PENDING, self::AUDIT_STATUS_PASSED])
  165. ->find();
  166. return !$existingApplication;
  167. }
  168. /**
  169. * 检查手机号是否已申请
  170. */
  171. public static function isPhoneApplied($phone, $excludeId = null)
  172. {
  173. $where = ['phone' => $phone];
  174. if ($excludeId) {
  175. $where['id'] = ['neq', $excludeId];
  176. }
  177. return self::where($where)->find() ? true : false;
  178. }
  179. /**
  180. * 是否可以修改
  181. */
  182. public function canEdit()
  183. {
  184. return $this->audit_status == self::AUDIT_STATUS_PENDING;
  185. }
  186. }