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

namespace app\admin\controller\bbfxshop\diypage;

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


class Link extends Base
{
    public $model = null;
    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\Diypage;
    }
    
    public function getList(){
        $params = $this->param();
        
        $type = intval($params['type']);
        $type = $type?$type:1;
        $page = intval($params['page']);
        $page = $page?$page:1;
        $size = 10;
        
        $list = $this->model->getLink($type);
        $total = count($list);
        
        if($type == 2 || $type == 3 || $type == 9 || $type == 10){
            $list = array_slice($list,($page-1)*$size,$size);
        }
        
        __success("操作成功",['total'=>$total,'list'=>$list]);
    }
    
    public function goods(){
        $params = $this->param();
        $page = max(1,intval($params['page']));
        $psize = 10;
        $keyword = $params['keyword'];
        
        $where = ["uniacid"=>UNIACID,"status"=>1,"sh_status"=>1,"mid"=>0];
        if(!empty($keyword)){
            $where['title'] = ['like','%'.$keyword.'%'];
        }
        $merchant_status = Db::name("bbfx_merchant_set")->where(['uniacid'=>UNIACID])->value("status");
        if($merchant_status == 1){
            $merchants = Db::name("bbfx_merchant")->where(['uniacid'=>UNIACID,'is_shenhe'=>1,'status'=>1])->field("id")->select();
            if(!empty($merchants)){
                if(empty($merchants)) $merchants = [];
                $mids = array_column($merchants,'id');
                $mids[] = 0;
                $where['mid'] = ['in',$mids];
            }
        }
        
        $list = Db::name("bbfx_goods")->where($where)->field('id,price,title,thumb,unit,guarantee,subtitle,merchant_commission_rate')->order(['sort desc','id desc'])->limit(($page-1)*$psize,$psize)->select();
        
        if(empty($list)){
            $list = [];
        }
        
        foreach ($list as $key => $val){
            $list[$key]['thumb'] = cdnurl($val['thumb']);
            $list[$key]['guarantee'] = iunserializer($val['guarantee']);
        }
        
        $total = Db::name("bbfx_goods")->where($where)->count();
        
        __success("操作成功",['total'=>$total,'list'=>$list]);
        
    }

    public function activity(){
        $params = $this->param();
        $page = max(1,intval($params['page']));
        $psize = 10;
        
        $where = ["uniacid"=>UNIACID,"status"=>1,'sh_status'=>1];
        
        $list = Db::name("bbfx_activity_list")->where($where)->field(['id','baoming_fee','title','image','baoming_total'])->order(['id desc'])->limit(($page-1)*$psize,$psize)->select();
        
        if(empty($list)){
            $list = [];
        }
        
        foreach ($list as $key => $val){
            $list[$key]['image'] = cdnurl($val['image']);
        }
        
        $total = Db::name("bbfx_activity_list")->where($where)->count();
        
        __success("操作成功",['total'=>$total,'list'=>$list]);
        
    }
    
    public function cate(){
        $params = $this->param();
        $page = intval($params['page']);
        $psize = 15;
        $page = $page?$page:1;
        
        $list = Db::name("bbfx_category")->where(["uniacid"=>UNIACID,"status"=>1,'mid'=>0])->field(['id','title','thumb'])->order(['sort desc','id desc'])->limit(($page-1)*$psize,$psize)->select();
        
        if(empty($list)){
            $list = [];
        }
        
        foreach ($list as $key => $val){
            $list[$key]['thumb'] = cdnurl($val['thumb']);
        }
        
        $total = Db::name("bbfx_category")->where(["uniacid"=>UNIACID,"status"=>1,'mid'=>0])->count();
        
        __success("操作成功",['total'=>$total,'list'=>$list]);
        
    }

    public function coupon(){
        $params = $this->param();
        $page = intval($params['page']);
        $psize = 999;
        $page = $page?$page:1;

        $where = ["uniacid"=>UNIACID,"status"=>1,'obtain_method'=>1];
        
        $list = Db::name("bbfx_coupon")->where($where)->order('id desc')->limit(($page-1)*$psize,$psize)->select();
        
        if(!empty($list)){
            foreach ($list as $key => &$val){
                $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
            }
            unset($val);
        }
        
        // $total = Db::name("bbfx_coupon")->where($where)->count();
        
        __success("操作成功",$list);
        
    }

    public function getPintuanGoodsList(){
        $params = $this->param();
        
        $pindex = max(1, intval($params["page"]));
        $psize = intval($params["limit"])?intval($params["limit"]):20;
        
        $where = ["a.uniacid"=>UNIACID,'a.status'=>1];
        
        $keyword = trim($params['keyword']);
        $type = intval($params['type']);
        
        
    	$list = Db::name("bbfx_pintuan_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id")->where($where)->field("a.*,b.title,b.thumb,b.stock as goods_stock")->order('a.sort desc,a.id desc')->limit(($pindex - 1) * $psize,$psize)->select();
      	foreach($list as $key => &$val){
      	    
        	$val['thumb'] = cdnurl($val['thumb']);
    	    $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
        	
            $categorys = Db::name("bbfx_goods_category")->alias("a")->join("bbfx_category b","a.category_id=b.id")->where(['a.uniacid'=>UNIACID,'a.goods_id'=>$val['goods_id']])->field('b.id,b.title')->order('b.sort desc')->select();
        	
        	$val['categorys'] = $categorys;
        	
        	$typeText = "";
        	if($val['type'] == 1){
        	    $typeText = "虚拟商品(卡密)";
        	}
        	else if($val['type'] == 2){
        	    $typeText = "实体商品";
        	}
        	else if($val['type'] == 3){
        	    $typeText = "核销商品";
        	}
        	else if($val['type'] == 4){
        	    $typeText = "虚拟商品";
        	}
        	
        	$val['typeText'] = $typeText;
            
        }
        unset($val);
    	$total = Db::name("bbfx_pintuan_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id")->where($where)->count();
        __success("操作成功",['total'=>$total,'list'=>$list]);
    }
    
}