<?php


namespace app\api\controller;

use app\api\model\Deposit;
use app\api\model\DepositOrder;
use app\api\model\Industry;
use app\api\model\StoreComment;
use app\api\model\StoreInform;
use app\api\model\StoreInformGood;
use app\api\model\UserHouse;
use app\api\validate\HotValidate;
use app\common\controller\Api;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\Request;

/**
 * 周边热点接口
 */
class Hot extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    protected $store_model;
    protected $store_inform_model;
    protected $store_inform_good_model;
    protected $user_house_model;
    protected $industry_model;
    protected $favorite_model;
    protected $comment_model;
    protected $good_model;
    protected $follow_model;
    protected $user_id;

    public function __construct(Request $request,\app\api\model\Store $store,Industry $industry,StoreInform $store_inform,UserHouse $user_house,StoreInformGood $store_inform_good)
    {
        parent::__construct($request);
        $this->industry_model = $industry;
        $this->store_model = $store;
        $this->store_inform_model = $store_inform;
        $this->store_inform_good_model = $store_inform_good;
        $this->user_house_model = $user_house;
        $this->user_id = $this->auth->id;
    }

    /**
     * 商圈信息列表
     * @ApiWeigh    (80)
     *
     * @ApiTitle    (商圈信息列表)
     * @ApiSummary  (商圈信息列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/hot/store_list)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="page", type="integer", required=true, description="页数")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
        "list": [
            {
            "id": 5,
            "user_id": 1,
            "store_id": 0,
            "content": "内容",
            "images": "http://cloud.caiyunpan.brotop.cn/assets/img/qrcode.png",
            "house_ids": ",1,",
            "type": 类型1=红包2=一般信息,
            "score": "5800",
            "red_package": "5000",
            "number": 红包数量,
            "single": "单人领取金额",
            "get": 已领数量,
            "surplus": 剩余数量,
            "views": 浏览量,
            "good_count": 点赞总数,
            "is_good": 是否点赞0=否1=是
            "image_arr"图片: [
                "http://cloud.caiyunpan.brotop.cn/assets/img/qrcode.png"
            ],
            "store": {
                "id": 1,
                "store_name": "店铺名称",
                "store_icon": "图标",
                "industry_id": "行业标签",
                },
            },
        ],
        "this_page": 当前页数,
        "total_page": 总页数
    }
    })
     */
    public function store_list()
    {
        if($this->request->isPost()){
            $page = $this->request->param('page',1,'intval');
            $house_ids = $this->user_house_model->where('user_id',$this->user_id)->column('house_id');
            $query = function ($query) use ($house_ids){
                foreach ($house_ids as $k=>$v){
                    $w = ['house_ids'=>['like','%,'.$v.',%']];
                    if($k == 0) {
                        $query->where($w);
                    } else {
                        $query->whereOr($w);
                    }
                }
            };
            $where = [
                'where' => $query,
                'with' => ['store']
            ];
            $order = ['number'=>'DESC','createtime'=>'DESC'];
            $inform = $this->store_inform_model->pageSelect($page,$where,'*',$order,config('option.num'));
            $list = $inform->items();
            foreach ($list as &$v) {
                // 获取点赞数及是否点赞
                $where_g = [
                    'object_id' => $v['id']
                ];
                $v['good_count'] = $this->store_inform_good_model->getCount($where_g);
                $where_g['user_id'] = $this->user_id;
                $v['is_good'] = $this->store_inform_good_model->getCount($where_g);
            }
            $return = [
                'list' => $list,
                'this_page' => $inform->currentPage(),
                'total_page' => $inform->lastPage()
            ];
            $this->success('请求成功',$return);
        }
    }

    /**
     * 留言提交
     * @ApiWeigh    (50)
     *
     * @ApiTitle    (留言提交)
     * @ApiSummary  (留言提交)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/hot/comment_add)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="store_id", type="integer", required=true, description="店铺id")
     * @ApiParams   (name="content", type="string", required=true, description="留言内容")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function comment_add()
    {
        $param = (new HotValidate())->goCheck('comment_add');
        $where = [
            'where' => ['id' => $param['store_id']]
        ];
        $store = $this->store_model->findOrFail($where);
        Db::startTrans();
        $result = false;
        try{
            $param['user_id'] = $this->auth->id;
            $param['store_user_id'] = $store['user_id'];
            $model = new StoreComment();
            $result = $model->add($param);
            Db::commit();
        } catch (PDOException $e) {
            Db::rollback();
            $this->error($e->getMessage());
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if(!$result) {
            $this->error('留言失败');
        }
        $this->success('留言成功');
    }
}