Files

324 lines
8.8 KiB
PHP

<?php
namespace app\admin\controller\app\coupon;
use think\Db;
use app\common\controller\Backend;
use app\admin\model\app\coupon\Coupon as CouponModel;
use app\common\traits\app\CouponSend;
use app\admin\model\app\coupon\CouponGoods;
/**
* 优惠券
*/
class Coupon extends Backend
{
public function _initialize()
{
parent::_initialize();
$this->model = new CouponModel;
$this->couponGoodsModel = new CouponGoods;
}
/**
* 优惠券列表
*
* @return \think\Response
*/
public function index()
{
//当前是否为关联查询
$this->relationSearch = false;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
//如果发送的来源是Selectpage,则转发到Selectpage
$status = input('status');
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$extendWhere = [
'deletetime'=>NULL
];
switch ($status){
case 2:
case 3:
$extendWhere['status'] = $status;
break;
case 1:
$extendWhere['status'] = 1;
$extendWhere['use_start_time'] = ['<=',time()];
$extendWhere['use_end_time'] = ['>=',time()];
break;
case 4:
$extendWhere['status'] = 1;
$extendWhere['use_start_time'] = ['>',time()];
break;
case 5:
$extendWhere['status'] = 1;
$extendWhere['use_end_time'] = ['<',time()];
break;
}
$list = $this->model
->where($where)
->where($extendWhere)
->order($sort, $order)
->paginate($limit);
foreach ($list as $row) {
// $row->ticket_count = $this->ticketModel->getTicketCount($row->id);
// $row->apply_total_data = $this->userTicketModel->getApplyTotalData($row->id);
}
$result = array("total" => $list->total(), "rows" => $list->items(),'attr'=>[
'typeList'=>$this->model->getTypeList(),
'statusList'=>$this->model->getStatusList(),
]);
return $this->success("获取成功",$result);
}
/**
* 添加优惠券
*
* @return \think\Response
*/
public function add()
{
$params = $this->request->only([
'name','type','goods','max_amount_status','enough_status','discount','use_time','use_scope','amount','max_amount','enough','stock','limit_num','limit_status','use_time_type','use_start_time','use_end_time','start_days','days','description','recommend_goods_status','stock_status','share_status','public_status','double_discount_status'
]);
$params['uniacid'] = UNIACID;
$goods = $params['goods'];
unset($params['goods']);
$params['use_start_time'] = strtotime($params['use_time'][0]) ?? 0;
$params['use_end_time'] = strtotime($params['use_time'][1]) ?? 0;
unset($params['use_time']);
$params['createtime'] = $params['updatetime'] = time();
$this->validateForm($params);
$couponId = $this->model->insertGetId($params);
$this->couponGoodsModel->setGoods($couponId,$goods);
return $this->success("保存成功");
}
/**
* 参数验证
* @param $params
* @return void
*/
public function validateForm($params){
if($params['use_time_type'] == 'range'){
if($params['use_start_time'] == 0 || $params['use_end_time'] == 0){
return $this->error("请完善使用时间");
}
}else{
if($params['days']<=0){
return $this->error("有效期请设置大于 0 天");
}
}
if($params['type'] == 'discount'){
if($params['discount'] <= 0 || $params['discount'] > 9.99 ){
return $this->error("请填写有效的消费折扣");
}
}
if($params['type'] == 'reduce'){
if($params['amount'] <= 0){
return $this->error("请填写有效的满减金额");
}
}
}
/**
* 优惠券详情
*
* @param $id
* @return \think\Response
*/
public function detail($id)
{
$coupon = $this->model->where('id', $id)->find();
if (!$coupon) {
$this->error(__('No Results were found'));
}
$coupon->use_time = [$coupon->use_start_time,$coupon->use_end_time];
$coupon->max_amount = floatval($coupon->max_amount);
$coupon->discount = floatval($coupon->discount);
$coupon->amount = floatval($coupon->amount);
$coupon->enough = floatval($coupon->enough);
$coupon->goods = $coupon->goods_value;
return $this->success("获取成功",$coupon);
}
/**
* 修改优惠券
*
* @return \think\Response
*/
public function edit($id = null)
{
$params = $this->request->only(['name','goods','max_amount_status','enough_status','discount','type','use_time','use_scope','amount','max_amount','enough','stock','limit_num','limit_status','use_time_type','use_start_time','use_end_time','start_days','days','description','recommend_goods_status','stock_status','share_status','public_status','double_discount_status']);
// $this->svalidateForm($params);
$goods = $params['goods'];
unset($params['goods']);
$params['use_start_time'] = strtotime($params['use_time'][0]) ?? 0;
$params['use_end_time'] = strtotime($params['use_time'][1]) ?? 0;
unset($params['use_time']);
$params['updatetime'] = time();
$coupon = $this->model->where('id', $id)->find();
if (!$coupon) {
$this->error(__('No Results were found'));
}
$this->validateForm($params);
$this->couponGoodsModel->setGoods($id,$goods);
$coupon->save($params);
return $this->success("保存成功");
}
/**
* 删除优惠券
* @param string $id 要删除的优惠券
* @return void
*/
public function delete($ids)
{
if (empty($ids)) {
$this->error(__('Parameter %s can not be empty', 'id'));
}
if(is_string($ids)){
$ids = explode(',', $ids);;
}
$list = $this->model->where('id', 'in', $ids)->select();
$result = Db::transaction(function () use ($list) {
$count = 0;
foreach ($list as $item) {
$item->deletetime = time();
$count += $item->save();
}
return $count;
});
if ($result) {
$this->success('删除成功', null, $result);
} else {
$this->error(__('No rows were deleted'));
}
}
/**
* 优惠券列表
*/
public function select()
{
if (!$this->request->isAjax()) {
return $this->view->fetch();
}
$type = $this->request->param('type', 'page');
$coupons = $this->model->sheepFilter();
if ($type == 'select') {
// 普通结果
$coupons = $coupons->select();
} elseif ($type == 'find') {
$coupons = $coupons->find();
} else {
// 分页结果
$coupons = $coupons->paginate($this->request->param('list_rows', 10));
}
$this->success('获取成功', null, $coupons);
}
/**
* 切换状态
* @param $ids
* @param $status
* @return void
*/
public function status($ids=null,$status=1){
if (false === $this->request->isPost()) {
$this->error(__("Invalid parameters"));
}
$ids = $ids ?: $this->request->post("ids");
if (empty($ids)) {
$this->error(__('Parameter %s can not be empty', 'ids'));
}
$status = $status ?: $this->request->post("status");
$pk = $this->model->getPk();
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
$this->model->where($this->dataLimitField, 'in', $adminIds);
}
$list = $this->model->where($pk, 'in', $ids)->select();
$count = 0;
Db::startTrans();
try {
foreach ($list as $item) {
$row = [
'status'=>$status
];
$this->model->where([
'id'=>$item['id']
])->update($row);
$count++;
}
Db::commit();
} catch (PDOException|Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($count) {
$this->success("操作成功");
}
$this->error(__('未操作任何数据'));
}
}