汪总电商平台
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.
 
 
 
 
 
 

215 lines
8.7 KiB

<?php
namespace app\admin\controller\wanlshop;
use app\common\controller\Backend;
use app\admin\model\wanlshop\active\Goods;
use think\Db;
use think\Exception;
use think\exception\PDOException;
/**
*
*
* @icon fa fa-circle-o
*/
class Active extends Backend
{
/**
* Active模型对象
* @var \app\admin\model\wanlshop\Active
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\wanlshop\Active;
$this->view->assign("statusList", $this->model->getStatusList());
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
public function add(){
if($this->request->isPost()){
$data = $this->request->post();
$maininfo = $data['row'];
$maininfo['createtime'] = date('Y-m-d H:i:s');
$goodIDS = $data['goods_id'];
if(!isset($data['goods_id'])||empty($goodIDS)) $this->error("请选择参与活动的商品");
Db::startTrans($data);
try {
$aid = $this->model->insertGetId($maininfo);
if($aid){
$pnum = $data['pnum'];
$indexshow = $data['indexshow'];
$insetGoods = [];
$item = [];
foreach ($goodIDS as $key => $value){
$item['goods_id'] = $value;
$item['type'] = 1;
$item['link_id'] = $aid;
$item['start_time'] = $maininfo['start_time'];
$item['end_time'] = $maininfo['end_time'];
$item['isindex'] = $indexshow[$value];
$item['pnum'] = $pnum[$value];
$item['bili'] = $maininfo['active_bili'];
$insetGoods[] = $item;
}
if(!Goods::insertAll($insetGoods)) throw new Exception("创建活动失败,商品信息有误");
Db::commit();
}else{
throw new Exception("创建活动失败,活动信息有误");
}
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success("限时活动创建成功");
}
return $this->view->fetch();
}
public function addnew(){
if($this->request->isPost()){
$data = $this->request->post();
$maininfo = $data['row'];
$maininfo['createtime'] = date('Y-m-d H:i:s');
$goodIDS = $data['goods_id'];
if(!isset($data['goods_id'])||empty($goodIDS)) $this->error("请选择参与活动的商品");
Db::startTrans($data);
try {
$aid = $this->model->insertGetId($maininfo);
if($aid){
$pnum = $data['pnum'];
$indexshow = $data['indexshow'];
$insetGoods = [];
$item = [];
foreach ($goodIDS as $key => $value){
$item['goods_id'] = $value;
$item['type'] = 1;
$item['link_id'] = $aid;
$item['start_time'] = $maininfo['start_time'];
$item['end_time'] = $maininfo['end_time'];
$item['isindex'] = $indexshow[$value];
$item['pnum'] = $pnum[$value];
$item['bili'] = $maininfo['active_bili'];
$insetGoods[] = $item;
}
if(!Goods::insertAll($insetGoods)) throw new Exception("创建活动失败,商品信息有误");
Db::commit();
}else{
throw new Exception("创建活动失败,活动信息有误");
}
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success("限时活动创建成功");
}
return $this->view->fetch();
}
public function addtoday(){
if($this->request->isPost()){
$data = $this->request->post();
$maininfo = $data['row'];
$maininfo['createtime'] = date('Y-m-d H:i:s');
$goodIDS = $data['goods_id'];
if(!isset($data['goods_id'])||empty($goodIDS)) $this->error("请选择参与活动的商品");
Db::startTrans($data);
try {
$aid = $this->model->insertGetId($maininfo);
if($aid){
$pnum = $data['pnum'];
$indexshow = $data['indexshow'];
$insetGoods = [];
$item = [];
foreach ($goodIDS as $key => $value){
$item['goods_id'] = $value;
$item['type'] = 1;
$item['link_id'] = $aid;
$item['start_time'] = $maininfo['start_time'];
$item['end_time'] = $maininfo['end_time'];
$item['isindex'] = $indexshow[$value];
$item['pnum'] = $pnum[$value];
$item['bili'] = $maininfo['active_bili'];
$insetGoods[] = $item;
}
if(!Goods::insertAll($insetGoods)) throw new Exception("创建活动失败,商品信息有误");
Db::commit();
}else{
throw new Exception("创建活动失败,活动信息有误");
}
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success("限时活动创建成功");
}
return $this->view->fetch();
}
public function edit($ids=null){
$info = $this->model->get($ids);
if($this->request->post()){
$data = $this->request->post();
$maininfo = $data['row'];
$goodIDS = $data['goods_id'];
if(!isset($data['goods_id'])||empty($goodIDS)) $this->error("请选择参与活动的商品");
Db::startTrans($data);
try {
$info->active_name = $maininfo['active_name'];
$info->updatetime = date('Y-m-d H:i:s');
$info->start_time = $maininfo['start_time'];
$info->end_time = $maininfo['end_time'];
$info->active_bili = $maininfo['active_bili'];
$info->status = 0;
if($info->save() && Goods::where("link_id",$ids)->delete()){
$pnum = $data['pnum'];
$indexshow = $data['indexshow'];
$insetGoods = [];
$item = [];
foreach ($goodIDS as $key => $value){
$item['goods_id'] = $value;
$item['type'] = 1;
$item['link_id'] = $ids;
$item['start_time'] = $maininfo['start_time'];
$item['end_time'] = $maininfo['end_time'];
$item['isindex'] = $indexshow[$value];
$item['pnum'] = $pnum[$value];
$item['bili'] = $maininfo['active_bili'];
$insetGoods[] = $item;
}
if(!Goods::insertAll($insetGoods)) throw new Exception("参加活动的商品有误,请重新编辑信息");
Db::commit();
}else{
throw new Exception("活动信息有误,修改活动失败");
}
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success("限时活动修改成功");
}
$this->assign("row",$info);
return $this->view->fetch();
}
}