<?php namespace App\Models;

use CodeIgniter\Model;

class DevManageModel extends Model
{
    protected $DBGroup = 'default';                               //默认连接的数据库
    protected $table      = 'first_category';                     //指定模型要使用的数据库表
    protected $returnType     = 'array';

    protected $allowedFields = [                                 //允许更改，写入数据库的字段
        "category_name",
        "detail",
        "create_time",
        "first_category_id",
    ];

///////////////////////////////////////////////////  分类管理区域  ////////////////////////////////////////////////////////////////
    /****************************************************
     *       向第一分类表插入数据
     /****************************************************/
    public function insertFirstCategoryData($data){
        $maxIdObj = $this->db->query('select Max(id) AS id from first_category');
        $maxIdResult = $maxIdObj->getResultArray();
        $id = $maxIdResult[0]["id"] + 1;
        $sql = 'insert into first_category (id,category_name,detail,create_time) values ("'.$id.'","'
        .$data["category_name"].'","'
        .$data["detail"].'","'
        .$data["create_time"].'")';
        $insertObj = $this->db->query($sql);
    }

    /****************************************************
     *       删除一条第一分类数据
     /****************************************************/
     public function delFirstCategoryById($id){
        $sql = "delete from second_category where first_category_id=".$id;
        $this->db->query($sql);
        $sql = "delete from first_category where id=".$id;
        $this->db->query($sql);
     }

    /****************************************************
     *       删除一条第二级分类数据
     /****************************************************/
     public function delSecondCategoryById($id){
        $sql = "delete from second_category where id=".$id;
        $this->db->query($sql);
     }

    /****************************************************
     *       向第二分类表插入数据
     /****************************************************/
    public function insertSecondCategoryData($data){
        $sql = 'insert into second_category (category_name,detail,first_category_id,create_time) values ("'
        .$data["category_name"].'","'
        .$data["detail"].'","'
        .$data["first_category_id"].'","'
        .$data["create_time"].'")';
        $insertObj = $this->db->query($sql);
    }

    /****************************************************
     *       获取第一级分类表的所有数据
     /****************************************************/
    public function getAllFirstCategory(){
         $sql = "select * from first_category";
         $allFirstCategoryObj = $this->db->query($sql);
         $result = $allFirstCategoryObj->getResultArray();
         return $result;
     }

    /****************************************************
     *       获取第二级分类表的数据通过一级分类id
     /****************************************************/
    public function getSecondCategoryByFirstCategoryId($firstCategoryId=1){
        $sql = "select * from second_category where first_category_id='".$firstCategoryId."'";
        $allFirstCategoryObj = $this->db->query($sql);
        $result = $allFirstCategoryObj->getResultArray();
        return $result;
    }

///////////////////////////////////////////////////  设备管理区域  ////////////////////////////////////////////////////////////////
    /****************************************************
     *       插入一条设备
     /****************************************************/
     public function insertDevice($data){
         $sql = 'insert into devices (dev_name, dev_code, dev_brand, dev_model, dev_system,
         dev_version, dev_color, add_time, dev_status, dev_delete, first_category_id, second_category_id,
         org_section_id, dev_detail, dev_feature) values ("'
         .$data["dev_name"].'","'
         .$data["dev_code"].'","'
         .$data["dev_brand"].'","'
         .$data["dev_model"].'","'
         .$data["dev_system"].'","'
         .$data["dev_version"].'","'
         .$data["dev_color"].'","'
         .$data["add_time"].'",'
         .$data["dev_status"].','
         .$data["dev_delete"].','
         .$data["first_category_id"].','
         .$data["second_category_id"].','
         .$data["org_section_id"].',"'
         .$data["dev_detail"].'","'
         .$data["dev_feature"].'")';

         $insertObj = $this->db->query($sql);
     }

    /****************************************************
     *       更新设备信息
     /****************************************************/
     public function updateDevice($data){
        $sql = 'update devices set dev_name="'
        .$data["dev_name"].'",dev_code="'
        .$data["dev_code"].'",dev_brand="'
        .$data["dev_brand"].'",dev_model="'
        .$data["dev_model"].'",dev_system="'
        .$data["dev_system"].'",dev_version="'
        .$data["dev_version"].'",dev_color="'
        .$data["dev_color"].'",update_time="'
        .$data["update_time"].'",dev_status='
        .$data["dev_status"].',dev_delete='
        .$data["dev_delete"].',first_category_id='
        .$data["first_category_id"].',second_category_id='
        .$data["second_category_id"].',org_section_id='
        .$data["org_section_id"].',dev_detail="'
        .$data["dev_detail"].'",dev_feature="'
        .$data["dev_feature"].'"'
        .' where id="'.$data["id"].'"';
        $obj = $this->db->query($sql);
    }

