<?php

namespace Home\Controller;

use Think\Controller;


class SiteManagementController extends ComController
{
    //站点列表查询
    public function getSiteList()
    {
        $Data = I('');
        if ($_SESSION['permissionLabel'] != 1) {
            $res['msg'] = '操作失败，无此权限请联系管理员';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }

        if (empty($_SESSION['phone'])) {
            $res['msg'] = '请重新进入公众号';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        $where = '';
        if (!empty($Data['siteName'])) {
            $where .= " and b.site_name like '%" . $Data['siteName'] . "%'";
        }
        if (!empty($Data['route_name'])) {
            $where .= " and c.route_name like '%" . $Data['route_name'] . "%'";
        }

        if (!empty($Data['id'])) {
            $where .= " and b.id = " . $Data['id'];
        }

        if(!empty($Data['currentPage'])&&!empty($Data['size'])){
            $start_number = ($Data['currentPage']-1)*$Data['size'];
            $limit = " limit ".$start_number.",".$Data['size'];
        }

//        print_r("select b.id,b.site_name as siteName,b.site_address as address,b.company_code as companyCode,b.user_name as userName,b.phone,
//                            b.station_area_id,b.route_id,b.installed_capacity,b.rheological_change,b.longitude,b.latitude,b.region as region,b.create_time,
//                            b.creator,b.enable,c.route_name from personnel as a join site as b on a.site_id=b.id join route as c on b.route_id=c.id where b.enable=1 and a.personnel_phone = " . $_SESSION['phone'] . $where . "
//                             order by b.id desc");
        $R = M()->query("select count(*) as sl from personnel as a join site as b on a.site_id=b.id join route as c on b.route_id=c.id where b.enable=1 and a.personnel_phone = " . $_SESSION['phone'] . $where . "
                             order by b.id desc");
        $data = M()->query("select b.id,b.site_name as siteName,b.site_address as address,b.company_code as companyCode,b.user_name as userName,b.phone,
                            b.station_area_id,b.route_id,b.installed_capacity,b.rheological_change,b.longitude,b.latitude,b.region as region,b.create_time,
                            b.creator,b.enable,c.route_name,b.spare_user_name,b.spare_phone  from personnel as a join site as b on a.site_id=b.id join route as c on b.route_id=c.id where b.enable=1 and a.personnel_phone = " . $_SESSION['phone'] . $where . "
                             order by b.id desc".$limit);
//        $data = M()->query("select * from wj_power_monitoring_site where 1=1 " . $where."  order by id desc");
        $res['msg'] = '操作成功';
        $res['flag'] = true;
        $res['data'] = $data;
        $res['total'] = $R[0]['sl'];
        $res['currentPage'] = $Data['currentPage'];
        $res['size'] = $Data['size'];
        echo json_encode($res);
    }

    //站点新增
    public function setAddSite()
    {
        $Data = I('');
        if ($_SESSION['permissionLabel'] != 1) {
            $res['msg'] = '操作失败，无此权限请联系管理员';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        if (empty($_SESSION['phone'])) {
            $res['msg'] = '请重新进入公众号';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }

        $name_data = M()->query("select personnel_name from personnel where personnel_phone='" . $_SESSION['phone'] . "' limit 0,1");
        if (!empty($name_data[0]['personnel_name'])) {
            $arr['creator'] = $name_data[0]['personnel_name'];
            $arr1['creater'] = $name_data[0]['personnel_name'];
        } else {
            $arr['creator'] = $_SESSION['phone'];
            $arr1['creater'] = $_SESSION['phone'];
        }

        $arr['spare_user_name'] = $Data['spare_user_name'];
        $arr['spare_phone'] = $Data['spare_phone'];

        $arr['site_name'] = $Data['siteName'];
        $arr['user_name'] = $Data['userName'];
        $arr['phone'] = $Data['phone'];
        $arr['site_address'] = $Data['address'];
        $arr['station_area_id'] = '';
        $arr['route_id'] = $Data['route_id'];
        $arr['installed_capacity'] = $Data['installed_capacity'];
        $arr['rheological_change'] = $Data['rheological_change'];
        $arr['longitude'] = $Data['longitude'];
        $arr['latitude'] = $Data['latitude'];
        $arr['region'] = $Data['region'];
        $arr['create_time'] = date('Y-m-d H:i:s');
        $arr['enable'] = 1;
        $arr['company_code'] = "11111";
//        print_r($arr);
        if (!isset($arr['site_name']) || !isset($arr['site_address']) || !isset($arr['user_name']) || !isset($arr['phone'])
            || !isset($arr['route_id']) || !isset($arr['installed_capacity']) || !isset($arr['rheological_change']) || !isset($arr['longitude'])
            || !isset($arr['latitude']) || !isset($arr['region'])) {
            $res['msg'] = '操作失败，缺少关键参数';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
//        print_r($arr);
        $R = M("site")->add($arr);
        $arr1['personnel_name'] = '';
        $arr1['personnel_phone'] = $_SESSION['phone'];
        $arr1['site_id'] = $R;
        $arr1['personnel_function'] = 1;
        $arr1['alarm_setting'] = '';
        $arr1['create_time'] = date('Y-m-d H:i:s');
        $arr1['enable'] = 1;
        $R1 = M("personnel")->add($arr1);

        if ($R) {
            $res['msg'] = '操作成功';
            $res['flag'] = true;
            echo json_encode($res);
        } else {
            $res['msg'] = '操作失败';
            $res['flag'] = false;
            echo json_encode($res);
        }
    }

    //站点编辑
    public function setEditSite()
    {
        $Data = I('');
        if ($_SESSION['permissionLabel'] != 1) {
            $res['msg'] = '操作失败，无此权限请联系管理员';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        if (!isset($Data['id'])) {
            $res['msg'] = '操作失败，缺少关键参数';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        $where['id'] = $Data['id'];
        if (isset($Data['siteName'])) {
            $data['site_name'] = $Data['siteName'];
        }
        if (isset($Data['address'])) {
            $data['site_address'] = $Data['address'];
        }
        if (isset($Data['userName'])) {
            $data['user_name'] = $Data['userName'];
        }
        if (isset($Data['phone'])) {
            $data['phone'] = $Data['phone'];
        }
        if (isset($Data['station_area_id'])) {
            $data['station_area_id'] = $Data['station_area_id'];
        }
        if (isset($Data['route_id'])) {
            $data['route_id'] = $Data['route_id'];
        }
        if (isset($Data['installed_capacity'])) {
            $data['installed_capacity'] = $Data['installed_capacity'];
        }
        if (isset($Data['rheological_change'])) {
            $data['rheological_change'] = $Data['rheological_change'];
        }
        if (isset($Data['longitude'])) {
            $data['longitude'] = $Data['longitude'];
        }
        if (isset($Data['latitude'])) {
            $data['latitude'] = $Data['latitude'];
        }
        if (isset($Data['region'])) {
            $data['region'] = $Data['region'];
        }
        if (isset($Data['spare_user_name'])) {
            $data['spare_user_name'] = $Data['spare_user_name'];
        }
        if (isset($Data['spare_phone'])) {
            $data['spare_phone'] = $Data['spare_phone'];
        }

        $R = M("site")->where($where)->save($data);
        if ($R) {
            $res['msg'] = '操作成功';
            $res['flag'] = true;
            echo json_encode($res);
        } else {
            $res['msg'] = '操作失败';
            $res['flag'] = false;
            echo json_encode($res);
        }
    }

    //站点删除
    public function setDelSite()
    {
        $Data = I('');
        if ($_SESSION['permissionLabel'] != 1) {
            $res['msg'] = '操作失败，无此权限请联系管理员';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        if (!isset($Data['id'])) {
            $res['msg'] = '操作失败，缺少关键参数';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        $where['id'] = $Data['id'];
        $where1['site_id'] = $Data['id'];
        $data['enable'] = 0;
        $R = M("site")->where($where)->save($data);
        M("personnel")->where($where1)->save($data);
//        $R = M("site")->where($where)->delete();
//        M("personnel")->where($where1)->delete();
        if ($R) {
            $res['msg'] = '操作成功';
            $res['flag'] = true;
            echo json_encode($res);
        } else {
            $res['msg'] = '操作失败';
            $res['flag'] = false;
            echo json_encode($res);
        }
    }
}