<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/4/3
 * Time: 下午5:14
 * 楼盘导出
 */

namespace backend\controllers;

use common\models\Housepushstatus;
use Yii;
use backend\base\Help;
use backend\base\CommonController;
use common\models\House;
use yii\helpers\ArrayHelper;

class OtherwatermarkController extends CommonController
{
    public function actionAa()
    {
        $m = House::find()->select(['id', 'name'])->asArray()->all();
        $s = new Housepushstatus();
        foreach ($m as $val) {
            $_s = clone $s;
            $_s->status = 1;
            $_s->hid = $val['id'];
            $_s->save();
        }
    }

    //楼盘导出-楼盘显示页面
    public function actionHouse()
    {
        return $this->render('house');
    }

    //楼盘导出-楼盘显示页面数据
    public function actionHousrform()
    {
//            $model = new House();
        $input = Yii::$app->request->post();

        if (!empty($input['city'])) {
            $city = new \common\models\CategoryCity();
            $pro = $city->FindById($input['city']);
            if ($pro['pid'] == 0) {
                $r = $city->GetAllSon($input['city'], ['city_name', 'id']);
                if (!empty($r)) {
                    $input['city'] = array_column($r, 'id');
                }
            }
        }

        $query = House::find();

        if (ArrayHelper::keyExists('start_time', $input) && !empty($input['start_time'])) {
            $query->andFilterWhere(['>=', 'pfg_house.create_at', strtotime($input['start_time'] . ' 00:00:00')]);
        }

        if (ArrayHelper::keyExists('end_time', $input) && !empty($input['end_time'])) {
            $query->andFilterWhere(['<=', 'pfg_house.create_at', strtotime($input['end_time'] . ' 23:59:59')]);
        }

        $query->andFilterWhere(['pfg_house.del' => [1, 2]]);

        if (ArrayHelper::keyExists('name', $input)) {
            $query->andFilterWhere(['like', 'pfg_house.name', $input['name']]);
        }

        if (ArrayHelper::keyExists('id', $input)) {
            $query->andFilterWhere(['pfg_house.id' => $input['id']]);
        }

        if (ArrayHelper::keyExists('city', $input) && !empty($input['city'])) {
            $query->andWhere(['pfg_house.city' => $input['city']]);
        }

        if (ArrayHelper::keyExists('housestatus', $input)) {
            switch ($input['housestatus']) {
                case 1:
                case 2:
                    $query->andFilterWhere(['pfg_house.is_view' => $input['housestatus']]);
                    break;
                case 3:
                case 4:
                    if ($input['housestatus'] == 3) {
                        $delNum = 1;
                    } else if ($input['housestatus'] == 4) {
                        $delNum = 2;
                    }
                    $query->andFilterWhere(['pfg_house.del' => $delNum]);
                    break;
                case 5:
                    $query->andFilterWhere(['pfg_house.del' => 1]);
                    $query->andFilterWhere(['pfg_house.is_view' => 1]);
                    break;
            }
        }

        if (ArrayHelper::keyExists('housepermit', $input)) {
            $yushou = new \common\models\HousePermit();
            $yushow = $yushou::find()->andWhere(['del' => 1])->select(['hid'])->groupBy('hid')->column();

            switch ($input['housepermit']) {
                case 1:

                    if ($yushow != null) {
                        $query->andWhere(['pfg_house.id' => $yushow]);
                    }
                    break;
                case 2:

                    if ($yushow != null) {
                        $query->andWhere(['not', ['pfg_house.id' => $yushow]]);
                    }
                    break;
            }
        }

        if (!empty($input['page'])) {
            $query->offset = ($input['page'] - 1) * $input['limit'];
            $query->limit = $input['limit'];
        }


        $query->select(['pfg_house.create_at', 'pfg_house.id', 'pfg_category_city.city_name', 'pfg_house.name', 'pfg_house.is_view', 'pfg_house.del', 'pfg_housepushstatus.status']);
        $statusSql = '';
        if (ArrayHelper::keyExists('status', $input) && !empty($input['status'])) {
            switch ($input['status']) {
                case 1:
                    $query->andFilterWhere(['pfg_housepushstatus.status' => 1]);
                    break;
                case 2:
                    $query->andWhere('pfg_house.id not in(select hid from pfg_housepushstatus where status in(1))');
                    break;
            }
        }
        $query->leftJoin('pfg_housepushstatus', 'pfg_house.id = pfg_housepushstatus.hid');
        $query->leftJoin('pfg_category_city', 'pfg_house.city=pfg_category_city.id');
        $count = $query->count();
        $rows = $query->orderBy(['pfg_house.create_at' => SORT_DESC])->asArray()->all();

        if ($rows != null) {

            $id = array_column($rows, 'id');

            //预售证
            $yushou = new \common\models\HousePermit();
            $yushow = $yushou->HidTotal(['hid' => $id]);
            if ($yushow != null) {
                $PermitColumn = array_column($yushow, 'num', 'hid');
            }

            foreach ($rows as &$val) {
                $val['create_at'] = date('Y-m-d H:i', $val['create_at']);
                $val['permit'] = 0;
                if (!empty($PermitColumn[$val['id']])) {
                    $val['permit'] = $PermitColumn[$val['id']];
                }
            }

            return Help::JsonData(0, '成功', $count, $rows);
        }
        return Help::JsonCode(Help::ERROR, Yii::t('app', 'get_error'));
    }

