<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
class ProductController extends Controller{
public function productadd(){
if(IS_POST){
$_POST['on_time'] = NOW_TIME;
if($_POST['goods_sn'] == ""){
$_POST['goods_sn'] = 'TP'.mt_rand(100000,999999);
}
if(!D('product')->create($_POST)){
exit(D('product')->getError());
}else{
$product_info = D('product')->add($_POST);
if($product_info){
$this->success('添加成功',U('Admin/product/productlist'));
}else{
$this->error('添加失败');
}
}
print_r($_POST);
}
$brandlist=D('brand')->order(['id'=>'asc'])->select();
$this->assign('brand',$brandlist);
$parent_id_0=M('product_category')->where('parent_id='.'0')->select();
$this->assign('parent_id_0',$parent_id_0);
$this->display();
}
public function productlist(){
$where = '1=1';
I('brand_id') && $where = "$where and brand_id =".I('brand_id');
I('cat_id') && $where = "$where and cat_id = ".I('cat_id',0) ;
(I('is_on_sale') !== '') && $where = "$where and is_on_sale = ".I('is_on_sale') ;
I('intro') && $where = "$where and ".I('intro')."=1";
$keyword = I('keyword')? trim(I('keyword')) : '';
if($keyword){
$where = "$where and goods_name like '%".$keyword."%'";
}
$count = D('product')->where($where)->count();
$page = new Page($count,25);
$show = $page->show();
$proinfo = D('product')
->where($where)
->order('sort',desc)
->limit($page->firstRow.','.$page->listRows)
->select();
$catlist = D('product_category')->getField('id,name');
$brand = D('brand')->select();
foreach($brand as $key => $value){
$str = $value['name']."<br>";
}
$this->assign('page',$show);
$this->assign('count',$count);
$this->assign('proinfo',$proinfo);
$this->assign('cat',$catlist);
$this->assign('brand',$brand);
$this->display();
}
public function productdel(){
$productinfo = D('product')->where('goods_id='.I('goods_id'))->delete();
if($productinfo){
$this->ajaxReturn(['status'=>1,'msg'=>'删除成功']);
}else{
$this->araxReturn(['status'=>0,'msg'=>'删除失败']);
}
}
public function stock_list(){
$where = '1=1';
$count = D('stock_log')->order('ctime',desc)->count();
$Page = new Page($count,25);
$show = $Page->show();
$stock_list = D('stock_log')->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
$this->assign('stock_list',$stock_list);
$this->assign('count',$count);
$this->assign('page',$show);
$this->display();
}
public function selectproduct(){
$count = M('product')->count();
$Page = new Page($count,25);
$show = $Page->show();
$selectproduct = M('product')
->order('sort',desc)
->limit($Page->firstRow.','.$Page->listRows)
->select();
$this->assign('page',$show);
$this->assign('selectproduct',$selectproduct);
$this->display();
}
public function producttypelist(){
$count = D('product_type')->count();
$page = new Page($count,15);
$show = $page->show();
$type = D('product_type')
->order('id',asc)
->limit($page->firstRow.','.$page->listRows)
->select();
$this->assign('producttype',$type);
$this->assign('page',$show);
$this->display();
}
public function producttypeadd(){
if(IS_POST){
if(!D('product_type')->create()){
exit(D('product_type')->getError());
}
}
$this->display();
}
public function speclist(){
$where = '1=1';
$spec=D('spec')->where($where)->select();
$this->assign('spec',$spec);
$this->display();
}
public function productattributelist(){
$where = '1 = 1';
I('type_id') && $where = "$where and type_id = ".I('type_id') ;
$count=D('product_attribute')->where($where)->count();
$page=new \Think\Page($count,20);
$show=$page->show();
$attr=D('product_attribute')->where($where)->limit($page->firstRow.','.$page->listRows)->select();
$this->assign('page',$show);
$this->assign('attr',$attr);
$this->display();
}
public function productegory($parent_id,$level){
$category = D('product_category')->where('parent_id='.$parent_id,'level='.$level)->select();
foreach ($category as $value) {
$parent_id .= "<option value='{$value['id']}'>{$value['name']}</option>";
}
echo $parent_id;
}
}