ShoporderController.php 4.4 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/5
 * Time: 11:20
 */

namespace app\admin\controller;


use cmf\controller\AdminBaseController;
use think\Db;
use think\Validate;

class ShoporderController extends AdminBaseController
{
    public function index()
    {
        $where['a.delete_time'] = ['eq',0];
        $param = $this->request->param();
        $startTime = empty($param['start_time']) ? '' : strtotime($param['start_time']);
        $endTime = empty($param['end_time']) ? '' : (strtotime($param['end_time']));
        if (!empty($startTime) && !empty($endTime)) {
            $where['r.create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['r.create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['r.create_time'] = ['<= time', $endTime];
            }
        }
        $data = Db::name('shoporder')
            ->alias('a')
            ->join('shopgoods r','a.shopgoods_id = r.id')
            ->field('a.*,r.goods_name,r.price')
            ->where($where)
            ->order('a.id desc')
            ->paginate(10);
        $list=$data->items();
        $this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
        $this->assign([
            'data'=>$list,
            'page'=>$data->render(),
        ]);
        return $this->fetch();
    }

    //添加
    public function add()
    {
        if($this->request->isPost()){
            $param=$this->request->param();
            $param['create_time']=time();
            $validate = new Validate([
                'attr_name'=>'require',
                'attr_price'=>'require'
            ]);
            $validate->message([
                'attr_name'=>'属性名称不能为空',
                'attr_price'=>'属性对应价格不能为空'
            ]);
            if (!$validate->check($param)) {
                $this->error($validate->getError());
            }

            Db::name('attr')
                ->insert($param);
            $this->success('添加成功!','index');

        }else{
            return $this->fetch();
        }
    }

    //编辑
    public function edit(){
        $id=$this->request->param('id', 0, 'intval');
        if($this->request->isPost()){
            $param=$this->request->param();
            $validate = new Validate([
                'attr_name'=>'require',
                'attr_price'=>'require'
            ]);
            $validate->message([
                'attr_name'=>'属性名称不能为空',
                'attr_price'=>'属性对应价格不能为空'
            ]);
            if (!$validate->check($param)) {
                $this->error($validate->getError());
            }
            $param['update_time'] = time();
            $data = Db::name('attr')
                ->where('id',$id)
                ->update($param);
            if($data){
                $this->success('更新成功!');
            }else{
                $this->error('sql执行错误');
            }

        }else{
            $data=Db::name('attr')
                ->where('id',$id)
                ->find();
            $data['rgoods'] = Db::name('rgoods')->whereIn('id',$data['rgoods_id'])->field('id,rgoods_name')->select();
            $keyword = $this->request->param('keyword');
            $this->assign('keyword', isset($keyword) ? $keyword : '');
            $this->assign('data',$data);
            return $this->fetch();
        }
    }


    //删除
    public function delete(){
        $param = $this->request->param();
        if (isset($param['id'])) {
            $id = $this->request->param('id', 0, 'intval');
            $resultPortal = Db::name('attr')
                ->where(['id' => $id])
                ->delete();
            if($resultPortal){
                $this->success("删除成功", '');
            }else{
                $this->error("删除失败", '');
            }
        }

        if (isset($param['ids'])) {
            $ids = $this->request->param('ids/a');
            $result = Db::name('attr')
                ->where(['id' => ['in', $ids]])
                ->delete();
            if ($result) {
                $this->success("删除成功", '');
            }else{
                $this->error("删除失败", '');
            }
        }
    }
}