Files

179 lines
5.0 KiB
PHP

<?php
namespace app\admin\model\order;
use think\Model;
use app\admin\model\order\Item;
class Order extends Model
{
// 表名
protected $name = 'order';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'integer';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = false;
protected $deleteTime = false;
// 追加属性
protected $append = [
// 'pay_time_text',
// 'pay_type_text',
// 'source_text',
// 'send_type_text',
// 'status_text'
];
/**
* 获取销量
* @params $goodsType course\vipcard\activity\exercises\
* @param $goodsId 商品ID
* @return void
*/
public function getSales($goodsType,$goodsId){
return \app\admin\model\order\Item::alias('item')
->join('order','item.order_no=order.order_no','LEFT')
->where([
'goods_type'=>$goodsType,
'item_id'=>$goodsId,
'order.status'=>['in',\app\common\constant\order\Status::getSuccessOrderStatus()]
])->sum('count');
}
public function getPayTypeList()
{
return ['balance' => __('Balance'), 'alipay' => __('Alipay'), 'wxpay' => __('Wxpay')];
}
public function getSourceList()
{
return ['h5' => __('source.H5'),'channels' => __('source.channels'), 'wxOfficialAccount' => __('source.wxOfficialAccount'), 'wxMiniProgram' => __('source.wxMiniProgram'), 'dyMiniProgram' => __('source.dyMiniProgram')];
}
public function getSendTypeList()
{
return ['express' => __('Express'), 'noexpress' => __('Noexpress'), 'offline' => __('Offline')];
}
public function getVirtualPayList()
{
return ['0' => __('否'), '1' => __('是')];
}
public function getTypeList()
{
$list = ['goods' => __('order_type.goods'), 'score' => __('order_type.score'), 'vipcard' => __('order_type.vipcard'), 'exercises' => __('order_type.exercises'), 'physical' => __('order_type.physical'), 'live_gift' => __('order_type.live_gift')];
// 组合商品订单类型仅在 composite 插件已安装时显示
if (\app\admin\library\project\App::isInstall('composite')) {
$list['composite'] = __('order_type.composite');
}
return $list;
}
public function getPayTimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
public function getPayTypeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['pay_type']) ? $data['pay_type'] : '');
$list = $this->getPayTypeList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getSourceTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['source']) ? $data['source'] : '');
$list = $this->getSourceList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getSendTypeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['send_type']) ? $data['send_type'] : '');
$list = $this->getSendTypeList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getStatusList()
{
return ['close' => __('Close'), 'unpaid' => __('Unpaid'), 'unsend' => __('Unsend'), 'unreceive' => __('Unreceive'), 'success' => __('Success'), 'cancel' => __('Cancel'), 'after_sale' => __('After_sale'), 'refund' => __('Refund')];
}
public function getStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
$list = $this->getStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
protected function setPayTimeAttr($value)
{
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
}
public function user()
{
return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function item()
{
return $this->hasMany('app\admin\model\order\Item', 'order_no', 'order_no');
}
public function log()
{
return $this->hasMany('app\common\model\order\Log','order_no', 'order_no');
}
/**
* 获取交易统计
* @return void
*/
public function getTransactionStatistic($userId){
$data = self::where([
'user_id'=>$userId,
'status'=>['in',['success']]
])->order('createtime','desc')->field(['sum(real_pay_price) as pay_price','count(*) as pay_count','createtime as last_pay_time'])->find();
if(!$data){
$data = [
'pay_price'=>0,
'pay_count'=>0,
'last_pay_time'=>0
];
}
$data = $data->toArray();
foreach ($data as &$item){
if(!$item){
$item = 0;
}
}
return $data;
}
}