car4s/application/admin/controller/user4s/User.php
孟凡懂 dc5d9598be
All checks were successful
continuous-integration/drone/push Build is passing
更新导入功能更完善
2022-06-21 04:25:24 +08:00

577 lines
22 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\admin\controller\user4s;
use app\common\controller\Backend;
use app\common\model\user4s\Category;
use app\common\model\user4s\Level;
use app\common\model\user4s\Log;
use app\admin\model\Admin;
use Exception;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Config;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use fast\Pinyin;
/**
* 用户档案
*
* @icon fa fa-circle-o
*/
class User extends Backend
{
/**
* User模型对象
* @var \app\common\model\user4s\User
*/
protected $model = null;
protected $searchFields = 'id,name,tel,carno';
/**
* 是否开启数据限制
* 支持auth/personal
* 表示按权限判断/仅限个人
* 默认为禁用,若启用请务必保证表中存在admin_id字段
*/
protected $dataLimit = 'auth';
/**
* 数据限制字段
*/
protected $dataLimitField = 'admin_id';
public function _initialize()
{
parent::_initialize();
$this->model = new \app\common\model\user4s\User;
$this->level_model = new \app\common\model\user4s\Level;
$this->levellog_model = new \app\common\model\user4s\Levellog;
$this->log_model = new \app\common\model\user4s\Log;
$this->view->assign("genderdataList", $this->model->getGenderdataList());
$this->view->assign("statusList", $this->model->getStatusList());
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
/**
* 查看
*/
public function index()
{
//当前是否为关联查询
$this->relationSearch = true;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isAjax()) {
//如果发送的来源是Selectpage则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
//var_dump($where);
$list = $this->model
->with(['category', 'level'])
->where($where)
->order($sort, $order)
->paginate($limit);
foreach ($list as $row) {
$row->getRelation('category')->visible(['name']);
$row->getRelation('level')->visible(['name']);
}
$result = array("total" => $list->total(), "rows" => $list->items());
return json($result);
}
return $this->view->fetch();
}
public function info($id = null)
{
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isAjax()) {
$id = $this->request->param('id');
if (empty($id)) {
$result = array("code" => 1, "msg" => '用户ID为空');
return json($result);
};
$row = $this->model->get($id);
if (!$row) {
$result = array("code" => 1, "msg" => __('No Results were found'));
return json($result);
} else {
//$row->getRelation('category')->visible(['name']);
//$row->getRelation('level')->visible(['name']);
$result = array("code" => 0, "msg" => 'success', "data" => $row);
return json($result);
}
}
$result = array("code" => 1, "msg" => '出现错误,请重试!');
return json($result);
}
public function add()
{
if ($this->request->isPost()) {
//$this->token();
}
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
$params = $this->preExcludeFields($params);
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$params[$this->dataLimitField] = $this->auth->id;
}
$result = false;
$this->model->startTrans();
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
$this->model->validateFailException(true)->validate($validate);
}
$cardinfo = $this->checkcardid($params['cardid']);
if ($cardinfo['iscard'] == false) {
$this->model->rollback();
$this->error('身份证号错误');
} else {
$params['birthday'] = $cardinfo['birthday'];
$params['genderdata'] = $cardinfo['sex'];
}
$has_frameno = $this->model->get(['frameno', $params['frameno']]);
if (!empty($has_frameno->id)) {
$this->model->rollback();
$this->error('车架号重复');
}
$result = $this->model->allowField(true)->save($params);
//var_dump($params['need_visit']);
//新加用户期初等级
if ($result && $this->model->id) {
if ($params['need_visit'] == 1) {
$log_res = $this->model->afterlog($this->auth->id, $this->model, $this->model->id, '', true);
} else {
$log_res = $this->model->afterlog($this->auth->id, $this->model, $this->model->id, '', false);
}
if (!$log_res) {
$this->model->rollback();
$this->error('新增失败');
}
}
//var_dump($result,$this->model->id);
$this->model->commit();
} catch (ValidateException $e) {
$this->model->rollback();
$this->error($e->getMessage());
} catch (PDOException $e) {
$this->model->rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
$this->model->rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success();
} else {
$this->error(__('No rows were inserted'));
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
return $this->view->fetch();
}
/**
* 编辑
*/
public function edit($ids = null)
{
if ($this->request->isPost()) {
//$this->token();
}
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
if (!in_array($row[$this->dataLimitField], $adminIds)) {
$this->error(__('You have no permission'));
}
}
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
$params = $this->preExcludeFields($params);
$result = false;
$this->model->startTrans();
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
$row->validateFailException(true)->validate($validate);
}
$cardinfo = $this->checkcardid($params['cardid']);
if ($cardinfo['iscard'] == false) {
$this->model->rollback();
$this->error('身份证号错误');
} else {
$params['birthday'] = $cardinfo['birthday'];
$params['genderdata'] = $cardinfo['sex'];
}
$has_frameno = $this->model->get(['frameno', $params['frameno']]);
if (!empty($has_frameno->id)) {
$this->model->rollback();
$this->error('车架号重复');
}
$result = $this->model->allowField(true)->save($params);
$result = $row->allowField(true)->save($params);
$this->model->commit();
} catch (ValidateException $e) {
$this->model->rollback();
$this->error($e->getMessage());
} catch (PDOException $e) {
$this->model->rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
$this->model->rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success();
} else {
$this->error(__('No rows were updated'));
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
$this->view->assign("row", $row);
return $this->view->fetch();
}
public function check_cardid()
{
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
$cardinfo = $this->checkcardid($params['cardid']);
if ($cardinfo['iscard']) {
$this->success('身份证号正确');
}
}
$this->error('身份证号错误');
}
$this->error('获取数据错误');
}
public function check_frameno()
{
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
$has_frameno = $this->model->where(['frameno' => $params['frameno']])->find();
//var_dump($has_frameno);
if (empty($has_frameno->id)) {
$this->success('车架号正确');
}
}
$this->error('车架号重复');
}
$this->error('车架号重复');
}
/**
* 删除
*/
// public function del($ids = "")
// {
// if (!$this->request->isPost()) {
// $this->error(__("Invalid parameters"));
// }
// $ids = $ids ? $ids : $this->request->post("ids");
// if ($ids) {
// if($ids == 1 || in_array('1',$ids)){
// $this->error(__('默认等级(ID:1)禁止删除,请重新操作!'));
// }
// $pk = $this->model->getPk();
// $adminIds = $this->getDataLimitAdminIds();
// if (is_array($adminIds)) {
// $this->model->where($this->dataLimitField, 'in', $adminIds);
// }
// $list = $this->model->where($pk, 'in', $ids)->select();
// $count = 0;
// $this->model->startTrans();
// try {
// foreach ($list as $k => $v) {
// $count += $v->delete();
// //删除用户记录
// //$log_res = $this->model->afterlog($this->auth->id,$this->model,$v->id,'',false);
// }
// $this->model->commit();
// } catch (PDOException $e) {
// $this->model->rollback();
// $this->error($e->getMessage());
// } catch (Exception $e) {
// $this->model->rollback();
// $this->error($e->getMessage());
// }
// if ($count) {
// $this->success();
// } else {
// $this->error(__('No rows were deleted'));
// }
// }
// $this->error(__('Parameter %s can not be empty', 'ids'));
// }
/**
* 充值
*/
public function recharge($ids = null)
{
if ($this->request->isPost()) {
$this->token();
}
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
if (!in_array($row[$this->dataLimitField], $adminIds)) {
$this->error(__('You have no permission'));
}
}
$all_level = $this->level_model->where('status', 1)->column('id,name');
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
//var_dump($params);
$user_data = array(
'id' => $row->id
);
//记录用户充值、积分、等级记录
$log_data = array(
'user4s_id' => $row->id,
'admin_id' => $this->auth->id
);
//余额充值
if ($params['balance'] > 0) {
$now = round($params['balance'], 2);
$end = round($row->balance + $now, 2);
$log_data['start'] = $row->balance;
$log_data['end'] = $end;
$log_data['state'] = 0;
$log_data['description'] = $params['description'];
$this->log_model->isUpdate(false)->save($log_data);
$user_data['balance'] = $end;
}
//积分记录
if ($params['integral'] > 0) {
$now = intval($params['integral']);
$end = intval($row->integral + $now);
$log_data['start'] = $row->integral;
$log_data['end'] = $end;
$log_data['state'] = 1;
if ($now > 0) {
$log_data['description'] = $params['description'] . ' 赠送积分:' . $now;
} else {
$log_data['description'] = $params['description'] . ' 系统后台扣除积分' . $now;
}
$this->log_model = new \app\common\model\user4s\Log();
$this->log_model->isUpdate(false)->save($log_data);
$user_data['integral'] = $end;
//var_dump($log_data, $user_data);
}
//等级记录
if ($params['level_id'] != $row->level_id && $params['level_id'] > 0) {
$end = intval($params['level_id']);
$log_data['start'] = $row->level_id;
$log_data['end'] = $end;
$log_data['description'] = '会员等级变动,由 ' . $all_level[$row->level_id] . ' 更改为:' . $all_level[$end];
//var_dump($log_data);
$this->levellog_model->isUpdate(false)->save($log_data);
$user_data['level_id'] = $end;
}
if (isset($user_data['balance']) || isset($user_data['integral']) || isset($user_data['level_id'])) {
//var_dump($user_data);
//$this->model->get($row->id);
$this->model->save($user_data, ['id' => $row->id]);
}
$this->success();
}
//var_dump($col);
$this->view->assign("all_level", $all_level);
$this->view->assign('row', $row);
return $this->view->fetch();
}
/**
* 保险临期用户列表
*/
public function expireins()
{
//当前是否为关联查询
$this->relationSearch = true;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
$start_date = strtotime("-1 year");
$end_date = strtotime("+2 month", $start_date);
$ins_where = array(
'insdate' => ['between', [date('Y-m-d', $start_date), date('Y-m-d', $end_date)]]
);
if ($this->request->isAjax()) {
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$list = $this->model
->with(['category', 'level'])
->where($where)
->where(function ($query) {
for ($y=1; $y<=20; $y++) {
$start_date = strtotime("-".$y." year");
$end_date = strtotime("+2 month", $start_date);
$ins_where = array(
'insdate' => ['between', [date('Y-m-d', $start_date), date('Y-m-d', $end_date)]]
);
$query->whereOr($ins_where);
}
})
->order($sort, $order)
->paginate($limit);
//var_dump($this->model->getLastSql());
foreach ($list as $row) {
$row->getRelation('category')->visible(['name']);
$row->getRelation('level')->visible(['name']);
}
$result = array("total" => $list->total(), "rows" => $list->items(), "extend" => ['start_date' => date('m-d', $start_date), 'end_date' => date('m-d', $end_date)]);
return json($result);
}
return $this->view->fetch();
}
/**
* 续保操作
*/
public function renewal($ids = null)
{
if ($this->request->isPost()) {
$this->token();
}
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
if (!in_array($row[$this->dataLimitField], $adminIds)) {
$this->error(__('You have no permission'));
}
}
//$all_level = $this->level_model->where('status', 1)->column('id,name');
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
//记录用户续保记录
if (!empty($params['inscom']) && !empty($params['insdate'])) {
$log_data = array(
'user4s_id' => $row->id,
'admin_id' => $this->auth->id,
'instype' => $params['instype'],
'inscom' => $params['inscom'],
'insdate' => $params['insdate'],
'star' => $params['star'],
'description' => $params['description']
);
$this->Inslog_model = new \app\common\model\user4s\Inslog();
$this->Inslog_model->isUpdate(false)->save($log_data);
$user_data = array(
'instype' => $params['instype'],
'inscom' => $params['inscom'],
'insdate' => $params['insdate'],
);
$this->model->save($user_data, ['id' => $row->id]);
} else {
$this->error('请填写续保信息', url('user4s/user/expireins', ['user4s_id' => $row->id]));
}
$this->success('续保成功!', url('user4s/user/expireins', ['user4s_id' => $row->id]));
}
//var_dump($col);
$all_star = array(
1 => '★',
2 => '★★',
3 => '★★★',
4 => '★★★★',
5 => '★★★★★',
);
$this->view->assign("now", date('Y-m-d'));
$this->view->assign("all_star", $all_star);
$this->view->assign('row', $row);
return $this->view->fetch();
}
/**
* 过生日用户列表
*/
public function birthday()
{
//当前是否为关联查询
$this->relationSearch = true;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isAjax()) {
$birthday_users = Db::query("select id from car_user4s_user where MONTH(birthday) = MONTH(NOW()) and DAY(birthday) = DAY(NOW())");
if(empty($birthday_users)){
$result = array("total" => 0, "rows" => [], "extend" => ['start_date' => date('m-d')]);
return json($result);
}else{
foreach ($birthday_users as $key => $value) {
$birthday_user_ids[] = $value['id'];
}
}
if(empty($birthday_user_ids)){
$result = array("total" => 0, "rows" => [], "extend" => ['start_date' => date('m-d')]);
return json($result);
}
//var_dump($birthday_user_ids);
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$list = $this->model
->with(['category', 'level'])
->where($where)
->where('user.id', 'in', $birthday_user_ids)
->order($sort, $order)
->paginate($limit);
//var_dump($this->model->getLastSql());
foreach ($list as $row) {
$row->getRelation('category')->visible(['name']);
$row->getRelation('level')->visible(['name']);
}
$result = array("total" => $list->total(), "rows" => $list->items(), "extend" => ['start_date' => date('m-d')]);
return json($result);
}
return $this->view->fetch();
}
}