<?php

namespace app\admin\controller\nhc;

use app\admin\common\AdminController;
use app\common\api\DictApi;
use app\common\api\UploadApi;
use think\facade\Db;

/**
 * 医共体管理
 */
class MedicalCommunity extends AdminController {

    public function index() {
        return view("");
    }

    public function list() {
        $order = trim($this->request->param("order")) ?: "desc";
        $offset = trim($this->request->param("offset")) ?: 0;
        $limit = trim($this->request->param("limit")) ?: 10;
        $name = trim($this->request->param("name"));

        $where = [];
        $where[] = ["status", "<>", 3];
        if ($name) {
            $where[] = ["name", "like", "%" . $name . "%"];
        }

        $count = Db::table("nhc_medical_community")->where($where)->count();
        $rows = Db::table("nhc_medical_community")->where($where)->limit($offset, $limit)->order("num asc,createTime " . $order)->select()->toArray();

        return ["total" => $count, "rows" => $rows];
    }

    public function add() {
        return view("edit");
    }

    public function edit() {
        $id = \StrUtil::getRequestDecodeParam($this->request, 'id');
        $info = Db::table("nhc_medical_community")->where("id", $id)->find();
        return view("edit", ["row" => $info]);
    }

    public function upsert() {
        $response_obj = new \StdClass();
        $response_obj->code = 500;
        $id = \StrUtil::getRequestDecodeParam($this->request, 'id');
        $where = [];
        if ($id) {
            $info = Db::table("nhc_medical_community")->where("id", $id)->find();
            if (!$info) {
                $response_obj->msg = '找不到记录';
                return \StrUtil::back($response_obj, 'MedicalCommunityInfo.callBack');
            }
            $where[] = ["id", "<>", $id];
        }

        $name = trim($this->request['name']);
        if (\StrUtil::isEmpOrNull($name)) {
            $response_obj->msg = '医共体名称不能为空';
            return \StrUtil::back($response_obj, 'MedicalCommunityInfo.callBack');
        }
        $where[] = ["name", "=", $name];
        $repeat = Db::table("nhc_medical_community")->where($where)->find();

        if ($repeat) {
            $response_obj->msg = '医共体名称已经被使用';
            return \StrUtil::back($response_obj, 'MedicalCommunityInfo.callBack');
        }

        $num = trim($this->request['num']);
        if (\StrUtil::isNotEmpAndNull($num) && !is_numeric($num)) {
            $response_obj->msg = '序号只能是数字';
            return \StrUtil::back($response_obj, 'MedicalCommunityInfo.callBack');
        }
        $status = trim($this->request['status']);
        if (\StrUtil::isEmpOrNull($status)) {
            $response_obj->msg = '启用状态不能为空';
            return \StrUtil::back($response_obj, 'MedicalCommunityInfo.callBack');
        }
        try {
            $data["name"] = $name;
            $data["status"] = $status;
            $data["num"] = $num;
            $data["description"] = \StrUtil::getRequestDecodeParam($this->request, "description");
            if ($id) {
                $data["id"] = $id;
                $data["updateTime"] = date("Y-m-d H:i:s");
                $data["updateUser"] = $this->user['uid'];
                Db::table("nhc_medical_community")->save($data);
            } else {
                $data["createTime"] = date("Y-m-d H:i:s");
                $data["createUser"] = $this->user['uid'];
                Db::table("nhc_medical_community")->insert($data);
            }
            $response_obj->code = 200;
            $response_obj->msg = '修改成功';
            return \StrUtil::back($response_obj, "MedicalCommunityInfo.callBack");
        } catch (\Exception $e) {
            $response_obj->msg = $e->getMessage();
            return \StrUtil::back($response_obj, "MedicalCommunityInfo.callBack");
        }
    }

    public function delete() {
        $id = \StrUtil::getRequestDecodeParam($this->request, 'id');
        if (\StrUtil::isEmpOrNull($id)) {
            return json(['code' => 500, 'msg' => '缺少参数']);
        }
        $data["id"] = $id;
        $data["status"] = 3;
        $data["updateTime"] = date("Y-m-d H:i:s");
        $data["updateUser"] = $this->user["uid"];
        try {
            Db::table("nhc_medical_community")->save($data);
            return json(['code' => 200, 'msg' => "删除成功"]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage()]);
        }
    }

}
