DiverseYouthNightSchool/application/admin/controller/school/classes/ClassesLib.php

536 lines
17 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\school\classes;
use app\admin\model\dyqc\ManystoreShop;
use app\common\controller\Backend;
use app\common\model\school\classes\Order;
use app\manystore\model\Manystore;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\exception\ValidateException;
/**
* 机构课程库
*
* @icon fa fa-circle-o
*/
class ClassesLib extends Backend
{
protected $qSwitch = true;
protected $qFields = ["teacher_id","user_id","shop_id","manystore_id"];
/**
* ClassesLib模型对象
* @var \app\admin\model\school\classes\ClassesLib
*/
protected $model = null;
//不用审核允许修改的字段
protected $no_auth_fields = ['headimage','images','notice','content',"virtual_num","virtual_collect","underline_price"];
//更新数据是否需要触发审核开关
protected $need_auth = false;
protected $have_auth = false;
protected $success_auth = false;
protected $error_auth = false;
public function _initialize()
{
$this->model = new \app\admin\model\school\classes\ClassesLib;
parent::_initialize();
$this->view->assign("addTypeList", $this->model->getAddTypeList());
$this->view->assign("typeList", $this->model->getTypeList());
$this->view->assign("addressTypeList", $this->model->getAddressTypeList());
$this->view->assign("statusList", $this->model->getStatusList());
$this->view->assign("authStatusList", $this->model->getAuthStatusList());
$this->view->assign("recommendList", $this->model->getRecommendList());
$this->view->assign("hotList", $this->model->getHotList());
$this->view->assign("newList", $this->model->getNewList());
$this->view->assign("selfhotList", $this->model->getSelfhotList());
}
/**
* 默认生成的控制器所继承的父类中有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();
$list = $this->model
->with(['manystore','shop','user','admin'])
->where($where)
->order($sort, $order)
->paginate($limit);
foreach ($list as $row) {
$row->getRelation('manystore')->visible(['nickname']);
$row->getRelation('shop')->visible(['name','image','address_city','province','city','district','address','address_detail']);
$row->getRelation('user')->visible(['nickname','realname','mobile','avatar']);
$row->getRelation('admin')->visible(['nickname']);
}
$result = array("total" => $list->total(), "rows" => $list->items());
return json($result);
}
return $this->view->fetch();
}
protected function authClasses(&$params,$row=null){
//审核失败需填写原因
if($params["auth_status"] == '2' && empty($params["reason"])){
$this->error("审核失败需填写原因");
}
if($params["auth_status"] == '2'){
//审核不通过会平台下架
$params["status"] = '3';
}
//更新
if($row){
if($params["auth_status"] != '1' && $row["auth_status"] == '1'){
$this->error("审核已通过的课程不允许再修改审核状态!");
}
if($params["auth_status"] != '0' && $row["auth_status"] == '0'){
//填写审核时间和审核人
$params["auth_time"] = time();
$params["admin_id"] = $this->auth->id;
if($params["auth_status"] == '1'){
//审核通过
$this->success_auth = true;
}
if($params["auth_status"] == '2'){
//审核通过
$this->error_auth = true;
}
}
//审核通过
if($this->success_auth){
//如果是平台下架,则更新成正常下架
if($params["status"] == '3') $params["status"] = '2';
//调用通过事件
// $data = ['shop' => $row];
// \think\Hook::listen('classes_auth_success_after', $data);
}
if($this->error_auth){
//调用通过事件
// $data = ['shop' => $row];
// \think\Hook::listen('classes_auth_fail_after', $data);
}
}else{
//新增
}
}
protected function no_auth_fields_check($params,$row){
foreach ($params as $k=>$v){
//说明数值有变动
//$params[$k] 去掉两端空格
$params[$k] = trim($v);
if($row[$k]!=$params[$k]){
//当修改参数不在允许修改的字段中
if(!in_array($k,$this->no_auth_fields)){
$this->have_auth = true;break;
}
}
}
return $this->have_auth;
}
protected function updateCheck($id,$params=[],$row=null){
if($params && $row){
if(!$this->no_auth_fields_check($params,$row)){
return true;
}
}
// 课程存在未完成订单则不允许操作
$order = Order::where("classes_lib_id",$id)->where("status","in","0,3")->find();
if($order)$this->error("存在正在使用中的课程订单或存在正在售后中的课程订单无法继续操作!");
// 课程存在售后订单则不允许操作
}
protected function update_check(&$params,$row=null)
{
$shop_id = $params["shop_id"];
$manystore = Manystore::where("shop_id",$shop_id)->find();
if(!$manystore){
$this->error("店铺不存在");
}
if(!(new \app\common\model\dyqc\ManystoreShop)->checkFull($shop_id))$this->error("对方的认证信息未完善,请您先去帮忙完善!");
$params["manystore_id"] = $manystore["id"];
//验证老师id
$teacher_id = $params['teacher_id'];
$teacher = \app\admin\model\school\classes\Teacher::where("id",$teacher_id)->find();
if(!$teacher){
$this->error("老师不存在");
}
if(!$teacher["user_id"]){
$this->error("当前老师没有前端用户!请换一个!");
}
//老师与当前机构id不一致
if($teacher["manystore_id"] != $manystore["id"]){
$this->error("当前老师与当前机构不匹配,请换一个老师!");
}
$params["user_id"] = $teacher["user_id"];
//课时必须大于等于1
if($params["classes_num"] < 1) $this->error("课时必须大于等于1");
if($params["price"] < 0) $this->error("售价必须大于0");
//独立地点需传定位信息
if($params["address_type"] == "2"){
if(empty($params["address_city"])
|| empty($params["province"])
|| empty($params["city"])
|| empty($params["district"])
|| empty($params["longitude"])
|| empty($params["latitude"])) $this->error("独立地点需传定位信息");
}else{
//地址取机构的
$shop = ManystoreShop::where("id",$shop_id)->find();
if(!$shop){
$this->error("店铺不存在");
}
if(empty($shop["address_city"])
|| empty($shop["province"])
|| empty($shop["city"])
|| empty($shop["district"])
|| empty($shop["longitude"])
|| empty($shop["latitude"])) $this->error("当前机构地址并未完善,请在机构处完善!");
$params["address_city"] = $shop["address_city"];
$params["province"] = $shop["province"];
$params["city"] = $shop["city"];
$params["district"] = $shop["district"];
$params["longitude"] = $shop["longitude"];
$params["latitude"] = $shop["latitude"];
}
//特有认证判断
$this->authClasses($params,$row);
// var_dump($row);die;
//收费免费判断
if($params["price"]==0){
$params["feel"] = "1";
}else{
//个人认证无法下付费课程
$shop = ManystoreShop::where("id",$shop_id)->find();
if($shop["type"]=="1"){
$this->error("这个机构属于个人认证,无法发布付费课程!");
}
$params["feel"] = "0";
}
//更新
if($row){
$this->have_auth = false;
if($this->need_auth){
//判断更新的变动数据
$this->no_auth_fields_check($params,$row);
if($this->have_auth){
$params['status'] = "3";
$params['auth_status'] = "0";
}
}
$this->updateCheck($row->id,$params,$row);
//名称title不能与其他课程重复
$check_title = $this->model->where('id','<>',$row["id"])->where('title',$params["title"])->find();
if($check_title){
$this->error("课程名称已存在或被其他机构占用,请更改!");
}
}else{
//新增
$params["add_type"] = '2';
$params["add_id"] = $this->auth->id;
//名称title不能重复
$check_title = $this->model->where('title',$params["title"])->find();
if($check_title){
$this->error("课程名称已存在或被其他机构占用,请更改!");
}
}
//如果是上架,判断是否拥有课时规格,没有则无法上架
if($params["status"]=="1"){
if(!$row)$this->error("新添加的课程请先至少添加一个课时规格后再上架!");
//判断是否拥有课时规格,没有则无法上架
$check_spec = \app\common\model\school\classes\ClassesSpec::where("classes_lib_id",$row->id)->count();
if(!$check_spec)$this->error("新添加的课程请先至少添加一个课时规格后再上架!");
}
}
protected function update_classes($classes_lib_id){
\app\common\model\school\classes\ClassesLib::update_classes($classes_lib_id);
}
/**
* 添加
*
* @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;
}
$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);
}
$this->update_check($params,$row=null);
$result = $this->model->allowField(true)->save($params);
\app\common\model\school\classes\ClassesLib::add_virtual_init($this->model["id"]);
$this->update_classes($this->model["id"]);
if($this->have_auth){
//调用通过事件
$data = ['classes' => $this->model];
\think\Hook::listen('classes_auth_need_after', $data);
}
if($this->success_auth){
//调用通过事件
$data = ['classes' => $this->model];
\think\Hook::listen('classes_auth_success_after', $data);
}
if($this->error_auth){
//调用通过事件
$data = ['classes' => $this->model];
\think\Hook::listen('classes_auth_fail_after', $data);
}
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 $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'));
}
if (false === $this->request->isPost()) {
$this->view->assign('row', $row);
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);
$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);
}
$this->update_check($params,$row);
$result = $row->allowField(true)->save($params);
$this->update_classes($row["id"]);
if($this->have_auth){
//调用通过事件
$data = ['classes' => $row];
\think\Hook::listen('classes_auth_need_after', $data);
}
if($this->success_auth){
//调用通过事件
$data = ['classes' => $row];
\think\Hook::listen('classes_auth_success_after', $data);
}
if($this->error_auth){
//调用通过事件
$data = ['classes' => $row];
\think\Hook::listen('classes_auth_fail_after', $data);
}
// protected $have_auth = false;
Db::commit();
} catch (ValidateException|PDOException|Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if (false === $result) {
$this->error(__('No rows were updated'));
}
$this->success();
}
/**
* 删除
*
* @param $ids
* @return void
* @throws DbException
* @throws DataNotFoundException
* @throws ModelNotFoundException
*/
public function del($ids = null)
{
if (false === $this->request->isPost()) {
$this->error(__("Invalid parameters"));
}
$ids = $ids ?: $this->request->post("ids");
if (empty($ids)) {
$this->error(__('Parameter %s can not be empty', 'ids'));
}
$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();
foreach ($list as $item) {
$this->updateCheck($item->id);
}
$count = 0;
Db::startTrans();
try {
foreach ($list as $item) {
$count += $item->delete();
}
Db::commit();
} catch (PDOException|Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($count) {
$this->success();
}
$this->error(__('No rows were deleted'));
}
}