DiverseYouthNightSchool/application/common/model/school/classes/ClassesLib.php

354 lines
10 KiB
PHP
Raw Normal View History

<?php
namespace app\common\model\school\classes;
use app\common\model\dyqc\ManystoreShop;
use app\common\model\User;
use think\Model;
use traits\model\SoftDelete;
class ClassesLib extends Model
{
use SoftDelete;
// 表名
protected $name = 'school_classes_lib';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'integer';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = 'deletetime';
// 追加属性
protected $append = [
'add_type_text',
'type_text',
'address_type_text',
'status_text',
'auth_status_text',
'auth_time_text',
'recommend_text',
'hot_text',
'new_text',
'selfhot_text'
];
protected static function init()
{
self::afterInsert(function ($row) {
if (!$row['weigh']) {
$pk = $row->getPk();
$row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
}
});
}
public function getAddTypeList()
{
return ['1' => __('Add_type 1'), '2' => __('Add_type 2')];
}
public function getTypeList()
{
return ['out' => __('Type out'), 'in' => __('Type in')];
}
public function getAddressTypeList()
{
return ['1' => __('Address_type 1'), '2' => __('Address_type 2')];
}
public function getStatusList()
{
return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
}
public function getAuthStatusList()
{
return ['0' => __('Auth_status 0'), '1' => __('Auth_status 1'), '2' => __('Auth_status 2')];
}
public function getRecommendList()
{
return ['0' => __('Recommend 0'), '1' => __('Recommend 1')];
}
public function getHotList()
{
return ['0' => __('Hot 0'), '1' => __('Hot 1')];
}
public function getNewList()
{
return ['0' => __('New 0'), '1' => __('New 1')];
}
public function getSelfhotList()
{
return ['0' => __('Selfhot 0'), '1' => __('Selfhot 1')];
}
public function getAddTypeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['add_type']) ? $data['add_type'] : '');
$list = $this->getAddTypeList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getTypeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
$list = $this->getTypeList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getAddressTypeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['address_type']) ? $data['address_type'] : '');
$list = $this->getAddressTypeList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
$list = $this->getStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getAuthStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['auth_status']) ? $data['auth_status'] : '');
$list = $this->getAuthStatusList();
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 getRecommendTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['recommend']) ? $data['recommend'] : '');
$list = $this->getRecommendList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getHotTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['hot']) ? $data['hot'] : '');
$list = $this->getHotList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getNewTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['new']) ? $data['new'] : '');
$list = $this->getNewList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getSelfhotTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['selfhot']) ? $data['selfhot'] : '');
$list = $this->getSelfhotList();
return isset($list[$value]) ? $list[$value] : '';
}
protected function setAuthTimeAttr($value)
{
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
}
public function getHeadimageAttr($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 manystore()
{
return $this->belongsTo('app\admin\model\Manystore', 'manystore_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function shop()
{
return $this->belongsTo(ManystoreShop::class, 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function user()
{
return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function admin()
{
return $this->belongsTo('app\admin\model\Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function teacher()
{
return $this->belongsTo('app\common\model\school\classes\Teacher', 'teacher_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
/** 课程详情
* @param $id
* @return ClassesLib|null
* @throws \think\exception\DbException
*/
public function detail($id){
$self = $this->get($id,['shop','teacher']);
//下架判断
if($self['status'] != '1'){
$this->error("该课程已下架");
}
//参与人数 = 虚拟人数 + 平台人数
return $self;
}
/**课程参与人员信息
* @param $id
*
*/
public function virtualParticipants($id){
//虚拟用户生成数量
$v_num = 10;
$unpaid_user_data = $paid_user_data = [];
//参与者头像
//先从订单里取出最新的10个未支付和使用中的用户不足的用虚拟用户填充
$user_ids = \app\common\model\school\classes\order\Order::where("classes_lib_id",$id)->where("status","in",["0",'3'])->order("createtime desc")->limit($v_num)->column("createtime","user_id");
if($user_ids){
$users = User::where("id","in",array_keys($user_ids))->select();
foreach ($users as $user){
$unpaid_user_data[] = [
"nickname"=>$user['nickname'],
"head_image"=>$user['avatar'],
"classes_lib_id"=>$id,
"time"=>$user_ids[$user['id']],
"jointype"=>'1',
"havetype"=>'0'
];
}
}
//计算需要生成的虚拟用户数量
$v_num = $v_num - count($user_ids);
if($v_num<0)$v_num =0;
if($v_num){
$unpaid_user = VirtualUser::where("classes_lib_id",$id)->where("jointype",'1')->order("time desc")->limit($v_num)->select();
$unpaid_user_data = array_merge($unpaid_user_data,$unpaid_user);
}
//再从订单里取出最新的10个使用中已完成 和 已退款 的用户,不足的用虚拟用户填充
$user_ids = \app\common\model\school\classes\order\Order::where("classes_lib_id",$id)->where("status","in",["3",'6','9'])->order("createtime desc")->limit($v_num)->column("createtime","user_id");
if($user_ids){
$users = User::where("id","in",array_keys($user_ids))->select();
foreach ($users as $user){
$paid_user_data[] = [
"nickname"=>$user['nickname'],
"head_image"=>$user['avatar'],
"classes_lib_id"=>$id,
"time"=>$user_ids[$user['id']],
"jointype"=>'0',
"havetype"=>'1'
];
}
}
//计算需要生成的虚拟用户数量
$v_num = 10;
$v_num = $v_num - count($user_ids);
if($v_num<0)$v_num =0;
if($v_num){
$unpaid_user = VirtualUser::where("classes_lib_id",$id)->where("havetype",'1')->order("time desc")->limit($v_num)->select();
$paid_user_data = array_merge($paid_user_data,$unpaid_user);
}
//进行昵称加*和按时间重新排序
array_multisort(array_column($unpaid_user_data, 'time'), SORT_ASC, $unpaid_user_data);
array_multisort(array_column($paid_user_data, 'time'), SORT_ASC, $paid_user_data);
foreach ($unpaid_user_data as &$userss){
//substr_replace($username, '****', 3, 4);
$userss["nickname"] = $this->nickname_filter($userss["nickname"]);
}
foreach ($paid_user_data as &$usersss){
//substr_replace($username, '****', 3, 4);
$usersss["nickname"] = $this->nickname_filter($usersss["nickname"]);
}
return compact("unpaid_user_data","paid_user_data");
}
public function nickname_filter($nickname){
$nickname_len = mb_strlen($nickname);
if($nickname_len<=2){
return $nickname;
}else{
return mb_substr($nickname,0,1).str_repeat("*",$nickname_len-2).mb_substr($nickname,-1);
}
}
/**课程规格
* @param $id
*
*/
public function spec($id){
//课程规格
$spec_data = ClassesSpec::where("classes_lib_id",$id)->where("status",'1')->order("weigh desc")->select();
return $spec_data;
}
}