package com.bocloud.ims.repository.resource.hmc;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.JdbcGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.hmc.HmcProfile;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * hmcProfileDao接口实现
 *
 * @author ShaolongTan
 */
@Deprecated
@Repository("hmcProfileRepository")
public class HmcProfileRepository extends JdbcGenericDao<HmcProfile, Long> {

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

    public List<HmcProfile> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
        String sql = "select a.*, b.name create_user_name " +
                "from hmc_profile a left join users b on a.creator_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(HmcProfile.class, sql, paramMap);
    }

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

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

    public List<HmcProfile> listByPartitionId(Long partitionId) {
        Objects.requireNonNull(partitionId, "partitionId");
        String sql = "select a.* from hmc_profile a where a.is_deleted = 0 and a.partition_id = :partitionId";
        Map<String, Object> params = new HashMap<>();
        params.put("partitionId", partitionId);

        return this.list(HmcProfile.class, sql, params);
    }

    public List<HmcProfile> listByPartitionIdIncludeDeleted(Long partitionId) {
        Objects.requireNonNull(partitionId, "partitionId");
        String sql = "select a.* from hmc_profile a where a.partition_id = :partitionId";
        Map<String, Object> params = new HashMap<>();
        params.put("partitionId", partitionId);

        return this.list(HmcProfile.class, sql, params);
    }

    public HmcProfile getByUuid(String uuid) {
        String sql = "select a.id,a.name,a.uuid from hmc_profile a where a.is_deleted = 0 and a.uuid = :uuid";
        Map<String, Object> params = MapTools.simpleMap("uuid", uuid);
        List<HmcProfile> profiles = this.list(HmcProfile.class, sql, params);
        if (profiles.isEmpty()) {
            return null;
        }
        return profiles.get(0);
    }

    public HmcProfile queryById(Long id) {
        String sql = "select a.*, b.hmc_partition_id from hmc_profile a, hmc_partition b where (a.partition_id = b.id) and a.is_deleted = 0 and a.id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        List<HmcProfile> profiles = this.list(HmcProfile.class, sql, params);
        if (profiles.isEmpty()) {
            return null;
        }
        return profiles.get(0);
    }

    public HmcProfile getActiveProfile(Long partitionId) {
        String sql = "select a.* from hmc_profile a where a.is_deleted = 0 and a.partition_id = :partitionId and a.is_active_profile = true";
        Map<String, Object> params = MapTools.simpleMap("partitionId", partitionId);
        List<HmcProfile> profiles = this.list(HmcProfile.class, sql, params);
        if (profiles.isEmpty()) {
            return null;
        }
        return profiles.get(0);
    }

    public HmcProfile getDefaultProfile(Long partitionId) {
        String sql = "select a.* from hmc_profile a where a.is_deleted = 0 and a.partition_id = :partitionId and a.is_default_profile = true";
        Map<String, Object> params = MapTools.simpleMap("partitionId", partitionId);
        List<HmcProfile> profiles = this.list(HmcProfile.class, sql, params);
        if (profiles.isEmpty()) {
            return null;
        }
        return profiles.get(0);
    }

    public HmcProfile getIsLastActived(Long partitionId) {
        String sql = "select a.* from hmc_profile a where a.is_deleted = 0 and a.partition_id = :partitionId and a.is_last_activated = true";
        Map<String, Object> params = MapTools.simpleMap("partitionId", partitionId);
        List<HmcProfile> profiles = this.list(HmcProfile.class, sql, params);
        if (profiles.isEmpty()) {
            return null;
        }
        return profiles.get(0);
    }

    public boolean remove(Long id, Long userId) {
        String sql = "update hmc_profile 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 int count(List<Param> params) {
        String sql = "select count(1) from hmc_profile 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();
    }
}
