InspectionApplication.php 5.9 KB

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