    /****************************************************
     *       分页获取设备信息
     /****************************************************/
     public function getDevicesAllInfoByPage($data){
        $sql = 'select t1.*,
        t2.category_name,
        t3.org_section_name,
        t4.id as borrow_id,t4.borrower,t4.user_id,t4.borrow_status
        from devices t1 
        left join first_category  t2 on t1.first_category_id = t2.id
        left join org_section t3 on t1.org_section_id = t3.id
        left join dev_borrow t4 on t1.borrow_id = t4.id limit '.(($data["page"] - 1) * $data["perPage"]).','.$data["perPage"];
        $allFirstCategoryObj = $this->db->query($sql);
        $result = $allFirstCategoryObj->getResultArray();
        return $result;
    }

    /****************************************************
     *       获取单条设备信息
     /****************************************************/
     public function getDeviceInfoById($id){
        $sql = 'select t1.*,
        t2.category_name,
        t3.org_section_name,
        t4.id as borrow_id,t4.borrower,t4.user_id,t4.borrow_status
        from devices t1 
        left join first_category  t2 on t1.first_category_id = t2.id
        left join org_section t3 on t1.org_section_id = t3.id 
        left join dev_borrow t4 on t1.borrow_id = t4.id where t1.id='.$id;
        $allFirstCategoryObj = $this->db->query($sql);
        $result = $allFirstCategoryObj->getResultArray();
        return $result;
    }

    /****************************************************
     *       获取所有的设备数
     /****************************************************/
     public function getTatalDeviceNum(){
        $sql = 'select count(*) as total from devices';
        $obj = $this->db->query($sql);
        $result = $obj->getResultArray();
        return $result;
     }

    /****************************************************
     *       获取单条设备信息
     /****************************************************/
     public function getDevInfoById($id){
        $sql = "select * from devices where id=".$id;
        $obj = $this->db->query($sql);
        $result = $obj->getResultArray();
        return $result;
     }

    /****************************************************
     *       删除设备
     /****************************************************/
     public function delDevById($id){
        $sql = "delete from devices where id=".$id;
        $this->db->query($sql);
    }

///////////////////////////////////////////////////  图片操作区域  ////////////////////////////////////////////////////////////////
    /****************************************************
     *       添加设备图片
     /****************************************************/
     public function insertDevImg($data){
        $sql = 'insert into dev_img (dev_id,create_time,img_ori,img_small) values ('
        .$data["devId"].',"'
        .$data["create_time"].'","'
        .$data["img_ori"].'","'
        .$data["img_small"].'")';
        $this->db->query($sql);
    }

    /****************************************************
     *       根据设备id获取设备图片
     /****************************************************/
    public function getDevImgsById($id){
        $sql = "select * from dev_img where dev_id=".$id;
        $obj = $this->db->query($sql);
        $result = $obj->getResultArray();
        return $result;
    }

    /****************************************************
     *       根据图片id获取设备图片信息
     /****************************************************/
     public function getImgInfoById($id){
        $sql = "select * from dev_img where id=".$id;
        $obj = $this->db->query($sql);
        $result = $obj->getResultArray();
        return $result;
    }

    /****************************************************
     *       根据设备id删除指定设备图片
     /****************************************************/
     public function delImgByIds($data){
        $sql = "delete from dev_img where dev_id=".$data["dev_id"]." and id=".$data["img_id"];
        $this->db->query($sql);
    }

///////////////////////////////////////////////////  设备签借区域  ////////////////////////////////////////////////////////////////
    /****************************************************
     *       添加设备签借信息
     /****************************************************/
     public function insertBorrowInfo($data){
        $sql = 'insert into dev_borrow (dev_id,user_id,borrower,comment,borrower_time,borrow_status) values ('
        .$data["dev_id"].','
        .$data["user_id"].',"'
        .$data["borrower"].'","'
        .$data["comment"].'","'
        .$data["borrower_time"].'",'
        .$data["borrow_status"].')';
        $this->db->query($sql);
    }

    /****************************************************
     *       根据用户名和设备id查询签借信息
     /****************************************************/
     public function searchBorrowInfoByBorrowerAndDevid($data){
        $sql = 'select * from dev_borrow where borrower="'.$data["borrower"].'" and dev_id='.$data["dev_id"];
        $obj = $this->db->query($sql);
        $result = $obj->getResultArray();
        return $result;
     }

     /****************************************************
     *       根据设备id查询签借信息
     /****************************************************/
     public function searchBorrowerInfoByDevid($data){
        $sql = 'select * from dev_borrow where borrower="'.$data["borrower"].'" and dev_id='.$data["dev_id"];
        $obj = $this->db->query($sql);
        $result = $obj->getResultArray();
        return $result;
     }
}