<?php
/**
 * BusinessCircle
 * Class BusinessCircle
 * @package App\Models\marketing\mysql
 */

namespace App\Services\marketing;

use App\Exception\custom\MarketingException;
use App\Models\marketing\mysql\BusinessCircle;

class BusinessCircleService
{
    /**
     * 商圈详情
     * @param array $params
     * @return \Api\PhpUtils\Mysql\MysqlBase
     */
    public static function businessCircleDetail($params = [])
    {
        return BusinessCircle::getRecord(["business_circle_id" => $params["business_circle_id"]]);
    }

    /**
     * 商圈编辑
     * @param array $params
     * @return \Api\PhpUtils\Mysql\MysqlBase
     * @throws MarketingException
     */
    public static function businessCircleEdit($params = [])
    {
        $businessCircleId = empty($params["business_circle_id"]) ? 0 : $params["business_circle_id"];

        $data = [];
        $data["business_circle_name"] = empty($params["business_circle_name"]) ? "" : $params["business_circle_name"];
        $data["longitude"] = empty($params["longitude"]) ? "" : $params["longitude"];
        $data["latitude"] = empty($params["latitude"]) ? "" : $params["latitude"];
        $data["location"] = empty($params["location"]) ? "" : $params["location"];
        $data["is_delete"] = empty($params["is_delete"]) ? BusinessCircle::STATUS_IS_DEL_NO : $params["is_delete"];

        if (!empty($businessCircleId)) {
            $businessCircle = BusinessCircle::getRecordMaster(["business_circle_name" => $data["business_circle_name"], "business_circle_id[!]" => $businessCircleId]);
            if (!empty($businessCircle)) {
                throw new MarketingException(['cus' => MarketingException::BUSINESS_CIRCLE_IS_EXIST]);
            }

            $result = BusinessCircle::save($data, ["business_circle_id" => $businessCircleId]);
        } else {
            $businessCircle = BusinessCircle::getRecordMaster(["business_circle_name" => $data["business_circle_name"]]);
            if (!empty($businessCircle)) {
                throw new MarketingException(['cus' => MarketingException::BUSINESS_CIRCLE_IS_EXIST]);
            }

            $result = BusinessCircle::save($data);
        }
        return $result;
    }

    /**
     * 逻辑删除商圈
     * @param array $params
     * @return \Api\PhpUtils\Mysql\MysqlBase
     */
    public static function businessCircleDelete($params = [])
    {
        $businessCircleId = $params["business_circle_id"];
        $data = [];
        $data["is_delete"] = empty($params["is_delete"]) ? 0 : $params["is_delete"];
        return BusinessCircle::save($data, ["business_circle_id" => $businessCircleId]);
    }

    /**
     * 商圈列表
     * @param array $params
     * @return array
     */
    public static function getList($params = [])
    {
        $params['page'] = !empty($params['page']) ? $params['page'] : 1;
        $limit = !empty($params['page_size']) ? $params['page_size'] : 20;
        $page = ($params['page'] - 1) * $limit;

        $where = [];
        if (!empty($params["business_circle_name"])) {
            $where["business_circle_name[~]"] = $params["business_circle_name"];
        }
        $where['is_delete'] = BusinessCircle::STATUS_IS_DEL_NO;
        $where['ORDER'] = ["business_circle_id" => "DESC"];
        $where['LIMIT'] = [$page, $limit];
        $list = BusinessCircle::getRecords($where);

        unset($where['LIMIT']);
        unset($where['ORDER']);
        $count = BusinessCircle::count("*", $where);
        return ['list' => $list, 'count' => $count];
    }
}