<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\admin\controller\bbfxshop\bargain;

use app\admin\controller\bbfxshop\Base;
use think\Db;


class Category extends Base
{

    public $model = null;
    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\Seckill;
    }
    
    public function all(){
        $where = ["uniacid"=>UNIACID];
        $list = Db::name("bbfx_bargain_goods_category")->where($where)->order("sort desc,id desc")->select();
        __success("操作成功",['list'=>$list]);
    }

    public function getList(){
        $params = $this->param();
        
        $pindex = max(1, intval($params["page"]));
        $psize = intval($params["limit"])?intval($params["limit"]):20;
        
        $where = ["uniacid"=>UNIACID];
        
        $keyword = trim($params['keyword']);
        if($keyword){
            $where['title'] = ['like','%'.$keyword.'%'];
        }
        
    	$list = Db::name("bbfx_bargain_goods_category")->where($where)->order("sort desc,id desc")->limit(($pindex - 1) * $psize,$psize)->select();
      	foreach($list as $key => &$val){
        	
        }
        unset($val);
    	$total = Db::name("bbfx_bargain_goods_category")->where($where)->count();
        __success("操作成功",['total'=>$total,'list'=>$list]);
    }
    
    public function post(){
        $params = $this->param();
        
        $id = intval($params['id']);
        $item = Db::name("bbfx_bargain_goods_category")->where(['id'=>$id])->find();

        if(empty($params['title'])){
            __error("请填写分类标题");
        }
        
    	$data = array(
        	"uniacid"=>UNIACID,
          	"title"=>trim($params['title']),
          	"sort"=>intval($params['sort']),
        );
        
        if(!empty($item)){
            $res = Db::name("bbfx_bargain_goods_category")->where(["id"=>$item['id']])->update($data);
            $data['id'] = $item['id'];
        }
      	else{
            $res = Db::name("bbfx_bargain_goods_category")->insertGetId($data);
            $data['id'] = $res;
        }
        
        if($res){
            \app\admin\model\AdminLog::record("","",$data,"bbfx_bargain_goods_category");
            __success("编辑成功");
        }
        else{
            __success("操作成功");
        }
    }
    
    public function detail(){
        $params = $this->param();
        
        $id = intval($params['id']);
        $item = Db::name("bbfx_bargain_goods_category")->where(['id'=>$id])->find();
        
        if(!empty($item)){
            __success("操作成功",$item);
        }
        else{
            __error("分类不存在");
        }
    }
    
    public function delete(){
        $this->commonDelete('bbfx_bargain_goods_category');
    }
}
