<?php

namespace app\common\controller;

use app\BaseController;
use app\common\middleware\Auth;
use app\admin\model\Notice as NoticeModel;
use think\facade\Db;

/**
 * Description of Tool
 *
 * @author sgq
 */
class Tool extends BaseController {

    //protected $middleware = [Auth::class];

    public function findChildDictBatch() {
        $result = [];
        for ($i = 0; $i < count($this->request->param()); $i++) {
            if ($obj = $this->request->param($i)) {
                $code = $obj["code"];
                $dict = \app\common\model\Dict::where("code", $code)->findOrEmpty();
                $childs = \app\common\model\Dict::where("pid", $dict["id"])->order("num")->select();
                $result[$obj["name"]] = $childs;
            }
        }
        return json(["code" => "200", "msg" => "", "obj" => $result]);
    }

    public function findChildDictByCode() {
        $code = \StrUtil::getRequestDecodeParam($this->request, "code");
        return json(\app\common\api\DictApi::findChildDictByCode($code));
    }

    public function getProvinceSelect() {
        $where = [];
        $where[] = ["code", "like", "%0000"];
        return json(Db::table("un_common_location")->where($where)->select()->toArray());
    }

    /**
     * 通过省份编码查找所有市区
     * @return type
     */
    public function findCityByProvinceSelect() {
        $code = $this->request->param("code");
        $province_prefix = substr($code, 0, 2);
        $where[] = ["code", "like", $province_prefix . "%00"];
        $where[] = ["code", "<>", $code];
        return json(Db::table("un_common_location")->where($where)->select()->toArray());
    }

    /**
     * 通过市级编码查找所有区县
     * @return type
     */
    public function findCountyByCitySelect() {
        $code = $this->request->param("code");
        $city_prefix = substr($code, 0, 4);
        $where[] = ["code", "like", $city_prefix . "%"];
        $where[] = ["code", "<>", $code];
        return json(Db::table("un_common_location")->where($where)->select()->toArray());
    }

    /**
     * 通过地区编码找下级地区，比如传入35就找35**00，传入3505就找3505**
     * @return type
     */
    public function findChildAreaByCode() {
        $param = $this->request->param();
        $code = $param["code"];
        $where[] = ["code", "like", str_pad($code . "%%", 6, 0)];
        $where[] = ["code", "<>", str_pad($code . "00", 6, 0)];
        if (isset($param["no"])) {
            $where[] = ["code", "<>", $param["no"]];
        }
        return json(Db::table("un_common_location")->where($where)->select()->toArray());
    }

}
