初始化项目:添加后端代码、ThinkPHP框架、前端资源
This commit is contained in:
@@ -0,0 +1,346 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\app\activity;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use think\Db;
|
||||
/**
|
||||
* 线下活动
|
||||
*
|
||||
* @icon fa fa-circle-o
|
||||
*/
|
||||
class Activity extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* Activity模型对象
|
||||
* @var \app\admin\model\app\activity\Activity
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\app\activity\Activity;
|
||||
$this->userTicketModel = new \app\admin\model\app\activity\Userticket;
|
||||
$this->ticketModel = new \app\admin\model\app\activity\Ticket;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
||||
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
||||
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//当前是否为关联查询
|
||||
$this->relationSearch = false;
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags', 'trim']);
|
||||
//如果发送的来源是Selectpage,则转发到Selectpage
|
||||
if ($this->request->request('keyField')) {
|
||||
return $this->selectpage();
|
||||
}
|
||||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||||
|
||||
$list = $this->model
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->paginate($limit);
|
||||
|
||||
foreach ($list as $row) {
|
||||
$row->ticket_count = $this->ticketModel->getTicketCount($row->id);
|
||||
|
||||
$row->apply_total_data = $this->userTicketModel->getApplyTotalData($row->id);
|
||||
}
|
||||
|
||||
$result = array("total" => $list->total(), "rows" => $list->items(),'attr'=>[
|
||||
'statusList'=>$this->model->getStatusList()
|
||||
]);
|
||||
|
||||
return $this->success("获取成功",$result);
|
||||
}
|
||||
|
||||
|
||||
public function formParse($params,$type = 'encode'){
|
||||
|
||||
if($type == 'encode'){
|
||||
$params['apply_info_form'] = json_encode($params['apply_info_form']);
|
||||
foreach ($params as $key => $val){
|
||||
if(substr($key,-4) == 'time'){
|
||||
$params[$key] = strtotime($params[$key]);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
$params['apply_info_form'] = json_decode($params['apply_info_form'],true);
|
||||
foreach ($params as $key => $val){
|
||||
if(substr($key,-4) == 'time'){
|
||||
$params[$key] = date('Y-m-d H:i:s',$params[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
$params['ticket'] = \app\admin\model\app\activity\Ticket::where([
|
||||
'activity_id'=>$params['id']
|
||||
])->field(['name','price','inventory','audit','limit_buy','id','weigh'])
|
||||
->order('weigh','asc')
|
||||
->select();
|
||||
|
||||
$course = \app\admin\model\app\activity\Course::alias('activity')
|
||||
->join(\app\admin\model\course\Course::getTable() .' course',"activity.course_id=course.id")
|
||||
->where([
|
||||
'activity.activity_id'=>$params['id']
|
||||
])->field([
|
||||
'course.name','course.type','course.cover','course.id'
|
||||
])
|
||||
|
||||
->select();
|
||||
|
||||
$params['course'] = $course;
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取详情
|
||||
* @param $id
|
||||
* @return mixed
|
||||
*/
|
||||
public function detail($id){
|
||||
$row = $this->model->get($id);
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
|
||||
$row = $row->toArray();
|
||||
|
||||
$row = $this->formParse($row,'decode');
|
||||
|
||||
return $this->success("获取成功",$row);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*
|
||||
* @return string
|
||||
* @throws \think\Exception
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if (false === $this->request->isPost()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
$params = $this->request->post('row/a');
|
||||
if (empty($params)) {
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$params = $this->preExcludeFields($params);
|
||||
|
||||
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
|
||||
$params[$this->dataLimitField] = $this->auth->id;
|
||||
}
|
||||
|
||||
$ticket = $params['ticket'];
|
||||
unset($params['ticket']);
|
||||
$course = $params['course'];
|
||||
unset($params['course']);
|
||||
|
||||
$params = $this->formParse($params);
|
||||
|
||||
$params['uniacid'] = UNIACID;
|
||||
$params['createtime'] = time();
|
||||
$result = false;
|
||||
Db::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()->validate($validate);
|
||||
}
|
||||
$result = $this->model->allowField(true)->insertGetId($params);
|
||||
$this->parseTicket($result,$ticket);
|
||||
$this->parseCourse($result,$course);
|
||||
|
||||
// 计算并更新价格
|
||||
$this->updateActivityPrice($result);
|
||||
|
||||
Db::commit();
|
||||
} catch (ValidateException|PDOException|Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($result === false) {
|
||||
$this->error(__('No rows were inserted'));
|
||||
}
|
||||
$this->success("提交成功");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 解析活动票
|
||||
* @param $rowId
|
||||
* @param $ticket
|
||||
* @return bool
|
||||
*/
|
||||
public function parseTicket($rowId,$ticket){
|
||||
|
||||
$hasTicketIds = \app\admin\model\app\activity\Ticket::where([
|
||||
'activity_id'=>$rowId
|
||||
])->field('id')->column('id');
|
||||
|
||||
|
||||
$updateTicketIds=[];
|
||||
|
||||
|
||||
if($ticket){
|
||||
foreach ($ticket as $item){
|
||||
$item['createtime'] = time();
|
||||
$item['activity_id'] = $rowId;
|
||||
$item['uniacid'] = UNIACID;
|
||||
|
||||
if(isset($item['id']) && $item['id']){
|
||||
$updateTicketIds[] = $item['id'];
|
||||
\app\admin\model\app\activity\Ticket::where([
|
||||
'id'=>$item['id']
|
||||
])->update($item);
|
||||
}else{
|
||||
\app\admin\model\app\activity\Ticket::insert($item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if($hasTicketIds){
|
||||
foreach ($hasTicketIds as $id){
|
||||
if(!$updateTicketIds || !in_array($id,$updateTicketIds)){
|
||||
\app\admin\model\app\activity\Ticket::where([
|
||||
'id'=>$id
|
||||
])->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定课程
|
||||
* @param $rowId
|
||||
* @param $course
|
||||
* @return bool
|
||||
*/
|
||||
public function parseCourse($rowId,$course){
|
||||
\app\admin\model\app\activity\Course::where([
|
||||
'activity_id'=>$rowId
|
||||
])->delete();
|
||||
if($course){
|
||||
foreach ($course as $item){
|
||||
$data = [];
|
||||
$data['course_id'] = $item;
|
||||
$data['createtime'] = time();
|
||||
$data['activity_id'] = $rowId;
|
||||
$data['uniacid'] = UNIACID;
|
||||
\app\admin\model\app\activity\Course::insert($data);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
*
|
||||
* @param $ids
|
||||
* @return string
|
||||
* @throws DbException
|
||||
* @throws \think\Exception
|
||||
*/
|
||||
public function edit($ids = null)
|
||||
{
|
||||
$row = $this->model->get($ids);
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
$adminIds = $this->getDataLimitAdminIds();
|
||||
if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
|
||||
$this->error(__('You have no permission'));
|
||||
}
|
||||
|
||||
$params = $this->request->post('row/a');
|
||||
if (empty($params)) {
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$params = $this->preExcludeFields($params);
|
||||
|
||||
$ticket = $params['ticket'];
|
||||
unset($params['ticket']);
|
||||
|
||||
$course = $params['course'];
|
||||
unset($params['course']);
|
||||
|
||||
$params = $this->formParse($params);
|
||||
$result = false;
|
||||
Db::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()->validate($validate);
|
||||
}
|
||||
$result = $row->allowField(true)->save($params);
|
||||
$this->parseTicket($row->id,$ticket);
|
||||
$this->parseCourse($row->id,$course);
|
||||
|
||||
// 计算并更新价格
|
||||
$this->updateActivityPrice($row->id);
|
||||
|
||||
Db::commit();
|
||||
} catch (ValidateException|PDOException|Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if (false === $result) {
|
||||
$this->error(__('No rows were updated'));
|
||||
}
|
||||
$this->success("提交成功");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 更新活动价格
|
||||
* 从 ticket 表中读取最低价格
|
||||
* @param int $activityId 活动ID
|
||||
*/
|
||||
protected function updateActivityPrice($activityId)
|
||||
{
|
||||
$minPrice = $this->ticketModel
|
||||
->where('activity_id', $activityId)
|
||||
->where('uniacid', UNIACID)
|
||||
->min('price');
|
||||
|
||||
$price = $minPrice ?? 0;
|
||||
|
||||
// 更新价格
|
||||
$this->model->where('id', $activityId)->update([
|
||||
'price' => $price
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user