123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- namespace app\common\model\inspection;
- use think\Model;
- use traits\model\SoftDelete;
- /**
- * 验货员申请模型
- */
- class InspectionApplication extends Model
- {
- use SoftDelete;
- // 表名
- protected $table = 'inspection_application';
-
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
-
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = 'deletetime';
- // 追加属性
- protected $append = [
- 'audit_status_text',
- 'apply_time_text',
- 'audit_time_text',
- 'province_name',
- 'city_name',
- 'district_name',
- 'supplier_name',
- ];
- // 审核状态常量
- const AUDIT_STATUS_PENDING = 1; // 审核中
- const AUDIT_STATUS_PASSED = 2; // 通过
- const AUDIT_STATUS_REJECTED = 3; // 驳回
- /**
- * 获取审核状态列表
- */
- public function getAuditStatusList()
- {
- return [
- self::AUDIT_STATUS_PENDING => '审核中',
- self::AUDIT_STATUS_PASSED => '审核通过',
- self::AUDIT_STATUS_REJECTED => '审核驳回'
- ];
- }
- /**
- * 获取审核状态文本
- */
- public function getAuditStatusTextAttr($value, $data)
- {
- $status = $value ?: ($data['audit_status'] ?? self::AUDIT_STATUS_PENDING);
- $list = $this->getAuditStatusList();
- return $list[$status] ?? '未知状态';
- }
- /**
- * 获取申请时间文本
- */
- public function getApplyTimeTextAttr($value, $data)
- {
- $time = $value ?: ($data['apply_time'] ?? 0);
- return $time > 0 ? date('Y-m-d H:i:s', $time) : '';
- }
- /**
- * 获取审核时间文本
- */
- public function getAuditTimeTextAttr($value, $data)
- {
- $time = $value ?: ($data['audit_time'] ?? 0);
- return $time > 0 ? date('Y-m-d H:i:s', $time) : '';
- }
- /**
- * 获取供应商名称
- */
- public function getSupplierNameAttr($value, $data)
- {
- if (!empty($data['supplier_id'])) {
- $supplier = \app\common\model\supplier\Index::where('id', $data['supplier_id'])->find();
- return $supplier ? $supplier['name'] : '';
- }
- return '';
- }
- /**
- * 获取身份证正面图片完整URL
- */
- public function getIdCardFrontAttr($value)
- {
- return $value ? cdnurl($value, true) : '';
- }
- /**
- * 获取身份证反面图片完整URL
- */
- public function getIdCardBackAttr($value)
- {
- return $value ? cdnurl($value, true) : '';
- }
- /**
- * 设置身份证正面图片
- */
- public function setIdCardFrontAttr($value)
- {
- return $value ? str_replace(request()->domain(), '', $value) : '';
- }
- /**
- * 设置身份证反面图片
- */
- public function setIdCardBackAttr($value)
- {
- return $value ? str_replace(request()->domain(), '', $value) : '';
- }
- /**
- * 关联用户表
- */
- public function user()
- {
- return $this->belongsTo('app\\common\\model\\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- /**
- * 关联供应商表
- */
- public function supplier()
- {
- return $this->belongsTo('app\\common\\model\\supplier\\Index', 'supplier_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- /**
- * 关联地区表(省)
- */
- public function province()
- {
- return $this->belongsTo('app\\common\\model\\Area', 'province_adcode', 'adcode', [], 'LEFT')->setEagerlyType(0);
- }
- /**
- * 关联地区表(市)
- */
- public function city()
- {
- return $this->belongsTo('app\\common\\model\\Area', 'city_adcode', 'adcode', [], 'LEFT')->setEagerlyType(0);
- }
- /**
- * 关联地区表(区县)
- */
- public function district()
- {
- return $this->belongsTo('app\\common\\model\\Area', 'district_adcode', 'adcode', [], 'LEFT')->setEagerlyType(0);
- }
- /**
- * 获取省级名称
- */
- public function getProvinceNameAttr($value, $data)
- {
- if (!empty($data['province_adcode'])) {
- $area = \app\common\model\Area::where('id', $data['province_adcode'])->find();
- return $area ? $area['name'] : '';
- }
- return '';
- }
- /**
- * 获取市级名称
- */
- public function getCityNameAttr($value, $data)
- {
- if (!empty($data['city_adcode'])) {
- $area = \app\common\model\Area::where('id', $data['city_adcode'])->find();
- return $area ? $area['name'] : '';
- }
- return '';
- }
- /**
- * 获取区域名称
- */
- public function getDistrictNameAttr($value, $data)
- {
- if (!empty($data['district_adcode'])) {
- $area = \app\common\model\Area::where('id', $data['district_adcode'])->find();
- return $area ? $area['name'] : '';
- }
- return '';
- }
- /**
- * 检查用户是否可以申请
- */
- public static function canApply($userId)
- {
- // 检查是否已有审核中或已通过的申请
- $existingApplication = self::where('user_id', $userId)
- ->where('audit_status', 'in', [self::AUDIT_STATUS_PENDING, self::AUDIT_STATUS_PASSED])
- ->find();
- return !$existingApplication;
- }
- /**
- * 检查手机号是否已申请
- */
- public static function isPhoneApplied($phone, $excludeId = null)
- {
- $where = ['phone' => $phone];
- if ($excludeId) {
- $where['id'] = ['neq', $excludeId];
- }
-
- return self::where($where)->find() ? true : false;
- }
- /**
- * 是否可以修改
- */
- public function canEdit()
- {
- return $this->audit_status == self::AUDIT_STATUS_PENDING;
- }
- }
|