DiverseYouthNightSchool/application/common/model/dyqc/ManystoreShop.php

305 lines
9.0 KiB
PHP
Raw Normal View History

<?php
namespace app\common\model\dyqc;
use app\common\model\BaseModel;
use app\common\model\school\classes\ClassesLib;
use app\common\model\school\classes\Teacher;
use app\common\model\User;
use think\Model;
class ManystoreShop extends BaseModel
{
// 表名
protected $name = 'manystore_shop';
// 自动写入时间戳字段
protected $autoWriteTimestamp = false;
// 定义时间戳字段名
protected $createTime = false;
protected $updateTime = false;
protected $deleteTime = false;
// 追加属性
protected $append = [
'status_text',
'auth_time_text',
'create_time_text',
'update_time_text'
];
/** 专家团队
* @return \think\model\relation\HasMany
*/
public function teachers()
{
return $this->hasMany(Teacher::class,'shop_id')->where("status","1")->order("weigh desc");
}
public function getLogoAttr($value, $data)
{
if (!empty($value)) return cdnurl($value, true);
}
public function getImageAttr($value, $data)
{
if (!empty($value)) return cdnurl($value, true);
}
public function getImagesAttr($value, $data)
{
$imagesArray = [];
if (!empty($value)) {
$imagesArray = explode(',', $value);
foreach ($imagesArray as &$v) {
$v = cdnurl($v, true);
}
return $imagesArray;
}
return $imagesArray;
}
public function getYyzzImagesAttr($value, $data)
{
$imagesArray = [];
if (!empty($value)) {
$imagesArray = explode(',', $value);
foreach ($imagesArray as &$v) {
$v = cdnurl($v, true);
}
return $imagesArray;
}
return $imagesArray;
}
public function getStatusList()
{
return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
}
public function getStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
$list = $this->getStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getAuthTimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['auth_time']) ? $data['auth_time'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
public function getCreateTimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
public function getUpdateTimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
protected function setAuthTimeAttr($value)
{
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
}
protected function setCreateTimeAttr($value)
{
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
}
protected function setUpdateTimeAttr($value)
{
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
}
public function admin()
{
return $this->belongsTo('app\admin\model\Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
/** 机构详情
* @param $id
* @throws \think\exception\DbException
*/
public function detail($id){
$self = $this->get($id,['teachers']);
//一对多隐藏字段方法
// foreach ($self['teachers'] as $k=>$v){
// $v->visible(['id','name','expert_image','expert_content']);
// }
// $self->visible(['id','name','expert_image','expert_content']);
$self["hot"] = $this->getHotClasses($id,$limit=5);
return $self;
}
public function getHotClasses($id,$limit=5){
$data = ClassesLib::where("shop_id",$id)
->where("status",'1')
->limit($limit)
->order("hot desc,weigh desc")
->field("title,headimage,price,hot,weigh")
->select();
return $data;
}
/**申请机构
* @param $type 申请类型 类型:1=个人,2=机构
* @param int $user_id 申请人
* @param $params 申请参数
* @param bool $check
* @param bool $trans
* @return bool
* @throws \Exception
*/
public function apply($type,$user_id=0,$params=[],$check=false,$oper_type='user',$oper_id=0,$trans=false){
if($check){
$user = User::get($user_id);
if(!$user)throw new \Exception("用户不存在");
//已经是机构
$shop = ManystoreShop::where( 'user_id',$user_id)->where("status","1")->find();
if($shop)throw new \Exception("已申请机构,请勿重复申请");
//验证参数
//$type 1=个人,2=机构
if(!in_array($type,['1','2']))throw new \Exception("类型参数错误");
//switch不同类型type做不同验证
//未传手机号则默认用户手机号
if(empty($params['tel'])) $params['tel'] = $user['mobile'];
switch ($type) {
case '1': //个人
$rule = [
'name' => 'require',
'tel' => 'require|number',
// 'desc' => 'require',
'front_idcard_image' => 'require',
'reverse_idcard_image' => 'require',
];
$rule_msg = [
'name.require' => '姓名必须填写',
'tel.require' => '服务电话必须填写',
'tel.number' => '服务电话必须是数字',
// 'desc.require' => '申请备注必须填写',
'front_idcard_image.require' => '身份证正面必须上传',
'reverse_idcard_image.require' => '身份证反面必须上传',
];
break;
case '2': //机构
$rule = [
'name' => 'require',
'tel' => 'require|number',
// 'desc' => 'require',
'front_idcard_image' => 'require',
'reverse_idcard_image' => 'require',
'images' => 'require',
'yyzz_images' => 'require',
];
$rule_msg = [
'name.require' => '机构名称必须填写',
'tel.require' => '服务电话必须填写',
'tel.number' => '服务电话必须是数字',
// 'desc.require' => '申请备注必须填写',
'front_idcard_image.require' => '法人身份证正面必须上传',
'reverse_idcard_image.require' => '法人身份证反面必须上传',
'images.require' => '机构环境照片必须上传',
'yyzz_images.require' => '营业执照照片必须上传',
];
break;
}
self::check($params,$rule,$rule_msg);
}
$params["type"] = $type;
//得到申请单(没有则创建)
$shop = ManystoreShop::where( 'user_id',$user_id)->where("status","1")->find();
//如果存在申请单,则直接更新这个单的状态
//如果不存在则创建一个新单
// $order = self::getHaveCancelOrder($order_no);
//判断逻辑
if($trans){
self::beginTrans();
}
$res = true;
try{
//事务逻辑
// //更新订单取消状态
// $order = Order::updateCancel($order);
// //插入订单取消日志
// if(!$user_id ||$order["user_id"] !=$user_id ){
// OrderLog::log($order['id'],"[员工代操作]课时预约单取消成功,课时已原路退回",$oper_type ?: 'user', $oper_id ?: $order['user_id']);
// }else{
// OrderLog::log($order['id'],"课时预约单取消成功,课时已原路退回",$oper_type ?: 'user', $oper_id ?: $order['user_id']);
// }
//
// //调用订单取消事件
// $data = ['order' => $order];
// \think\Hook::listen('classeshour_order_cancel_after', $data);
// //执行课时数更新
// $res1 = \app\common\model\school\classes\order\Order::statisticsAndUpdateClassesNumber($order['classes_order_id']);
if($trans){
self::commitTrans();
}
}catch (\Exception $e){
if($trans){
self::rollbackTrans();
}
throw new \Exception($e->getMessage());
}
return $res;
}
}