<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/3/4
 * Time: 上午9:33
 */

namespace common\models;

//use yii\db\ActiveRecord;
//use yii\behaviors\TimestampBehavior;
class HouseAlbum extends Common
{
//        public $setDel = 1;
//        public function behaviors()
//        {
//            return [
//                [
//                    'class' => TimestampBehavior::className(),
//                    'attributes' => [
//                        # 创建之前
//                        ActiveRecord::EVENT_BEFORE_INSERT => ['create_at', 'update_at'],
//                        # 修改之前
//                        ActiveRecord::EVENT_BEFORE_UPDATE => ['update_at']
//                    ],
//                    #设置默认值
//                    'value' => $_SERVER['REQUEST_TIME']
//                ]
//            ];
//        }


    public function rules()
    {
        return [
            [['hid', 'album_id'], 'required', 'message' => '{attribute}不能为空'],
            ['sort', 'number'],
            ['hid', 'number'],
            ['title', 'string', 'max' => 50],
            ['del', 'in', 'range' => [1, 2]],
            ['del', 'default', 'value' => 1]
        ];
    }

    public function attributeLabels()
    {
        return [
            'album_id' => '所属栏目',
        ];
    }

    public function Authenticator($input)
    {
        $this->load($input, '');
        if (!$this->validate()) return $this->errors;
        return $this;
    }

    /*
     * 相册分组,获取每组的个数和组名。
     * */
    public function AlbumGroup($input)
    {
        $query = self::find();
        $query->select(['pfg_category_album.name', 'pfg_house_album.img', 'pfg_house_album.album_id', 'pfg_house_album.state', 'pfg_house_album.hid', "count('pfg_house_album.album_id') as num"]);
        $query->andWhere(['pfg_house_album.hid' => $input['hid']]);
        $query->andWhere(['pfg_house_album.del' => $this->setDel]);
        $query->leftJoin('pfg_category_album', 'pfg_house_album.album_id = pfg_category_album.id');
        $query->groupBy('pfg_house_album.album_id');
        $query->orderBy(['pfg_category_album.sort' => SORT_DESC]);
        return $query->asArray()->all();
    }

    /*
     * 相册分组,获取每组的个数和组名。(取出主图)
     * */
    public function AlbumGroupzhu($input)
    {
        $query = self::find();
        $query->select(['pfg_category_album.name', 'pfg_house_album.img', 'pfg_house_album.album_id', 'pfg_house_album.state', 'pfg_house_album.hid','pfg_house_album.album_id']);
        $query->andWhere(['pfg_house_album.hid' => $input['hid']]);
        $query->andWhere(['pfg_house_album.del' => $this->setDel]);
        $query->leftJoin('pfg_category_album', 'pfg_house_album.album_id = pfg_category_album.id');
        $query->orderBy(['pfg_category_album.sort' => SORT_DESC,'pfg_house_album.zhu'=>SORT_DESC]);
        $data = $query->asArray()->all();
        $arr = [];
        $arr_data = [];
        foreach ($data as $val){
            if(!in_array($val['album_id'],$arr)){
                $arr[] = $val['album_id'];
                $arr_data[$val['album_id']] = $val;
                $arr_data[$val['album_id']]['num'] = 1;
            }else{
                $arr_data[$val['album_id']]['num'] += 1;
            }
        }
        return array_values($arr_data);
    }
    /**
     * 分组后 各取一张
     * 2020.10.4 lyy 修改 (直接查询暂时无法进行排序分组获取) 改为 遍历分组获取排序最高
     */
    public function Groupfind($input, $limit = null)
    {
        $query = self::find();
        $query->select(['pfg_category_album.name','pfg_house_album.sort', 'pfg_house_album.img', 'pfg_house_album.album_id', 'pfg_house_album.state', 'pfg_house_album.hid']);
        $query->andWhere(['pfg_house_album.hid' => $input['hid']]);
        $query->andWhere(['pfg_house_album.del' => $this->setDel]);
        $query->leftJoin('pfg_category_album', 'pfg_house_album.album_id = pfg_category_album.id');
        if(!empty($limit)){
            $query->limit = $limit;
        }
        $query->orderBy(['pfg_house_album.sort' => SORT_DESC, 'pfg_house_album.create_at' => SORT_DESC]);
//        $query->groupBy('pfg_house_album.album_id');
        $data = $query->asArray()->all();
        $list = [];
        foreach ($data as $val){
            if(!isset($list[$val['album_id']])){
                $list[$val['album_id']] = $val;
                $list[$val['album_id']]['album_total'] = 1;
            }elseif($list[$val['album_id']]['sort'] < $val['sort']){
                $val['album_total'] = $list[$val['album_id']]['album_total']+1;
                $list[$val['album_id']] = $val;
            }else{
                $list[$val['album_id']]['album_total'] += 1;
            }
        }
        return array_values($list);
    }


    /**
     * 分组后 各取一张 加总数
     */
    public function Groupfind_Total($input, $limit = null)
    {
        $query = self::find();
        $query->select(['COUNT(*) as album_total', 'pfg_category_album.name', 'pfg_house_album.img', 'pfg_house_album.album_id', 'pfg_house_album.state', 'pfg_house_album.hid']);
        $query->andWhere(['pfg_house_album.hid' => $input['hid']]);
        $query->andWhere(['pfg_house_album.del' => $this->setDel]);
        $query->leftJoin('pfg_category_album', 'pfg_house_album.album_id = pfg_category_album.id');
        $query->limit = $limit;
        $query->groupBy('pfg_house_album.album_id');
        $query->orderBy(['pfg_category_album.sort' => SORT_DESC, 'pfg_house_album.zhu' => SORT_DESC, 'pfg_house_album.sort' => SORT_DESC]);
        return $query->asArray()->all();
    }

    /*
     * 移动端 随机获取一张图片
     * */
    public function RandFind()
    {
        $query = self::find();
        $query->select(['img']);
        $query = $this->QueryWhere($query);
        return $query->orderBy('RAND()')->asArray()->one();
    }


    public function getList()
    {
        $query = self::find();
        $query = $this->QueryWhere($query);
        return $query->asArray()->all();
    }

    public function HidTotal()
    {
        $query = self::find();
        $query->select(["count('hid') as num", 'hid']);
        $query = $this->QueryWhere($query);
        $query->andWhere(['NOT', ['img' => '']]);
        return $query->groupBy('hid')->asArray()->all();
    }

    protected function QueryWhere($query)
    {
        $query->andFilterWhere(['hid' => $this->hid]);
        $query->andFilterWhere(['del' => $this->setDel]);
        $query->andFilterWhere(['album_id' => $this->album_id]);
        $query->orderBy(['sort' => SORT_DESC, 'id' => SORT_DESC]);
        return $query;
    }

    public function Total()
    {
        $query = self::find();
        $query = $this->QueryWhere($query);
        return $query->count();
    }

}