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

648 lines
19 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 app\manystore\model\Manystore;
use app\manystore\model\ManystoreAuthGroup;
use app\manystore\model\ManystoreAuthGroupAccess;
use fast\Random;
use think\Cache;
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 = [
'type_text',
'status_text',
'auth_time_text',
'create_time_text',
'update_time_text'
];
public function getTypeList()
{
return ['1' => __('Type 1'), '2' => __('Type 2')];
}
public function getTypeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
$list = $this->getTypeList();
return isset($list[$value]) ? $list[$value] : '';
}
// 拼音对象
protected static $pinyin = null;
protected static function init()
{
self::$pinyin = new \Overtrue\Pinyin\Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
}
/** 专家团队
* @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);
}
public function user()
{
return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function checkFull($id){
$self = $this->get($id,['teachers']);
// var_dump($self->toArray());
if(empty($self["address_city"])
|| empty($self["province"])
|| empty($self["city"])
|| empty($self["district"])
|| empty($self["longitude"])
|| empty($self["latitude"])
|| empty($self["name"])
|| empty($self["image"])
|| empty($self["images"])
|| empty($self["content"])
|| empty($self["tel"])
) return false;
return true;
}
/** 机构详情
* @param $id
* @throws \think\exception\DbException
*/
public function detail($id){
$self = $this->get($id,['teachers']);
if(!$this->checkFull($id))throw new \Exception("当前机构未完善信息,暂时无法展示!");
//一对多隐藏字段方法
// 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,$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;
// $order = self::getHaveCancelOrder($order_no);
//判断逻辑
if($trans){
self::beginTrans();
}
$res = true;
try{
//事务逻辑
//得到申请单(没有则创建)
$shop = ManystoreShop::where( 'user_id',$user_id)->find();
//如果存在申请单,则直接更新这个单的状态
//如果不存在则创建一个新单
if(!$shop){
//创建申请单
$shop = $this->creatShop($type,$user_id,$params);
}
//更新申请单状态为审核中
$shop['status']= '0';
//清空审核时间
$shop['reason']= '';
$shop['auth_time']= 0;
$shop["admin_id"] = 0;
$shop->save();
$store = Manystore::where("shop_id",$shop['id'])->find();
$store["status"] = 'hidden';
$store->save();
//调用订单事件
$data = ['shop' => $shop];
\think\Hook::listen('shop_apply_after', $data);
if($trans){
self::commitTrans();
}
}catch (\Exception $e){
if($trans){
self::rollbackTrans();
}
throw new \Exception($e->getMessage());
}
return $res;
}
/** 机构后台默认密码获取
* @param $type
* @param $user_id
* @param $params
*
* @throws \think\exception\DbException
*/
public function getDefaultPassword($type,$user_id,$params){
$user = User::get($user_id);
$defaultPassword = null;
switch ($type) {
case '1': //个人
//手机号 + 个人id生成的code
$defaultPassword = $user["mobile"].en_code($user['id']);
// $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': //机构
$defaultPassword = $user["mobile"].en_code($user['id']);
// $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;
}
return $defaultPassword;
}
public function creatShop($type,$user_id,$params){
$params["user_id"] = $user_id;
//商家附表
$shop = (new self);
$shop_info = $shop->allowField(true)->save($params);
if($shop_info === false){
throw new \Exception($shop->getError());
}
//账号主表
$manystore_params = [
"nickname" => $params["name"],
"user_id"=>$user_id,
];
$manystore_params["username"] = $params["tel"] ?: self::$pinyin->permalink($manystore_params["nickname"]);
$manystore_params["email"] = $manystore_params["username"] . "@xx.com";
$manystore_params['password'] = self::getDefaultPassword($type,$user_id,$params);
$manystore_params['shop_id'] = $shop->id;
$manystore_params['salt'] = Random::alnum();
$manystore_params['password'] = md5(md5($manystore_params['password']) . $manystore_params['salt']);
$manystore_params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
$manystore_params['is_main'] = 1;
$manystore_params['status'] = "hidden";
$model = new Manystore();
$result = $model->allowField(true)->save($manystore_params);
if ($result === false) {
throw new \Exception($model->getError());
}
$manystoreAuthGroupModel = new ManystoreAuthGroup();
$group = [];
switch ($type) {
case '1': //个人
$group['name'] = '个人认证类型';
$group['rules'] = '*';
break;
case '2': //机构
$group['name'] = '机构认证类型';
$group['rules'] = '*';
break;
}
$group['shop_id'] = $shop->id;
$group['createtime'] = time();
$group['updatetime'] = time();
$group_id = $manystoreAuthGroupModel->insertGetId($group);
if(!$group_id){
$this->error('添加失败');
}
$manystoreAuthGroupAccessModel = new ManystoreAuthGroupAccess();
$group_access = [];
$group_access['uid'] = $model->id;
$group_access['group_id'] = $group_id;
$manystoreAuthGroupAccessModel->insert($group_access);
//调用订单事件
$data = ['shop' => $shop];
\think\Hook::listen('shop_create_after', $data);
return $shop;
}
/** 机耕申请状态查询
*
*/
public static function getAuthInfo($user_id){
$auth_status = '-1'; //-1=未申请,0=待审核,1=审核通过,2=审核失败
$shop_id = 0;
$type = '1';
$reason =""; //失败原因
//得到申请单
$apply_info = self::where("user_id",$user_id)->where("status","1")->find();
if(!$apply_info)$apply_info = self::where("user_id",$user_id)->find();
//不存在说明未申请,直接返回
if(!$apply_info){
return compact('auth_status','shop_id','reason','apply_info',"type");
}
$type = $apply_info['type'];
//从申请单取到申请状态
$auth_status = $apply_info['status'];
//如果是审核失败,取失败原因
if($auth_status == '2'){
$reason = $apply_info['reason'];
}
//如果是申请成功取店铺id
if($auth_status == '1'){
$shop_id = $apply_info['id'];
}
return compact('auth_status','shop_id','reason','apply_info',"type");
}
/**
* 获取所有课程列表
*/
public static function getVaildList($params) {
extract($params);
$a = (new self)->getWithAlisaName().'.';
// 查询自提点
if(isset($status) && in_array($status, [1,2,0])){
$selfetch = self::with(['user']);
}else{
$selfetch = self::with(['user'])->where($a.'status', '1')->where("{$a}auth_status",1);
}
$order = $order?? 'normal';
$per_page = $limit ?? 10;
$field = "{$a}id,{$a}name,{$a}user_id,{$a}logo,{$a}image,{$a}images,{$a}address_city,{$a}city,{$a}province,{$a}district,{$a}address,{$a}address_detail,{$a}longitude,{$a}latitude,{$a}type,{$a}tel,{$a}status,{$a}create_time,{$a}update_time,{$a}weigh";
//得到距离
if (isset($latitude) && isset($longitude) && $latitude && $longitude) {
$field .= ', '.getDistanceBuilder($latitude, $longitude);
}else{
$field .= ', 0 as distance';
}
//得到每个
$selfetch = $selfetch->field($field);
if (isset($keyword) && $keyword) {
$selfetch = $selfetch->where("{$a}name|{$a}address|{$a}address_detail|{$a}address_city", 'like', '%' . $keyword . '%');
}
if (isset($user_id) && $user_id) {
$selfetch = $selfetch->where("{$a}user_id", 'in', ''.$user_id);
}
if (isset($my) && $my && isset($my_user_id) && $my_user_id) {
$selfetch = $selfetch->where("{$a}user_id", 'in', ''.$my_user_id);
}
if (isset($id) && $id) {
$selfetch = $selfetch->where("{$a}id", 'in', ''.$id);
}
if (isset($teacher_id) && $teacher_id) {
$teacher = Teacher::where("id",$teacher_id)->find();
if($teacher){
$selfetch = $selfetch->where("{$a}id", 'in', ''.$teacher["shop_id"]);
}
}
if (isset($type) && $type) {
$selfetch = $selfetch->where("{$a}type", 'in', ''.$type);
}
if (isset($status) && $status) {
$selfetch = $selfetch->where("{$a}status", 'in', ''.$status);
}
//区域搜索
if (isset($province) && $province) {
$selfetch = $selfetch->where("{$a}province", 'in', ''.$province);
}
if (isset($city) && $city) {
$selfetch = $selfetch->where("{$a}city", 'in', ''.$city);
}
if (isset($district) && $district) {
$selfetch = $selfetch->where("{$a}district", 'in', ''.$district);
}
//排序
switch ($order) {
case "normal": //综合排序(推薦優先)
$selfetch = $selfetch->order("{$a}weigh desc,{$a}id desc");
break;
case "distance": //距离优先 权重
$selfetch = $selfetch->order("distance asc,{$a}weigh desc,{$a}id desc");
break;
default:
throw new \Exception("不支持的排序类型");
}
if(isset($nearby) && $nearby) {
$selfetch = $selfetch->having("distance <= {$nearby}");
}
$selfetch = $selfetch->paginate($per_page);
//额外附加数据
// foreach ($selfetch as $row) { //迭代器魔术方法遍历,填值自动引用传值
// //设置是否已收藏
// $row->is_collect = in_array($row->id,$collect_classes_lib_ids) ? 1 : 0;
// }
return $selfetch;
}
}