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

namespace app\admin\controller\bbfxshop\bargain;

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


class Goods extends Base
{

    public $model = null;
    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\Seckill;
    }
    
    public function getList(){
        $params = $this->param();
        
        $pindex = max(1, intval($params["page"]));
        $psize = intval($params["limit"])?intval($params["limit"]):20;
        
        $where = ["a.uniacid"=>UNIACID];
        
        $keyword = trim($params['keyword']);
        if($keyword){
            $where['b.title'] = ['like','&'.$keyword.'&'];
        }
        
    	$list = Db::name("bbfx_bargain_goods")->alias("a")->join("bbfx_goods b","a.goods_id = b.id")->where($where)->field("a.*,b.title,b.thumb,b.price as o_price,b.type as goods_type,b.stock")->order("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']);
    	    $val['updatetime'] = date("Y-m-d H:i:s",$val['updatetime']);
        	
        	$typeText = "";
        	if($val['goods_type'] == 1){
        	    $typeText = "卡密商品";
        	}
        	else if($val['goods_type'] == 2){
        	    $typeText = "实体商品";
        	}
        	else if($val['goods_type'] == 3){
        	    $typeText = "核销商品";
        	}
        	else if($val['goods_type'] == 4){
        	    $typeText = "虚拟商品";
        	}
        	$val['goodsTypeText'] = $typeText;

            $val['category_title'] = Db::name("bbfx_bargain_goods_category")->where(['id'=>$val['category_id']])->value("title");
            if(empty($val['category_title'])) $val['category_title'] = "";
        	
            $val['bargain_price'] = round($val['s_price'] - $val['price'],2);
            $val['bargain_count'] = Db::name("bbfx_bargain_goods_log")->where(['uniacid'=>UNIACID,'bargain_goods_id'=>$val['id'],'createtime'=>['>',$val['stime']]])->count();
        }
        unset($val);
    	$total = Db::name("bbfx_bargain_goods")->alias("a")->join("bbfx_goods b","a.goods_id = b.id")->where($where)->count();
        __success("操作成功",['total'=>$total,'list'=>$list]);
    }
    
    public function search(){
        $params = $this->param();
        
        $goodsids = trim($params['goodsids']);
        $goodsids = explode(",",$goodsids);
        
        $where = ['a.uniacid'=>UNIACID,'a.id'=>['in',$goodsids]];
        
        
        $list = Db::name("bbfx_bargain_goods")->alias("a")->join("bbfx_goods b","a.goods_id = b.id")->where($where)->field("a.*,b.title,b.thumb,b.price as o_price,b.type as goods_type,b.stock")->select();
        foreach ($list as $key => &$val){
            $val['thumb'] = cdnurl($val['thumb']);
            
            $cate_title = Db::name("bbfx_goods_category")->alias("a")->join("bbfx_category b","a.category_id=b.id")->where(['a.uniacid'=>UNIACID,'a.goods_id'=>$val['id']])->field('b.id,b.title')->value("title");
            
            if(!empty($cate_title)){
        	    $val['cate_title'] = $cate_title;
        	}
        	else{
        	    $val['cate_title'] = "";
        	}
        }
        unset($val);
        
        if(empty($list)){
            __error("未查询到数据");
        }
        else{
            __success("操作成功",$list);
        }
    }

    public function resetPrice(){
        $id = $this->param('id/d');
        $item = Db::name("bbfx_bargain_goods")->where(['id'=>$id])->find();
        if(empty($item)){
            __error("砍价商品不存在");
        }

        $data = [
            "price"=>$item['s_price'],
            "stime"=>time(),
            "etime"=>time() + $item['effective_day'] * 24 * 60 * 60
        ];

        Db::name("bbfx_bargain_goods")->where(["id"=>$id])->update($data);
        
        __success("操作成功");
    }
    
    public function post(){
        $params = $this->param();
        
        $id = intval($params['id']);
        $item = Db::name("bbfx_bargain_goods")->where(['id'=>$id])->find();

        if($params['goods_id'] <= 0){
        	__error("请选择商品");
        }

        $goods = Db::name("bbfx_goods")->where(['id'=>$params['goods_id']])->find();
        if(empty($goods)){
            __error("商品不存在");
        }
        if($goods['status'] != 1){
            __error("商品已下架");
        }

        $rule = $params['rule'];
        if(empty($rule)){
            __error("请添加砍价规则");
        }
        $price = 9999999;
        foreach($rule as $key => &$val){
            $max = round($val['max'],2);
            $min = round($val['min'],2);
            $bargain_max = round($val['bargain_max'],2);
            $bargain_min = round($val['bargain_min'],2);
            
            if($max <= $min){
                __error("规则".($key+1)."价格区间必须从大到小");
            }
            if($bargain_max <= $bargain_min){
                __error("规则".($key+1)."砍价价格区间必须从大到小");
            }

            if($price != $max && $price != 9999999){
                __error("规则".($key+1)."价格区间最大值必须等于上一区间最小值");
            }
            $price = $min;
        }

        
    	$data = array(
        	"uniacid"=>UNIACID,
          	"buy_interval_day"=>intval($params['buy_interval_day']),
          	"effective_day"=>intval($params['effective_day']),
          	"stock"=>intval($params['stock']),
          	"goods_id"=>intval($params['goods_id']),
          	"category_id"=>intval($params['category_id']),
            "virtual_check_num"=>intval($params['virtual_check_num']),
          	"status"=>intval($params['status']),
            "price"=>$rule[0]['max'],
            "s_price"=>$rule[0]['max'],
            "e_price"=>$rule[count($rule)-1]['min'],
            "use_desc"=>iserializer($params['use_desc']),
            "rule"=>iserializer($rule),
          	"updatetime"=>time(),
        );
        

        if($data['effective_day'] <= 0){
            __error("有效天数必须大于0");
        }
        

        if(!empty($item)){
            unset($data['goods_id']);
            
            if($item['etime'] > time() || ($data['status'] == 1 && $item['status'] == 0)){
                $data['stime'] = strtotime(date("Y-m-d 00:00:00"));
                $data['etime'] = strtotime(date("Y-m-d 00:00:00")." +".$data['effective_day']." day");
            }

            $res = Db::name("bbfx_bargain_goods")->where(["id"=>$item['id']])->update($data);
            $data['id'] = $item['id'];
        }
      	else{
            $data['stime'] = strtotime(date("Y-m-d 00:00:00"));
            $data['etime'] = strtotime(date("Y-m-d 00:00:00")." +".$data['effective_day']." day");
          	$data['createtime'] = time();
            $res = Db::name("bbfx_bargain_goods")->insertGetId($data);
            $data['id'] = $res;
        }
        
        if($res){
            \app\admin\model\AdminLog::record("","",$data,"bbfx_bargain_goods");
            __success("编辑成功");
        }
        else{
            __success("操作成功");
        }
    }
    public function detail(){
        $params = $this->param();
        
        $id = intval($params['id']);
        $goods = Db::name("bbfx_bargain_goods")->where(['id'=>$id])->find();
        
        if(!empty($goods)){

            $goods['rule'] = iunserializer($goods['rule']);
            $goods['use_desc'] = iunserializer($goods['use_desc']);

            __success("操作成功",$goods);
        }
        else{
            __error("商品不存在");
        }
    }
    
    public function status(){
        $params = $this->param();
        
        $id = intval($params['id']);
        $status = intval($params['status']);
        if($status != 1) $status = 0;

        $item = Db::name("bbfx_bargain_goods")->where(['id'=>$id])->find();
        if(empty($item)){
            __error("商品不存在");
        }

        $res = Db::name("bbfx_bargain_goods")->where(["id"=>$id])->update(["status"=>$status]);
        if($res){
            \app\admin\model\AdminLog::record("","",["id"=>$id,"status"=>$status],"bbfx_bargain_goods");
            __success("操作成功");
        }
        else{
            __success("操作成功");
        }
    }

    public function zhiding(){
        $params = $this->param();
        
        $id = intval($params['id']);
        $zhiding = intval($params['zhiding']);
        if($zhiding != 1) $zhiding = 0;

        $item = Db::name("bbfx_bargain_goods")->where(['id'=>$id])->find();
        if(empty($item)){
            __error("商品不存在");
        }
        
        $res = Db::name("bbfx_bargain_goods")->where(["id"=>$id])->update(["zhiding"=>$zhiding]);
        if($res){
            \app\admin\model\AdminLog::record("","",["id"=>$id,"zhiding"=>$zhiding],"bbfx_bargain_goods");
            __success("操作成功");
        }
        else{
            __success("操作成功");
        }
    }
    
    public function delete(){
        $this->commonDelete('bbfx_bargain_goods');
    }
}