    //导出按钮
    public function actionExport()
    {
        $input = Yii::$app->request->get();

        if (!empty($input['city'])) {
            $city = new \common\models\CategoryCity();
            $pro = $city->FindById($input['city']);
            if ($pro['pid'] == 0) {
                $r = $city->GetAllSon($input['city'], ['city_name', 'id']);
                if (!empty($r)) {
                    $input['city'] = array_column($r, 'id');
                }
            }
        }

        $query = House::find();

        if (ArrayHelper::keyExists('start_time', $input) && !empty($input['start_time'])) {
            $query->andFilterWhere(['>=', 'pfg_house.create_at', strtotime($input['start_time'] . ' 00:00:00')]);
        }

        if (ArrayHelper::keyExists('end_time', $input) && !empty($input['end_time'])) {
            $query->andFilterWhere(['<=', 'pfg_house.create_at', strtotime($input['end_time'] . ' 23:59:59')]);
        }

        $query->andFilterWhere(['pfg_house.del' => [1, 2]]);

        if (ArrayHelper::keyExists('name', $input)) {
            $query->andFilterWhere(['like', 'pfg_house.name', $input['name']]);
        }

        if (ArrayHelper::keyExists('id', $input)) {
            $query->andFilterWhere(['pfg_house.id' => $input['id']]);
        }

        if (ArrayHelper::keyExists('city', $input) && !empty($input['city'])) {
            $query->andWhere(['pfg_house.city' => $input['city']]);
        }

        if (ArrayHelper::keyExists('housestatus', $input)) {
            switch ($input['housestatus']) {
                case 1:
                case 2:
                    $query->andFilterWhere(['pfg_house.is_view' => $input['housestatus']]);
                    break;
                case 3:
                case 4:
                    if ($input['housestatus'] == 3) {
                        $delNum = 1;
                    } else if ($input['housestatus'] == 4) {
                        $delNum = 2;
                    }
                    $query->andFilterWhere(['pfg_house.del' => $delNum]);
                    break;
                case 5:
                    $query->andFilterWhere(['pfg_house.del' => 1]);
                    $query->andFilterWhere(['pfg_house.is_view' => 1]);
                    break;
            }
        }
        if (!empty($input['page'])) {
            $query->offset = ($input['page'] - 1) * $input['limit'];
            $query->limit = $input['limit'];
        }


        $query->select(['pfg_house.create_at', 'pfg_house.id', 'pfg_category_city.city_name', 'pfg_house.name', 'pfg_house.is_view', 'pfg_house.del', 'pfg_housepushstatus.status']);
        if (ArrayHelper::keyExists('status', $input) && !empty($input['status'])) {
            switch ($input['status']) {
                case 1:
                    $query->andFilterWhere(['pfg_housepushstatus.status' => 1]);
                    break;
                case 2:
                    $query->andWhere('pfg_house.id not in(select hid from pfg_housepushstatus where status in(1))');
                    break;
            }
        }
        $query->leftJoin('pfg_housepushstatus', 'pfg_house.id = pfg_housepushstatus.hid');
        $query->leftJoin('pfg_category_city', 'pfg_house.city=pfg_category_city.id');
        $data = $query->orderBy(['pfg_house.create_at' => SORT_DESC])->asArray()->all();
        if (!empty($data)) {
            $excel = new \common\api\ExcelServer();
            $excel->SetTitle('楼盘项目信息');
            $excel->SheetIndex();
            $excel->SheetText($data);
            $excel->Export(date('YmdHi') . '.xls');

        }
        echo '<script> alert("无该数据") </script>';
    }

