package com.bocloud.ims.repository.resource;

import com.bocloud.cmp.model.Scope;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.Common;
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.Rack;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.*;

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

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

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

    public List<Rack> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select a.*,b.name roomname,c.name dcname,c.id as dc_id from rack a left join room b on a.room_id = b.id left join data_center c on b.dc_id = c.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(Rack.class, sql, paramMap);
    }

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select a.id,a.name from rack 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<Rack> racks = this.list(Rack.class, sql, paramMap);
        List<SimpleBean> beans = new ArrayList<SimpleBean>();
        for (Rack rack : racks) {
            beans.add(new SimpleBean(rack.getId(), rack.getName()));
        }
        return beans;
    }

    public boolean releaseRoom(Long id, Long userId) throws Exception {
        String sql = "update rack set room_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 belongRoom(Long id, Long roomId, Long userId) throws Exception {
        String sql = "update rack set room_id = :roomId , gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        params.put("roomId", roomId);
        params.put("menderId", userId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

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

    public List<Rack> list(Long roomId, Boolean simple) {
        String sql = "select * from rack where is_deleted = 0 ";
        if (null != simple && simple) {
            sql = "select id,name,gmt_create,gmt_modify,creator_id,mender_id,owner_id,remark,props,descs,specs,vendor,product_date,product_sn,warranty,type,slot_amount,material,room_id,is_deleted,unit,axis_x,axis_y,status from rack where is_deleted = 0 ";
        }
        Map<String, Object> paramMap = new HashMap<>(8);
        if (null != roomId && 0L != roomId) {
            sql += " and room_id = :roomId";
            paramMap.put("roomId", roomId);
        }
        return this.list(Rack.class, sql, paramMap);
    }

    public List<Rack> listFree(Long roomId) throws Exception {
        String sql = "select * from rack where is_deleted = 0 and (room_id = :roomId or room_id is null)";
        Map<String, Object> params = MapTools.simpleMap("roomId", roomId);
        return this.list(Rack.class, sql, params);
    }

    public int count(List<Param> params) throws Exception {
        String sql = "select count(1) from rack 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 rack 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 exists(List<Param> params) throws Exception {
        String sql = "select * from rack a where a.is_deleted = 0";
        sql = QueryBuilder.build(sql, params, null, "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<Rack> list = this.list(Rack.class, sql, paramMap);
        if (list.isEmpty()) {
            return false;
        }
        return true;
    }

    public Scope range(Long roomId) throws Exception {
        String sql = "select max(a.axis_x) rowNum,max(a.axis_y) colNum from rack a where a.is_deleted = 0 and a.room_id = :roomId";
        Map<String, Object> params = MapTools.simpleMap("roomId", roomId);
        List<Map<String, Object>> list = this.list(sql, params);
        if (list.isEmpty()) {
            return null;
        }
        Map<String, Object> map = list.get(0);
        return new Scope(Integer.parseInt(map.get(Common.ROW).toString()),
                Integer.parseInt(map.get(Common.COL).toString()));
    }

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

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

    public List<Rack> queryByRoomId(Long roomId) throws Exception {
        String sql = "select * from rack where room_id = :roomId and is_deleted = 0";
        Map<String, Object> params = MapTools.simpleMap("roomId", roomId);
        return this.list(Rack.class, sql, params);
    }

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