package com.bocloud.ims.repository.resource;

import com.bocloud.common.exception.BoCloudDBException;
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.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.SangforServerGroup;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

@Repository
public class SangforServerGroupRepository extends BasicGenericDao<SangforServerGroup, Long> {
    public SangforServerGroupRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate) {
        super(jdbcTemplate, npJdbcTemplate);
    }

    public List<SangforServerGroup> listByVid(Long vendorId) throws Exception {
        String sql = "select a.* from sangfor_server_group a where a.is_deleted = 0 and a.vendor_id = :vendorId ";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        return this.list(SangforServerGroup.class, sql, params);
    }

    public List<SangforServerGroup> listByCondition(Long vendorId, String azId) throws Exception {
        String sql = "select a.* from sangfor_server_group a where a.is_deleted = 0 and a.vendor_id = :vendorId ";
        if (StringUtils.isNotEmpty(azId)) {
            sql += " and az_id = :azId ";
        }
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        if (StringUtils.isNotEmpty(azId)) {
            params.put("azId", azId);
        }
        return this.list(SangforServerGroup.class, sql, params);
    }


    public Boolean removeByVid(Long vendorId, Long userId) throws Exception {
        String sql = "update sangfor_server_group set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        params.put("menderId", userId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

    public int stCount(List<Param> params) {
        String sql = "select count(1) from sangfor_server_group a " +
                "  LEFT JOIN sangfor_az sa on a.az_id= sa.uuid " +
                "  LEFT JOIN sangfor_project sp on a.project_id = sp.uuid" +
                "  where a.is_deleted=0";
        sql = QueryBuilder.buildRaw(sql, params, null, "a");
        Map<String, Object> param = QueryBuilder.getParam(params);
        return this.countQuery(sql, param).intValue();
    }


    public List<SangforServerGroup> stList(int page, int rows, List<Param> params, Map<String, String> sorter) throws BoCloudDBException {
        String sql = "select a.*,sa.name az_name,sp.name project_name from sangfor_server_group a " +
                "  LEFT JOIN sangfor_az sa on a.az_id= sa.uuid " +
                "  LEFT JOIN sangfor_project sp on a.project_id = sp.uuid" +
                "  where a.is_deleted=0";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(SangforServerGroup.class, sql);
    }


    public List<SangforServerGroup> listByVendor(Long vendorId) {
        String sql = "select a.* from sangfor_server_group a where a.is_deleted = 0 and a.vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        return this.list(SangforServerGroup.class, sql, params);
    }

    public SangforServerGroup listByParentId(Long vendorId, String uuid) {
        String sql = "select a.* from sangfor_server_group a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.parent_id = :uuid";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        params.put("uuid", uuid);
        List<SangforServerGroup> list = this.list(SangforServerGroup.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public SangforServerGroup listById(Long id) {
        String sql = "select a.* from sangfor_server_group a where a.is_deleted = 0 and a.id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        List<SangforServerGroup> list = this.list(SangforServerGroup.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public Boolean remove(Long id, Long userId) throws Exception {
        String sql = "update sangfor_server_group 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 removeById(Long id) throws Exception {
        String sql = "update sangfor_server_group set is_deleted = true,gmt_modify = :gmtModify where is_deleted = 0 and id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) {
        String sql = "select * from sangfor_server_group a where a.is_deleted = 0";

        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
        List<SangforServerGroup> groupList = this.list(SangforServerGroup.class, sql, paramMap);

        List<SimpleBean> beans = new ArrayList<>(groupList.size());

        for (SangforServerGroup group : groupList) {
            SimpleBean bean = new SimpleBean();
            bean.setId(group.getId());
            bean.setUuid(group.getUuid());
            bean.setName(group.getName());
            beans.add(bean);
        }

        return beans;
    }
}
