package com.bocloud.ims.repository.resource.azure.slb;

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.azure.slb.InboundNatRule;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

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

/**
 * @author weiwei
 */
@Component("inboundNatRuleRepository")
public class InboundNatRuleRepository extends BasicGenericDao<InboundNatRule, Long> {

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

    public List<InboundNatRule> list(int page, int rows, List<Param> params, Map<String, String> sorter)
            throws Exception {
        String sql = "SELECT a.*,f.name AS front_end_name ,p.ip_address AS front_end_ip_address FROM inbound_nat_rule a " +
                "LEFT JOIN front_end f ON a.front_end_id = f.id " +
                "LEFT JOIN public_ip_address p ON f.public_ip_id = p.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(InboundNatRule.class, sql, paramMap);
    }

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

    public int count(List<Param> params) throws Exception {
        String sql = "SELECT COUNT(1) FROM inbound_nat_rule a  " +
                "LEFT JOIN front_end f ON a.front_end_id = f.id " +
                "LEFT JOIN public_ip_address p ON f.public_ip_id = p.id " +
                "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 inbound_nat_rule 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 InboundNatRule query(Long id) throws Exception {
        String sql = "select a.* from inbound_nat_rule a where a.is_deleted = 0 and a.id = :id";
        Map<String, Object> param = MapTools.simpleMap(Common.ID, id);
        List<InboundNatRule> list = this.list(InboundNatRule.class, sql, param);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

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

    public InboundNatRule getByName(Long vendorId, Long slbId, String name) throws BoCloudDBException {
        String sql = "select a.* from inbound_nat_rule a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.name = :name and a.slb_id = :slbId ";
        Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
        paramMap.put(Common.NAME, name);
        paramMap.put("slbId", slbId);
        List<InboundNatRule> list = this.list(InboundNatRule.class, sql, paramMap);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public List<InboundNatRule> listSlbId(Long slbId) throws BoCloudDBException {
        String sql = "select a.* from inbound_nat_rule a where a.is_deleted = 0 and a.slb_id = :slbId";
        Map<String, Object> paramMap = MapTools.simpleMap("slbId", slbId);
        return this.list(InboundNatRule.class, sql, paramMap);
    }

    public boolean removeSlbId(Long slbId, Long userId) throws BoCloudDBException {
        String sql = "update inbound_nat_rule set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where slb_id = :slbId";
        Map<String, Object> params = MapTools.simpleMap("slbId", slbId);
        params.put("gmtModify", new Date());
        params.put(Common.MENDER_ID, userId);
        return this.execute(sql, params) > 0;
    }

}
