You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.9 KiB
131 lines
3.9 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Niucloud-admin 企业快速开发的多应用管理平台
|
|
// +----------------------------------------------------------------------
|
|
// | 官方网址:https://www.niucloud.com
|
|
// +----------------------------------------------------------------------
|
|
// | niucloud团队 版权所有 开源版本可自由商用
|
|
// +----------------------------------------------------------------------
|
|
// | Author: Niucloud Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace addon\hygl\app\service\admin\coupons;
|
|
|
|
use addon\hygl\app\model\coupons\Coupons;
|
|
|
|
use addon\hygl\app\model\user_coupons\UserCoupons;
|
|
use core\base\BaseAdminService;
|
|
use think\facade\Db;
|
|
|
|
|
|
/**
|
|
* 优惠券服务层
|
|
* Class CouponsService
|
|
* @package addon\hygl\app\service\admin\coupons
|
|
*/
|
|
class CouponsService extends BaseAdminService
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->model = new Coupons();
|
|
}
|
|
|
|
/**
|
|
* 获取优惠券列表
|
|
* @param array $where
|
|
* @return array
|
|
*/
|
|
public function getPage(array $where = [])
|
|
{
|
|
// $field = 'id,site_id,name,nominal_value,stock,is_show,create_time,update_time,delete_time';
|
|
$field = '*';
|
|
$order = 'id desc';
|
|
|
|
$search_model = $this->model->where([['site_id', "=", $this->site_id]])->withSearch(["name", "nominal_value", "is_show"], $where)->field($field)->order($order);
|
|
$list = $this->pageQuery($search_model);
|
|
|
|
foreach ($list['data'] as &$v){
|
|
$v['valid_time'] = '';
|
|
if (!empty($v['valid_from']) && !empty($v['valid_until'])) {
|
|
$date_1 = date('Y-m-d', $v['valid_from']);
|
|
$date_2 = date('Y-m-d', $v['valid_until']);
|
|
$v['valid_time'] = "{$date_1} - {$date_2}";
|
|
}
|
|
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* 获取优惠券信息
|
|
* @param int $id
|
|
* @return array
|
|
*/
|
|
public function getInfo(int $id)
|
|
{
|
|
$field = 'id,site_id,name,nominal_value,stock,is_show,is_long_term_valid,valid_from,valid_until,create_time,update_time,delete_time,is_obsolete';
|
|
$info = $this->model->field($field)->where([['id', "=", $id]])->findOrEmpty()->toArray();
|
|
if ($info){
|
|
$info['is_show'] = strval($info['is_show']);//转字符串
|
|
$info['valid_time'] = [];
|
|
if (!$info['is_long_term_valid']){
|
|
$date_1 = date('Y-m-d',$info['valid_from']);
|
|
$date_2 = date('Y-m-d',$info['valid_until']);
|
|
$info['valid_time'] = [$date_1,$date_2];
|
|
}
|
|
}
|
|
return $info;
|
|
}
|
|
|
|
/**
|
|
* 添加优惠券
|
|
* @param array $data
|
|
* @return mixed
|
|
*/
|
|
public function add(array $data)
|
|
{
|
|
$data['site_id'] = $this->site_id;
|
|
$res = $this->model->create($data);
|
|
return $res->id;
|
|
|
|
}
|
|
|
|
/**
|
|
* 优惠券编辑
|
|
* @param int $id
|
|
* @param array $data
|
|
* @return bool
|
|
*/
|
|
public function edit(int $id, array $data)
|
|
{
|
|
Db::startTrans();
|
|
try{
|
|
if (!empty($data['is_obsolete'])){
|
|
//把用户中间表中所有与这个优惠券有关联的数据都改为作废
|
|
UserCoupons::where('coupons_id',$id)->update([
|
|
'is_show'=>0,
|
|
]);
|
|
}
|
|
$this->model->where([['id', '=', $id], ['site_id', '=', $this->site_id]])->update($data);
|
|
Db::commit();
|
|
}catch (\Exception $exception){
|
|
Db::rollback();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 删除优惠券
|
|
* @param int $id
|
|
* @return bool
|
|
*/
|
|
public function del(int $id)
|
|
{
|
|
$model = $this->model->where([['id', '=', $id], ['site_id', '=', $this->site_id]])->find();
|
|
$res = $model->delete();
|
|
return $res;
|
|
}
|
|
|
|
}
|
|
|