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

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.account.BatchAccount;
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;

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: BatchAccountRepository
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2020/7/24 14:34
 */
@Repository("batchAccountRepository")
public class BatchAccountRepository extends BasicGenericDao<BatchAccount, Long> {
    public BatchAccountRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate) {
        super(jdbcTemplate, npJdbcTemplate);
    }

    public List<BatchAccount> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws BoCloudDBException {
        String sql = "SELECT v.*,rg.name AS resource_group_name , sa.name AS storage_account_name FROM batch_account v " +
                "LEFT JOIN resource_group rg ON v.resource_group_id = rg.id " +
                "LEFT JOIN storage_account sa ON v.storage_account_id = sa.id " +
                "WHERE v.is_deleted = 0 ";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "v");
        return this.list(BatchAccount.class, sql);
    }

    public int count(List<Param> params) throws BoCloudDBException {
        String sql = "SELECT count(1) FROM batch_account v " +
                "LEFT JOIN resource_group rg ON v.resource_group_id = rg.id " +
                "LEFT JOIN storage_account sa ON v.storage_account_id = sa.id " +
                "WHERE v.is_deleted = 0 ";
        sql = QueryBuilder.buildRaw(sql, params, null, "v");
        Map<String, Object> param = QueryBuilder.getParam(params);
        return this.countQuery(sql, param).intValue();
    }

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

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

    public boolean removeByVid(Long vendorId, String regionId, Long userId) throws BoCloudDBException {
        String sql = "update batch_account set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where vendor_id = :vendorId and region_id = :regionId ";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        params.put(Common.REGIONID, regionId);
        params.put("gmtModify", new Date());
        params.put(Common.MENDER_ID, userId);
        return this.execute(sql, params) > 0;
    }

    public List<BatchAccount> listByVendorId(long vendorId) throws BoCloudDBException {
        String sql = "select * from batch_account where is_deleted = 0 and vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        return this.list(BatchAccount.class, sql, params);
    }

    public List<BatchAccount> listByVendorId(long vendorId, String regionId) throws BoCloudDBException {
        String sql = "select * from batch_account where is_deleted = 0 and vendor_id = :vendorId and region_id = :regionId";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        params.put(Common.REGIONID, regionId);
        return this.list(BatchAccount.class, sql, params);
    }

    public BatchAccount query(long id) throws BoCloudDBException {
        String sql = "SELECT v.*,rg.name AS resource_group_name , sa.name AS storage_account_name FROM batch_account v " +
                "LEFT JOIN resource_group rg ON v.resource_group_id = rg.id " +
                "LEFT JOIN storage_account sa ON v.storage_account_id = sa.id " +
                "WHERE v.is_deleted = 0 and v.id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        List<BatchAccount> list = this.list(BatchAccount.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public BatchAccount getByUuid(String uuid) throws BoCloudDBException {
        String sql = "select a.* from batch_account a where a.uuid = :uuid ";
        Map<String, Object> params = new HashMap<>();
        params.put(Common.UUID, uuid);
        List<BatchAccount> list = this.list(BatchAccount.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public void remove(long id) throws BoCloudDBException {
        String sql = "update batch_account set is_deleted = 1 where id = :id ";
        Map<String, Object> params = new HashMap<>();
        params.put(Common.ID, id);
        execute(sql, params);
    }

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

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

    public boolean removeByGroupId(Long vendorId, Long userId, Long resourceGroupId) throws BoCloudDBException {
        String sql = "update batch_account set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where vendor_id = :vendorId and resource_group_id = :resourceGroupId ";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        params.put("resourceGroupId", resourceGroupId);
        params.put("gmtModify", new Date());
        params.put(Common.MENDER_ID, userId);
        return this.execute(sql, params) > 0;
    }

}