InspectionApplication.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 supplier()
  107. {
  108. return $this->belongsTo('app\\common\\model\\supplier\\Index', 'supplier_id', 'id', [], 'LEFT')->setEagerlyType(0);
  109. }
  110. /**
  111. * 关联地区表(省)
  112. */
  113. public function province()
  114. {
  115. return $this->belongsTo('app\\common\\model\\Area', 'province_adcode', 'adcode', [], 'LEFT')->setEagerlyType(0);
  116. }
  117. /**
  118. * 关联地区表(市)
  119. */
  120. public function city()
  121. {
  122. return $this->belongsTo('app\\common\\model\\Area', 'city_adcode', 'adcode', [], 'LEFT')->setEagerlyType(0);
  123. }
  124. /**
  125. * 关联地区表(区县)
  126. */
  127. public function district()
  128. {
  129. return $this->belongsTo('app\\common\\model\\Area', 'district_adcode', 'adcode', [], 'LEFT')->setEagerlyType(0);
  130. }
  131. /**
  132. * 获取省级名称
  133. */
  134. public function getProvinceNameAttr($value, $data)
  135. {
  136. if (!empty($data['province_adcode'])) {
  137. $area = \app\common\model\Area::where('id', $data['province_adcode'])->find();
  138. return $area ? $area['name'] : '';
  139. }
  140. return '';
  141. }
  142. /**
  143. * 获取市级名称
  144. */
  145. public function getCityNameAttr($value, $data)
  146. {
  147. if (!empty($data['city_adcode'])) {
  148. $area = \app\common\model\Area::where('id', $data['city_adcode'])->find();
  149. return $area ? $area['name'] : '';
  150. }
  151. return '';
  152. }
  153. /**
  154. * 获取区域名称
  155. */
  156. public function getDistrictNameAttr($value, $data)
  157. {
  158. if (!empty($data['district_adcode'])) {
  159. $area = \app\common\model\Area::where('id', $data['district_adcode'])->find();
  160. return $area ? $area['name'] : '';
  161. }
  162. return '';
  163. }
  164. /**
  165. * 检查用户是否可以申请
  166. */
  167. public static function canApply($userId)
  168. {
  169. // 检查是否已有审核中或已通过的申请
  170. $existingApplication = self::where('user_id', $userId)
  171. ->where('audit_status', 'in', [self::AUDIT_STATUS_PENDING, self::AUDIT_STATUS_PASSED])
  172. ->find();
  173. return !$existingApplication;
  174. }
  175. /**
  176. * 检查手机号是否已申请
  177. */
  178. public static function isPhoneApplied($phone, $excludeId = null)
  179. {
  180. $where = ['phone' => $phone];
  181. if ($excludeId) {
  182. $where['id'] = ['neq', $excludeId];
  183. }
  184. return self::where($where)->find() ? true : false;
  185. }
  186. /**
  187. * 是否可以修改
  188. */
  189. public function canEdit()
  190. {
  191. return $this->audit_status == self::AUDIT_STATUS_PENDING;
  192. }
  193. }