作者 何书鹏
1 个管道 的构建 通过 耗费 29 秒

新增功能与优化

... ... @@ -397,9 +397,17 @@ class User extends Backend
$fieldArr['密码'] = 'password';
$fieldArr['性别'] = 'sex';
$fieldArr['工作单位'] = 'work_address';
$fieldArr['报考科目'] = 'examname';
$fieldArr['报考科目到期时间'] = 'periodtime';
//加载文件
$insert_origin = [];
$insert = [];
$third_study_data = [];
$study_class_data = [];
$createtime = time();
$user_study_exist = [];
$user_exist = [];
try {
if (!$PHPExcel = $reader->load($filePath)) {
$this->error(__('Unknown data format'));
... ... @@ -440,32 +448,68 @@ class User extends Backend
$row['sex'] = 2;
break;
}
// $row['expirationtime'] = intval(($row['expirationtime'] - 25569) * 3600 * 24) - 28800; //转换成1970年以来的秒数
$insert[] = $row;
if(empty($row['username'])){
throw new Exception('第'.$currentRow.'行请填写用户名');
}
if(empty($row['password'])){
throw new Exception('第'.$currentRow.'行请填写密码');
}
if(!empty($row['examname']) && empty($row['periodtime'])){
throw new Exception('第'.$currentRow.'行请填写报考科目到期时间');
}
if(empty($row['examname']) && !empty($row['periodtime'])){
throw new Exception('第'.$currentRow.'行请填写报考科目');
}
$row['examname'] = !empty($row['examname']) ? $row['examname'] : '';
if(!isset($user_study_exist[$row['username'].'-'.$row['examname']])){
if ($user = UserModel::where('username',$row['username'])->field('id')->find()) {
// 添加报考科目信息
$study = Db::name('study')->where('examname',$row['examname'])->field('id')->find();
$third_study = Db::name('third_study')->where('study_id',$study['id'])->where('third_id',$user['id'])->find();
if($study && !$third_study){
$third_study_data[] = [
'third_id' => $user['id'],
'study_id' => $study['id'],
'periodtime' => intval(($row['periodtime'] - 25569) * 3600 * 24) - 28800, //转换成1970年以来的秒数
];
$classes_ids = Db::name('classes')->where('study_id',$study['id'])->where('is_major',1)->column('id');
foreach ($classes_ids as $class_id){
$study_class_data[] = [
'third_id' => $user['id'],
'study_id' => $study['id'],
'class_id' => $class_id,
'playtime' => 0,
'createtime' => $createtime,
];
}
}
$user_study_exist[$row['username'].'-'.$row['examname']] = 1;
}else{
// 原始数据
$insert_origin[] = $row;
$user_study_exist[$row['username'].'-'.$row['examname']] = 1;
if(!isset($user_exist[$row['username']])){
// 调整后的数据
unset($row['examname'],$row['periodtime']);
$insert[] = $row;
$user_exist[$row['username']] = 1;
}
}
}
}
}
} catch (Exception $exception) {
$this->error($exception->getMessage());
}
if (!$insert) {
// if (!$insert) {
// $this->error(__('No rows were updated'));
// }
if (!$insert && !$third_study_data && !$study_class_data) {
$this->error(__('No rows were updated'));
}
foreach ($insert as $k => $v){
if(empty($v['username'])){
$this->error('第'.($k+2).'行请填写用户名');
}
if(empty($v['password'])){
$this->error('第'.($k+2).'行请填写密码');
}
if (UserModel::getByUsername($v['username'])) {
$this->error('第'.($k+2).'行用户名已存在');
}
if (!empty($v['mobile']) && UserModel::getByMobile($v['mobile'])) {
$this->error('第'.($k+2).'行手机号已存在');
}
}
Db::startTrans();
try {
//是否包含admin_id字段
$has_admin_id = false;
... ... @@ -483,14 +527,49 @@ class User extends Backend
}
}
}
$this->model->saveAll($insert);
if($insert){
$this->model->saveAll($insert);
}
// 添加报考科目
foreach ($insert_origin as $v){
$third_id = UserModel::where('username',$v['username'])->value('id');
// 添加报考科目信息
$study = Db::name('study')->where('examname',$v['examname'])->field('id')->find();
$third_study = Db::name('third_study')->where('study_id',$study['id'])->where('third_id',$third_id)->find();
if($study && !$third_study){
$third_study_data[] = [
'third_id' => $third_id,
'study_id' => $study['id'],
'periodtime' => intval(($v['periodtime'] - 25569) * 3600 * 24) - 28800, //转换成1970年以来的秒数
];
$classes_ids = Db::name('classes')->where('study_id',$study['id'])->where('is_major',1)->column('id');
foreach ($classes_ids as $class_id){
$study_class_data[] = [
'third_id' => $third_id,
'study_id' => $study['id'],
'class_id' => $class_id,
'playtime' => 0,
'createtime' => $createtime,
];
}
}
}
if(!empty($third_study_data)){
Db::name('third_study')->insertAll($third_study_data);
}
if(!empty($study_class_data)){
Db::name('study_class')->insertAll($study_class_data);
}
Db::commit();
} catch (PDOException $exception) {
Db::rollback();
$msg = $exception->getMessage();
if (preg_match("/.+Integrity constraint violation: 1062 Duplicate entry '(.+)' for key '(.+)'/is", $msg, $matches)) {
$msg = "导入失败,包含【{$matches[1]}】的记录已存在";
};
$this->error($msg);
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
... ...
... ... @@ -41,7 +41,7 @@ use Endroid\QrCode\QrCode;
*/
class User extends Api
{
protected $noNeedLogin = ['registerUser','agreementUser','registerCompany','agreementCompany','agreementPrivacy','login','thirdLogin','thirdBindMobile','resetpwd','exam','noLogin','problemList','problemInfo'];
protected $noNeedLogin = ['registerUser','agreementUser','registerCompany','agreementCompany','agreementPrivacy','login','thirdLogin','thirdBindMobile','resetpwd','exam','noLogin','problemList','problemInfo','feedbackWrong','feedback'];
protected $noNeedRight = ['*'];
public function _initialize()
... ... @@ -1731,10 +1731,11 @@ class User extends Api
$content = $this->request->param('content');
empty($feedback_wrong_id) && $this->error('请选择错误类型');
empty($content) && $this->error('请填写反馈内容');
$user_id = $this->auth->isLogin() ? $this->auth->id : 0;
Feedback::create([
'user_id' => $this->auth->id,
'feedback_wrong_id' => $feedback_wrong_id,
'content' => $content
'user_id' => $user_id,
'feedback_wrong_id' => $feedback_wrong_id,
'content' => $content
]);
$this->success('反馈成功');
}
... ...
... ... @@ -115,7 +115,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
// {field: 'group.name', title: __('Group')},
{field: 'user.username', title: __('用户名'), operate:'LIKE'},
{field: 'user.nickname', title: __('真实姓名'), operate:'LIKE'},
{field: 'study.examname', title: __('报考科目'), searchList: Config.studyList,formatter: Table.api.formatter.flag},
{field: 'study.id', title: __('报考科目'), searchList: Config.studyList,formatter: Table.api.formatter.flag},
{field: 'score', title: __('成绩'), operate:false},
{field: 'is_pass', title: __('是否通过'), searchList: {"1":"通过","2":'未通过'},formatter: Table.api.formatter.flag},
{field: 'user.password', title: __('密码'), operate:false},
... ...