InspectionApplication.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. 'supplier_name',
  28. ];
  29. // 审核状态常量
  30. const AUDIT_STATUS_PENDING = 1; // 审核中
  31. const AUDIT_STATUS_PASSED = 2; // 通过
  32. const AUDIT_STATUS_REJECTED = 3; // 驳回
  33. /**
  34. * 获取审核状态列表
  35. */
  36. public function getAuditStatusList()
  37. {
  38. return [
  39. self::AUDIT_STATUS_PENDING => '审核中',
  40. self::AUDIT_STATUS_PASSED => '审核通过',
  41. self::AUDIT_STATUS_REJECTED => '审核驳回'
  42. ];
  43. }
  44. /**
  45. * 获取审核状态文本
  46. */
  47. public function getAuditStatusTextAttr($value, $data)
  48. {
  49. $status = $value ?: ($data['audit_status'] ?? self::AUDIT_STATUS_PENDING);
  50. $list = $this->getAuditStatusList();
  51. return $list[$status] ?? '未知状态';
  52. }
  53. /**
  54. * 获取申请时间文本
  55. */
  56. public function getApplyTimeTextAttr($value, $data)
  57. {
  58. $time = $value ?: ($data['apply_time'] ?? 0);
  59. return $time > 0 ? date('Y-m-d H:i:s', $time) : '';
  60. }
  61. /**
  62. * 获取审核时间文本
  63. */
  64. public function getAuditTimeTextAttr($value, $data)
  65. {
  66. $time = $value ?: ($data['audit_time'] ?? 0);
  67. return $time > 0 ? date('Y-m-d H:i:s', $time) : '';
  68. }
  69. /**
  70. * 获取供应商名称
  71. */
  72. public function getSupplierNameAttr($value, $data)
  73. {
  74. if (!empty($data['supplier_id'])) {
  75. $supplier = \app\common\model\supplier\Index::where('id', $data['supplier_id'])->find();
  76. return $supplier ? $supplier['name'] : '';
  77. }
  78. return '';
  79. }
  80. /**
  81. * 获取身份证正面图片完整URL
  82. */
  83. public function getIdCardFrontAttr($value)
  84. {
  85. return $value ? cdnurl($value, true) : '';
  86. }
  87. /**
  88. * 获取身份证反面图片完整URL
  89. */
  90. public function getIdCardBackAttr($value)
  91. {
  92. return $value ? cdnurl($value, true) : '';
  93. }
  94. /**
  95. * 设置身份证正面图片
  96. */
  97. public function setIdCardFrontAttr($value)
  98. {
  99. return $value ? str_replace(request()->domain(), '', $value) : '';
  100. }
  101. /**
  102. * 设置身份证反面图片
  103. */
  104. public function setIdCardBackAttr($value)
  105. {
  106. return $value ? str_replace(request()->domain(), '', $value) : '';
  107. }
  108. /**
  109. * 关联用户表
  110. */
  111. public function user()
  112. {
  113. return $this->belongsTo('app\\common\\model\\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
  114. }
  115. /**
  116. * 关联供应商表
  117. */
  118. public function supplier()
  119. {
  120. return $this->belongsTo('app\\common\\model\\supplier\\Index', 'supplier_id', 'id', [], 'LEFT')->setEagerlyType(0);
  121. }
  122. /**
  123. * 关联地区表(省)
  124. */
  125. public function province()
  126. {
  127. return $this->belongsTo('app\\common\\model\\Area', 'province_adcode', 'adcode', [], 'LEFT')->setEagerlyType(0);
  128. }
  129. /**
  130. * 关联地区表(市)
  131. */
  132. public function city()
  133. {
  134. return $this->belongsTo('app\\common\\model\\Area', 'city_adcode', 'adcode', [], 'LEFT')->setEagerlyType(0);
  135. }
  136. /**
  137. * 关联地区表(区县)
  138. */
  139. public function district()
  140. {
  141. return $this->belongsTo('app\\common\\model\\Area', 'district_adcode', 'adcode', [], 'LEFT')->setEagerlyType(0);
  142. }
  143. /**
  144. * 获取省级名称
  145. */
  146. public function getProvinceNameAttr($value, $data)
  147. {
  148. if (!empty($data['province_adcode'])) {
  149. $area = \app\common\model\Area::where('id', $data['province_adcode'])->find();
  150. return $area ? $area['name'] : '';
  151. }
  152. return '';
  153. }
  154. /**
  155. * 获取市级名称
  156. */
  157. public function getCityNameAttr($value, $data)
  158. {
  159. if (!empty($data['city_adcode'])) {
  160. $area = \app\common\model\Area::where('id', $data['city_adcode'])->find();
  161. return $area ? $area['name'] : '';
  162. }
  163. return '';
  164. }
  165. /**
  166. * 获取区域名称
  167. */
  168. public function getDistrictNameAttr($value, $data)
  169. {
  170. if (!empty($data['district_adcode'])) {
  171. $area = \app\common\model\Area::where('id', $data['district_adcode'])->find();
  172. return $area ? $area['name'] : '';
  173. }
  174. return '';
  175. }
  176. /**
  177. * 检查用户是否可以申请
  178. */
  179. public static function canApply($userId)
  180. {
  181. // 检查是否已有审核中或已通过的申请
  182. $existingApplication = self::where('user_id', $userId)
  183. ->where('audit_status', 'in', [self::AUDIT_STATUS_PENDING, self::AUDIT_STATUS_PASSED])
  184. ->find();
  185. return !$existingApplication;
  186. }
  187. /**
  188. * 检查手机号是否已申请
  189. */
  190. public static function isPhoneApplied($phone, $excludeId = null)
  191. {
  192. $where = ['phone' => $phone];
  193. if ($excludeId) {
  194. $where['id'] = ['neq', $excludeId];
  195. }
  196. return self::where($where)->find() ? true : false;
  197. }
  198. /**
  199. * 是否可以修改
  200. */
  201. public function canEdit()
  202. {
  203. return $this->audit_status == self::AUDIT_STATUS_PENDING;
  204. }
  205. }