<?php
/**
 * @package merchant
 * @Author: Qinii
 * @Date: 2020/5/8
 */

namespace app\common\repositories\movie;

use app\common\dao\movie\order\MovieOrderDao;
use app\common\model\movie\cinema\Cinema;
use app\common\repositories\BaseRepository;
use think\facade\Db;

/**
 * Class CinemaRepository
 * @package app\common\repositories\movie
 * @author xaboy
 * @mixin Cinema
 */
class CinemaRepository extends BaseRepository
{
    protected $dao;

    /**
     * ProductRepository constructor.
     * @param Cinema $dao
     */
    public function __construct(Cinema $dao)
    {
        $this->dao = $dao;
    }

    /**
     * @param $page
     * @param $limit
     * @param $longitude
     * @param $latitude
     * @param $where
     * @param $with
     * @param $order
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getLocalCinema($page, $limit, $longitude, $latitude, $where = [], $with = [], $order = 1)
    {
        $field = 'cinema_sign,cinema_name,address,tel,city_sign,city_name,area_sign,area_name,longitude,latitude';

        $cinema_list = $this->dao->search(null, $where)->when($with, function ($query) use ($with) {
            $query->with($with);
        })->when($latitude && $longitude, function ($query) use ($field, $longitude, $latitude) {
            $query->field([$field, $this->distance($latitude, $longitude)]);
        })->when($page && $limit, function ($query) use ($page, $limit) {
            $query->page($page, $limit);
        })->when(!is_null($order), function ($query) use ($order) {
            if ($order == 1) {//距离最近
                $query->order('distance asc');
            } else {//最新需求
                $query->order('create_time desc');
            }
        })->field([$field])->select()->toArray();
        foreach ($cinema_list as $key => $value) {
            $cinema_list[$key]['distance'] = isset($value['distance']) ? round($value['distance'] / 1000, 2) : 0.00;
        }

        $count = $this->dao->search(null, $where)->when($with, function ($query) use ($with) {
            $query->with($with);
        })->when($latitude && $longitude, function ($query) use ($field, $longitude, $latitude) {
            $query->field([$field, $this->distance($latitude, $longitude)]);
        })->count();

        return compact('cinema_list', 'count');
    }

    /**
     * 经纬度排序计算
     * @param string $latitude
     * @param string $longitude
     * @return string
     */
    private function distance(string $latitude, string $longitude)
    {
        return "(round(6378137 * 2 * asin(sqrt(pow(sin(((latitude * pi()) / 180 - ({$latitude} * pi()) / 180) / 2), 2) 
        + cos(({$latitude} * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ({$longitude} * pi()) / 180) / 2), 2))))) AS distance";
    }

    /**
     * 获取影院信息
     *
     * @param $cinema_sign
     * @return Cinema|array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCinemaInfo($cinema_sign)
    {
        return $this->dao->search(null, ['cinema_sign' => $cinema_sign])->find();
    }
}
