| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?php
- namespace app\mall\admin;
- use app\admin\controller\Admin;
- use think\Db;
- use think\Request;
- use think\Validate;
- use app\common\builder\ZBuilder;
- class Collageproduct extends Admin
- {
- public function index(){
- $order = $this->getOrder();
- if($order===''){
- $order='id desc';
- }
- $map = $this->getMap();
- $data_list = Db::name('collage_products')->where($map)->order($order)->paginate();
- $page = $data_list->render();
- return ZBuilder::make('table')
- ->setPageTitle('') // 设置页面标题
- ->setPageTips('') // 设置页面提示信息
- ->hideCheckbox() //隐藏第一列多选框
- ->setTableName('collage_products') // 指定数据表名
- ->addOrder('id,end_at') // 添加排序
- ->addTopSelect('status', '全部拼单状态', ['0'=>'进行中','1'=>'拼单成功','2'=>'拼单失败']) //添加顶部下拉筛选
- ->setSearch(['id' => 'ID', 'title' => '产品名称'], '', '', '搜索') // 设置搜索参数
- ->addColumns([
- ['id', 'ID'],
- ['title', '产品名称'],
- ['collage_price', '拼单价格(元)'],
- ['collage_price', '购买价格(元)'],
- ['require_orders', '拼单要求'],
- ['collage_orders', '当前人数','callback',function($value,$data){
- return db('collage_orders')->where(['collage_product_id'=>$data['id']])->count('id');
- },'__data__'],
- ['end_at', '截止时间','datetime', '未知','Y-m-d H:i'],
- ['status','拼单状态','status', '', ['进行中','拼单成功','拼单失败:danger']],
- ['audit_status', '判定', 'callback',function($value,$data,$menu_id){
- if($data['status']==0){
- if(!if_menu_auth($menu_id)) return '';
- return "<a href='".url('audit',['ids'=>$data['id'],'status'=>'1'])."' class='btn btn-xs btn-default ajax-get confirm'>判定成功</a><a href='".url('audit',['ids'=>$data['id'],'status'=>'2',])."' class='btn btn-xs btn-default ajax-get confirm'>判定失败</a>";
- }else{
- return '已判定';
- }
- },'__data__','244'],
- ['right_button', '操作', 'btn'],
- ]) //添加多列数据
- //->addRightButton('custom',['title'=>'查看详情','href'=>url('look',['id'=>'__ID__'])],true)
- ->addRightButton('edit',['title'=>'编辑产品'])
- ->addRightButton('custom',['title'=>'查看订单','href'=>url('mall/collageorder/index',['collage_product_id'=>'__ID__'])])
- ->addTopButton('add',['title'=>'新建产品'])
- //->addTopButton('custom',['title'=>'判定成功','href'=>url('audit',['status'=>'1']),'icon'=>'fa fa-fw fa-calendar-check-o','class'=>'btn btn-primary ajax-post confirm'])
- //->addTopButton('custom',['title'=>'判定失败','href'=>url('audit',['status'=>'2']),'icon'=>'fa fa-fw fa-calendar-times-o','class'=>'btn btn-primary ajax-post confirm'])
- ->setRowList($data_list) // 设置表格数据
- ->setPages($page) // 设置分页数据
- ->fetch();
- }
- /*public function look($id=''){
- $collage_product=Db::name("collage_products")->where('id',$id)->find();
- if(!$collage_product){
- return $this->error('请求错误');
- }
- // 使用ZBuilder快速创建表单
-
- }*/
- public function audit(){
- $status=input('status','0');
- if($status!='1' && $status!='2'){
- return $this->error('请求错误');
- }
- $now=time();
- $ids = (Request::instance()->isGet()) ? input('ids') : input('post.ids/a');
- $ids=(array)$ids;
- foreach ($ids as $key => $value) {
- if(Db::name('collage_products')->where('id',$value)->value('status')!='0'){
- unset($ids[$key]);
- }
- }
- $rt=Db::name('collage_products')->where('id','in',$ids)->update(['status'=>$status,'updated_at'=>$now]);
- if($rt!==false){
- if($status=='1'){
- //判定订单状态
- Db::name('collage_orders')->where('collage_product_id','in',$ids)->update(['state'=>'1','updated_at'=>$now]);
- //记录订单事件
- $collage_order_ids=Db::name('collage_orders')->field('id')->where('collage_product_id','in',$ids)->select();
- foreach ($collage_order_ids as $key => $value) {
- Db::name('collage_order_events')->insertGetId(['collage_order_id'=>$value['id'],'event'=>'拼单成功','created_at'=>$now,'updated_at'=>$now]);
- }
- $tips='成功';
- }else{
- //判定订单状态
- Db::name('collage_orders')->where('collage_product_id','in',$ids)->update(['state'=>'2','updated_at'=>$now]);
- //记录订单事件
- $collage_order_ids=Db::name('collage_orders')->field('id')->where('collage_product_id','in',$ids)->select();
- foreach ($collage_order_ids as $key => $value) {
- Db::name('collage_order_events')->insertGetId(['collage_order_id'=>$value['id'],'event'=>'拼单失败','created_at'=>$now,'updated_at'=>$now]);
- }
- $tips='失败';
- }
- return $this->success('已判定为拼单'.$tips);
- } else {
- return $this->error('数据库写入错误');
- }
- }
- public function add(){
- //判断是否为post请求
- if (Request::instance()->isPost()) {
- $now=time();
- //获取请求的post数据
- $data=input('post.');
- //数据输入验证
- $validate = new Validate([
- 'title|标题'=> 'require|length:1,20',
- 'weight|重量' => 'require|gt:0|regex:^\d{0,5}(\.\d{1,3})?$',
- 'bland|品牌' => 'require',
- 'require_orders|拼单要求' => 'require|gt:0|regex:^\d{0,10}$',
- 'collage_price|拼单价格' => 'require|gt:0|regex:^\d{0,10}(\.\d{1,2})?$',
- 'price|购买价格' => 'require|gt:0|regex:^\d{0,10}(\.\d{1,2})?$',
- 'end_at|截止时间' => 'require',
- 'admin_attachment_ids|图片' => 'require',
- 'detail|产品详情' => 'require'
- ]);
- if (!$validate->check($data)) {
- return $this->error($validate->getError());
- }
- if ($data['collage_price']>=$data['price']) {
- return $this->error('拼单价格必须小于购买价格');
- }
- $data['end_at']=strtotime($data['end_at']);
- if ($data['end_at']<=$now) {
- return $this->error('截止时间必须在当前时间之后');
- }
- if (count(explode(',',$data['admin_attachment_ids']))>5) {
- return $this->error('图片最多只能上传五张');
- }
- //数据处理
- $insert=array();
- $insert['title']=$data['title'];
- $insert['spec']=$data['spec'];
- $insert['size']=$data['size'];
- $insert['weight']=$data['weight'];
- $insert['bland']=$data['bland'];
- $insert['require_orders']=$data['require_orders'];
- $insert['collage_price']=$data['collage_price'];
- $insert['price']=$data['price'];
- $insert['end_at']=$data['end_at'];
- $insert['admin_attachment_ids']=$data['admin_attachment_ids'];
- $insert['detail']=$data['detail'];
- $insert['created_at']=$now;
- $insert['updated_at']=$now;
- //数据更新
- $insert_id=Db::name("collage_products")->insertGetId($insert);
- //跳转
- if($insert_id>0){
- return $this->success('发布拼单产品成功',url('index'));
- } else {
- return $this->error('发布拼单产品失败');
- }
- }
-
- // 使用ZBuilder快速创建表单
- return ZBuilder::make('form')
- ->setPageTitle('新建拼单产品') // 设置页面标题
- ->setPageTips('请认真编辑相关信息') // 设置页面提示信息
- ->setBtnTitle('submit', '发布') //修改默认按钮标题
- ->addBtn('<button type="reset" class="btn btn-default">重置</button>') //添加额外按钮
- ->addText('title', '标题','必填,限制在20个字以内')
- ->addText('spec', '规格描述','')
- ->addText('size', '尺寸','')
- ->addText('weight', '重量','必填,规则范围0.001~99999.999', '', ['', 'KG'])
- ->addText('bland', '品牌','必填')
- ->addText('require_orders', '拼单要求','必填,即最低订单笔数,规则范围1~9999999999')
- ->addText('collage_price', '拼单价格','必填,若有人参与拼单后则不可修改,需小于购买价格,规则范围0.01~9999999999.99', '', ['<i class="fa fa-fw fa-yen"></i>', ''])
- ->addText('price', '购买价格','必填,若有人直接购买或补差价购买后则不可修改,规则范围0.00~9999999999.99', '', ['<i class="fa fa-fw fa-yen"></i>', ''])
- ->addDatetime('end_at', '截止时间', '必选,需为确定提交之后的时间')
- ->addImages('admin_attachment_ids', '图片','必传,最多上传五张图片,第一张为主图')
- ->addUeditor('detail', '产品详情','')
- //->isAjax(false) //默认为ajax的post提交
- ->fetch();
- }
- public function edit($id=''){
- //判断是否为post请求
- if (Request::instance()->isPost()) {
- $now=time();
- //获取请求的post数据
- $data=input('post.');
- //数据输入验证
- $validate = new Validate([
- 'title|标题'=> 'require|length:1,20',
- 'weight|重量' => 'require|gt:0|regex:^\d{0,5}(\.\d{1,3})?$',
- 'bland|品牌' => 'require',
- 'require_orders|拼单要求' => 'require|gt:0|regex:^\d{0,10}$',
- 'collage_price|拼单价格' => 'require|gt:0|regex:^\d{0,10}(\.\d{1,2})?$',
- 'price|购买价格' => 'require|gt:0|regex:^\d{0,10}(\.\d{1,2})?$',
- 'end_at|截止时间' => 'require',
- 'admin_attachment_ids|图片' => 'require',
- 'detail|产品详情' => 'require'
- ]);
- if (!$validate->check($data)) {
- return $this->error($validate->getError());
- }
- if ($data['collage_price']>=$data['price']) {
- return $this->error('拼单价格必须小于购买价格');
- }
- $data['end_at']=strtotime($data['end_at']);
- if ($data['end_at']<db('collage_products')->where('id',$data['id'])->value('end_at')) {
- return $this->error('截止时间不能小于原来的截止时间');
- }
- if (count(explode(',',$data['admin_attachment_ids']))>5) {
- return $this->error('图片最多只能上传五张');
- }
- //数据处理
- $update=array();
- $update['id']=$data['id'];
- $update['title']=$data['title'];
- $update['spec']=$data['spec'];
- $update['size']=$data['size'];
- $update['weight']=$data['weight'];
- $update['bland']=$data['bland'];
- $update['require_orders']=$data['require_orders'];
- $update['collage_price']=$data['collage_price'];
- $update['price']=$data['price'];
- $update['end_at']=$data['end_at'];
- $update['admin_attachment_ids']=$data['admin_attachment_ids'];
- $update['detail']=$data['detail'];
- $update['updated_at']=$now;
- //数据更新
- $rt=Db::name("collage_products")->update($update);
- //跳转
- if($rt!==false){
- return $this->success('编辑拼单产品成功',url('index'));
- } else {
- return $this->error('编辑拼单产品失败');
- }
- }
-
- // 接收id
- if ($id>0) {
- // 查处数据
- $collage_product=Db::name("collage_products")->where('id',$id)->find();
- if(!$collage_product){
- return $this->error('请求错误');
- }
- $col_order=db('collage_orders')->where('collage_product_id',$id)->where('state','in','1,2,3')->find();
- if($col_order){
- $col_attr='readonly';
- $col_tips='已有人参与拼单,不能修改';
- }else{
- $col_attr='';
- $col_tips='必填,需小于购买价格,规则范围0.01~9999999999.99';
- }
- $buy_order=db('collage_orders')->where('collage_product_id',$id)->where('state','in','4')->find();
- if($buy_order){
- $buy_attr='readonly';
- $buy_tips='已有人购买,不能修改';
- }else{
- $buy_attr='';
- $buy_tips='必填,规则范围0.00~9999999999.99';
- }
- // 使用ZBuilder快速创建表单
- return ZBuilder::make('form')
- ->setPageTitle('编辑拼单产品') // 设置页面标题
- ->setPageTips('请认真编辑相关信息') // 设置页面提示信息
- ->setBtnTitle('submit', '发布') //修改默认按钮标题
- ->addBtn('<button type="reset" class="btn btn-default">重置</button>') //添加额外按钮
- ->addText('title', '标题','必填,限制在20个字以内',$collage_product['title'])
- ->addText('spec', '规格描述','',$collage_product['spec'])
- ->addText('size', '尺寸','',$collage_product['size'])
- ->addText('weight', '重量','必填,规则范围0.001~99999.999',$collage_product['weight'], ['', 'KG'])
- ->addText('bland', '品牌','必填',$collage_product['bland'])
- ->addText('require_orders', '拼单要求','必填,即最低订单笔数,规则范围1~9999999999',$collage_product['require_orders'])
- ->addText('collage_price','拼单价格', $col_tips,$collage_product['collage_price'], ['<i class="fa fa-fw fa-yen"></i>', ''], $col_attr)
- ->addText('price', '购买价格',$buy_tips,$collage_product['price'], ['<i class="fa fa-fw fa-yen"></i>', ''], $buy_attr)
- ->addDatetime('end_at', '截止时间', '必选,需为确定提交之后的时间',date('Y-m-d H:i',$collage_product['end_at']))
- ->addImages('admin_attachment_ids', '图片','必传,最多上传五张图片,第一张为主图',$collage_product['admin_attachment_ids'])
- ->addUeditor('detail', '产品详情','',$collage_product['detail'])
- ->addHidden('id',$collage_product['id'])
- //->isAjax(false) //默认为ajax的post提交
- ->fetch();
- }
- }
- }
|