// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\common\model; use app\api\validate\user\EducationExp; use cores\BaseModel; use think\model\relation\HasOne; /** * 学生教育经历模型类 * Class StudentEduexp * @package app\common\model */ class StudentEduexp extends BaseModel { protected $globalScope = ['']; // 定义表名 protected $name = 'student_eduexp'; // 定义主键 protected $pk = 'id'; /** * 获取凭证图片 * @param string $value * @return string */ public function getCertifyImgAttr(string $value): string { return $value? getPreview($value) :''; } /** * 获取用户最近的教育经历 * @param $userId * @return StudentEduexp|array|mixed|\think\Model|null * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function getInfoByUser($userId) { if($userId<=0){ return false; } return self::where(['user_id'=> $userId])->order('id desc')->find(); } /** * @param $params * @return bool * @throws \cores\exception\BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function setData($params) { $validate = new EducationExp(); if(!$validate->check($params)){ throwError($validate->getError()); } $userInfo = \app\api\service\User::getCurrentLoginUser(true); $userId = isset($userInfo['user_id'])? intval($userInfo['user_id']) : 0; $info = self::getInfoByUser($userId); $certifyImg = isset($params['certify_img'])? $params['certify_img'] : ''; if($info && $info['status'] == 2 && $checkModify = self::checkModify($params, $info) && empty($certifyImg)){ throwError('您已修改过信息,请先上传凭证'); } $data = [ 'user_id'=> $userId, 'education'=> isset($params['education'])? $params['education'] : '', 'school_name'=> isset($params['school_name'])? $params['school_name'] : '', 'speciality'=> isset($params['speciality'])? $params['speciality'] : '', 'grade'=> isset($params['grade'])? $params['grade'] : '', 'status'=> 1, ]; if($certifyImg){ $data['certify_img'] = $certifyImg; } if($info){ return $info->save($data); }else{ return self::save($data); } } /** * 验证是否修改过内容 * @param $params * @param $info * @return bool */ public static function checkModify($params, $info) { foreach ($params as $k => $v){ if($k != 'certify_img' && isset($info[$k]) && $info[$k] != $v){ return true; } } return false; } }