<?php
namespace app\common\model;
use think\Db;
class ShopDay extends Base {
    public $table = 'shop_day';
    protected $validateName = 'ShopDay';
    public function updates(){
        $data = request()->post();
        $time = $data['time'];
        if(!$time){
            $this->error = '未选择开始结束时间';
            return false;
        }
        $times = explode(',',$time);
        if(count($times) != 2){
            $this->error = '未选择开始结束时间';
            return false;
        }
        $data['start'] = $times[0];
        $data['end'] = $times[1];
        unset($data['time']);
        $data['org_id'] = cur_org_id();
        $result = validate($this->validateName)->check($data,[],'');
        if(true !== $result){
            $this->error = validate($this->validateName)->getError();
            return false;
        }
        $id = $data['id'];
        unset($data['id']);
        if($id > 0){
            $data['update_time'] = date('Y-m-d H:i:s');
            $ret = $this->allowField(true)->save($data,['id'=>$id]);
        }else{
            $data['create_time'] = date('Y-m-d H:i:s');
            $ret = $this->allowField(true)->save($data);
        }
        if(!$ret){
            $this->error = '操作失败';
            return false;
        }
        return true;
    }

    //获取今天和今天后的6天
    public function shopDate()
    {
        $frist=strtotime(date("Y-m-d"));
        $last=strtotime(date("Y-m-d"))+86400*6;
        $date=array();
        $array = ["日","一","二","三","四","五","六"];
        while($frist<=$last){
            $data['date']=date('Y-m-d',$last);
            $data['day']=date('d',$last);
            $data['week']=$array[date('w',$last)];
            array_unshift($date,$data);
            $last-=86400;
        }
        return $date;
    }

    public function cate($orgid){
        $cate = Db::name('shop_goods_cate')->where('org_id',$orgid)->field('id,name')->select();
        $goods = Db::name('shop_goods')
            ->where('org_id',$orgid)
            ->where('del',0)
            ->where('enable',1)
            ->field('id,title,price,img,description,cate_id')
            ->order('sort desc,id desc')
            ->select();
        foreach ($cate as $k=>$v){
            $gs = [];
            foreach ($goods as $kk=>$vv){
                if($vv['cate_id'] == $v['id']){
                    $gs[] = $vv;
                }
            }
            $cate[$k]['goods'] = $gs;
        }

        return $cate;
    }

    public function cate_old($type,$orgid){
        $ret = Db::name('shop_day')
            ->where('org_id',$orgid)->where('id',$type)->value('goods');
        if(!$ret){
            return false;
        }

        $ids = explode(',',$ret);
        $goods = Db::name('shop_goods')
            ->where('org_id',$orgid)
            ->where('del',0)
            ->where('enable',1)
            ->where('id','in',$ids)
            ->field('id,title,price,img,description,cate_id')
            ->order('sort desc,id desc')
            ->select();
        foreach ($goods as $k=>$v){
            $goodscateid[]=$v['cate_id'];
        }

        $cateId = array_unique($goodscateid);
        $cate = Db::name('shop_goods_cate')
            ->where('id','in',$cateId)->field('id,name')
            ->select();
        foreach ($cate as $k=>$v){
            $gs = [];
            foreach ($goods as $kk=>$vv){
                if($vv['cate_id'] == $v['id']){
                    $gs[] = $vv;
                }
            }
            $cate[$k]['goods'] = $gs;
        }

        return $cate;
    }

    public function goods($cate_id,$orgid){
        $ret = Db::name('shop_goods')
            ->field('id,title,price,img,description')
            ->where('cate_id',$cate_id)
            ->where('org_id',$orgid)
            ->where('del',0)
            ->where('enable',1)
            ->select();
        return $ret;
    }

    public function goods_old($type,$cate_id,$orgid){
        $ret = Db::name('shop_day')
            ->where('org_id',$orgid)->where('id',$type)->find();
        if(!$ret){
            return false;
        }
        $ids = explode(',',$ret['goods']);
        $ret = Db::name('shop_goods')
            ->field('id,title,price,img,description')
            ->where('cate_id',$cate_id)
            ->whereIn('id',$ids)
            ->where('del',0)
            ->where('enable',1)
            ->select();
        return $ret;
    }

}