    /**
     * 修改推广状态
     */
    public function actionPushstatus()
    {
        $input = Yii::$app->request->post();

        $model = new \common\models\Housepushstatus();
        $row = $model->FindById($input['id']);
        if (!empty($row)) {
            switch ($row->status) {
                case 1:
                    $row->status = 2;
                    break;
                case 2:
                    $row->status = 1;
                    break;
            }

            $result = $row->save();
        } else {
            $model->status = 1;
            $model->hid = $input['id'];
            $result = $model->save();
        }

        if ($result) return Help::JsonCode(Help::SUCCESS, Yii::t('app', 'edit_success'));
        return Help::JsonCode(Help::ERROR, Yii::t('app', 'edit_error'));

    }


    //批量修改推广
    public function actionPushstatuss()
    {
        $input = Yii::$app->request->post();
        $model = new \common\models\Housepushstatus();
        if (is_array($input['hid']) && !empty($input['hid'])) {
            foreach ($input['hid'] as $val) {
                $_m = clone $model;
                $find = $_m::findOne($val);
                if (!empty($find)) {
                    switch ($find['status']) {
                        case 1:
                            $find->status = 2;
                            break;
                        case 2:
                            $find->status = 1;
                            break;
                    }
                    $find->save();
                } elseif (empty($find)) {
                    $_m->status = 1;
                    $_m->hid = $val;
                    $_m->save();
                }

            }
            return Help::JsonCode(Help::SUCCESS, Yii::t('app', 'edit_success'));
        }
        return Help::JsonCode(Help::ERROR, Yii::t('app', 'edit_error'));
    }


    /*
     * 其他管理-水印设置显示页面
     * */
    public function actionHome()
    {
        $query = \common\models\CategoryWatermark::find()->asArray()->one();
        $positionArr = [
            1 => '#1',
            2 => '#2',
            3 => '#3',
            4 => '#4',
            5 => '#5',
            6 => '#6',
            7 => '#7',
            8 => '#8',
            9 => '#9',
        ];
        $query['template'] = Yii::$app->params['watermark_img']['url'] . $query['template'];
        return $this->render('home', ['model' => $query, 'position' => $positionArr]);
    }


    /*
     * 其他管理-修改水印设置
     * */
    public function actionEditform()
    {
        $model = new \common\models\CategoryWatermark();
        $input = Yii::$app->request->post();
        $model->load($input, '');
        if ($model->validate()) {
            $url = Yii::$app->params['watermark_img']['url'];
            $img = \backend\server\UploadFile::InstanceImgName('template', $url);
            $row = $model->FindById(Yii::$app->request->post('id'));
            if ($row != null) {
                $setAtt = Help::SetAttr(Yii::$app->request->post(), $model, $row);
                if ($img != false) {
                    $setAtt->template = $img;
                }
                if (!isset($input['status'])) {
                    $setAtt->status = 2;
                }


                if ($setAtt->update(false) == true) return Help::JsonCode(Help::SUCCESS, '操作成功');

            }

        }
        return Help::JsonCode(Help::ERROR, '操作失败', $model->errors);

    }


}