package com.bocloud.ims.repository.resource;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.ListTool;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.Room;
import com.google.common.collect.Lists;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 机房数据访问
 *
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 */
@Repository("roomRepository")
public class RoomRepository extends BasicGenericDao<Room, Long> {

    public RoomRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate) {
        super(jdbcTemplate, npJdbcTemplate);
    }

    public Room query(Long id) throws Exception {
        String sql = "select a.*,b.name dcname from room a left join data_center b on a.dc_id = b.id where a.is_deleted = 0 and a.id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        List<Room> list = this.list(Room.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public List<Room> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select a.*,b.name dcname from room a left join data_center b on a.dc_id = b.id where a.is_deleted = 0";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.list(Room.class, sql, paramMap);
    }

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select a.id,a.name from room a where a.is_deleted = 0";
        sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<Room> rooms = this.list(Room.class, sql, paramMap);
        List<SimpleBean> beans = Lists.newArrayList();
        for (Room room : rooms) {
            beans.add(new SimpleBean(room.getId(), room.getName()));
        }
        return beans;
    }

    public boolean release(Long id, Long userId) throws Exception {
        String sql = "update room set dc_id = null , gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        params.put("menderId", userId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

    public boolean belong(Long id, Long dcId, Long userId) throws Exception {
        String sql = "update room set dc_id = :dcId , gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        params.put("dcId", dcId);
        params.put("menderId", userId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

    public List<Room> listFree() throws Exception {
        String sql = "select * from room where is_deleted = 0 and dc_id is null";
        return this.list(Room.class, sql);
    }

    public List<Room> list(Long dcId, Boolean simple) {
        String sql = "select * from room where is_deleted = 0 ";
        if (null != simple && simple) {
            sql = "select id,name,gmt_create,gmt_modify,creator_id,mender_id,owner_id,status,props,remark,location_sn,contact_phone,contacter,type,commission_date,total_area,dc_id,is_deleted,row_num,col_num,vpc_id,vdc_id from room where is_deleted = 0";
        }
        Map<String, Object> paramMap = new HashMap<>(8);
        if (null != dcId && 0L != dcId) {
            sql += " and dc_id = :dcId";
            paramMap.put("dcId", dcId);
        }
        return this.list(Room.class, sql, paramMap);
    }

    public int count(List<Param> params) throws Exception {
        String sql = "select count(1) from room a where a.is_deleted = 0";
        sql = QueryBuilder.buildRaw(sql, params, null, "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.countQuery(sql, paramMap).intValue();
    }

    public boolean remove(Long id, Long userId) throws Exception {
        String sql = "update room set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        params.put("menderId", userId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

    public boolean isNameRepeat(String name, Long dcId) throws Exception {
        String sql = "select id,name from room where name = :name and dc_id = :dcId and is_deleted = 0 ";
        Map<String, Object> paramMap = MapTools.simpleMap("name", name);
        paramMap.put("dcId", dcId);
        List<Room> list = this.list(Room.class, sql, paramMap);
        if (list.isEmpty()) {
            return false;
        }
        return true;
    }

    public Room queryByName(String name, Long dcId) throws Exception {
        String sql = "select * from room where name = :name and dc_id = :dcId and is_deleted = 0";
        Map<String, Object> params = MapTools.simpleMap("name", name);
        params.put("dcId", dcId);
        List<Room> list = this.list(Room.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public List<Room> queryByDcId(Long dcId) throws Exception {
        String sql = "select * from room where dc_id = :dcId and is_deleted = 0";
        Map<String, Object> params = MapTools.simpleMap("dcId", dcId);
        return this.list(Room.class, sql, params);
    }

    public Room getRoomBean(Long id) throws Exception {
        String sql = "select a.*,b.name dcname from room a left join data_center b on a.dc_id = b.id where a.is_deleted = 0 and a.id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        List<Room> list = this.list(Room.class, sql, params);
        return ListTool.isEmpty(list) ? null : list.get(0);
    }
}
