manghe/app/admin/controller/Coupon.php
2025-03-21 19:25:07 +08:00

196 lines
6.3 KiB
PHP
Executable File

<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use \think\Request;
use think\facade\View;
use app\common\model\Coupon as CouponModel;
use app\common\model\GoodsList;
class Coupon extends Base
{
/**
* 优惠券列表
*/
public function index(Request $request)
{
$keyword = trim(input('get.keyword'));
$type = trim(input('get.type'));
if (!empty($keyword)) {
$whe[] = ['title', 'like', '%' . $keyword . '%'];
}
if (!empty($type)) {
$whe[] = ['type', '=', $type];
}
$whe[] = ['status', '=', 0];
$field = "*";
$order = "addtime desc";
$data = CouponModel::getList($whe, $field, $order, $this->page);
View::assign('list', $data['list']);
View::assign('count', $data['count']);
View::assign('page', $data['page']);
return View::fetch("Coupon/index");
}
/**
* 添加商品
*/
public function add(Request $request)
{
if (!$request->isPost()) {
return View::fetch("Coupon/add");
} else {
$data = input('post.');
// $validate = validate('Goods');
// try {
// $validate->scene('add')->check($data);
// } catch (\Exception $e) {
// return $this->renderError($e->getError());
// }
// if(isset($data['arrive_time'])){
// $data['arrive_time'] = strtotime($data['arrive_time']);
// }
$data['addtime'] = time();
$dd = CouponModel::insertGetId($data);
if ($dd) {
return $this->renderSuccess("添加成功");
} else {
return $this->renderError("网络繁忙,请稍后");
}
}
}
/**
* 编辑商品
*/
public function edit(Request $request)
{
if (!$request->isPost()) {
$id = $request->param('id');
$goods = CouponModel::getInfo(['id' => $id], '*');
View::assign('data', $goods);
return View::fetch("Coupon/edit");
} else {
$data = input('post.');
CouponModel::startTrans();
// $validate = validate('Goods');
// try {
// $validate->scene('add')->check($data);
// } catch (\Exception $e) {
// return $this->renderError($e->getError());
// }
// $goods = CouponModel::getInfo(['id'=>$data['id']]);
// $gl = 1;
// if($data['stock'] < $goods['stock']){
// //减少套数
// $whe = [];
// $whe[] = ['goods_id','=',$data['id']];
// $whe[] = ['num','>',$data['stock']];
// $gl = GoodsList::where($whe)->update(['status'=>'3']);
// }
// if($data['stock'] > $goods['stock']){
// //增加套数
// $whe = [];
// $whe[] = ['goods_id','=',$data['id']];
// $whe[] = ['num','=','1'];
// $whe[] = ['status','=','1'];
// $goodslist = GoodsList::getAllList($whe,'*');
// for($i=$goods['stock']+1;$i<=$data['stock'];$i++){
// $arr = [];
// foreach($goodslist as $k=>$v){
// $arr[$k]['title'] = $v['title'];
// $arr[$k]['shang_id'] = $v['shang_id'];
// $arr[$k]['price'] = $v['price'];
// $arr[$k]['money'] = $v['money'];
// $arr[$k]['stock'] = $v['stock'];
// $arr[$k]['imgurl'] = $v['imgurl'];
// $arr[$k]['num'] = $i;
// $arr[$k]['goods_id'] = $v['goods_id'];
// $arr[$k]['addtime'] = $v['addtime'];
// $arr[$k]['yc_num'] = $v['yc_num'];
// }
// $gl = GoodsList::insertAll($arr);
// if(!$gl){
// GoodsModel::rollback();
// return $this->renderError("网络繁忙,请稍后");
// }
// }
// }
// if(isset($data['arrive_time'])){
// $data['arrive_time'] = strtotime($data['arrive_time']);
// }
$id = $data['id'];
unset($data['id']);
$dd = CouponModel::where(['id' => $id])->update($data);
if ($dd) {
CouponModel::commit();
return $this->renderSuccess("编辑成功");
} else {
CouponModel::rollback();
return $this->renderError("网络繁忙,请稍后");
}
}
}
// public function edit(Request $request){
// if(!$request->isPost()){
// $id = $request->param('id');
// $goods = GoodsModel::getInfo(['id'=>$id],'*');
// View::assign('data',$goods);
// return View::fetch("Goods/edit");
// }else{
// $data = input('post.');
// $validate = validate('Goods');
// try {
// $validate->scene('add')->check($data);
// } catch (\Exception $e) {
// return $this->renderError($e->getError());
// }
// $dd=GoodsModel::where(['id'=>$data['id']])->update($data);
// if($dd){
// return $this->renderSuccess("编辑成功");
// }else{
// return $this->renderError("网络繁忙,请稍后");
// }
// }
// }
//上、下架商品
public function cao(Request $request)
{
$id = $request->post('id/d');
$status = $request->post('type/d');
if (!in_array($status, array(1, 2, 4))) {
return $this->renderError("数据错误");
}
$data = CouponModel::where(['id' => $id])->field('id')->find();
if (!$data) {
return $this->renderError("数据不存在");
}
$result = CouponModel::where(['id' => $id])->update(["status" => $status]);
if ($result) {
return $this->renderSuccess("操作成功");
} else {
return $this->renderError("网络繁忙,请稍后");
}
}
}