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

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.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.azure.blob.Container;
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.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * azure存储容器数据访问
 *
 * @author wangyu
 * @version 5.0.0-SNAPSHOT
 * @since 2021/2/2
 */
@Repository
public class ContainerRepository extends BasicGenericDao<Container, Long> {

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

    public int count(List<Param> params, Long userId, List<Long> projectIds) throws BoCloudDBException {
        StringBuilder builder = new StringBuilder("select count(a.id) from container a where a.is_deleted = 0");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        if (!ListTool.isEmpty(projectIds)) {
            builder.append(" and a.project_id in (:projectIds)");
            paramMap.put("projectIds", projectIds);
        }
        if (null != userId && 0L != userId) {
            builder.append(" and a.user_id = :userId");
            paramMap.put("userId", userId);
        }
        String sql = QueryBuilder.buildRaw(builder.toString(), params, null, "a");
        return this.countQuery(sql, paramMap).intValue();
    }

    public List<Container> list(int page, int rows, List<Param> params, Map<String, String> sorter, Long userId, List<Long> projectIds) throws BoCloudDBException {
        StringBuilder builder = new StringBuilder("select a.*,b.name vendor_name,c.name tenant_name,d.name project_name,e.name storage_account_name from container a left join cloud_vendor b on b.id = a.vendor_id left join tenant c on c.id = a.tenant_id left join project d on d.id = a.project_id left join storage_account e on e.id = a.storage_account_id where a.is_deleted = 0");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        if (!ListTool.isEmpty(projectIds)) {
            builder.append(" and a.project_id in (:projectIds)");
            paramMap.put("projectIds", projectIds);
        }
        if (null != userId && 0L != userId) {
            builder.append(" and a.user_id = :userId");
            paramMap.put("userId", userId);
        }
        String sql = QueryBuilder.buildRaw(builder.toString(), new Pager(page, rows, params, sorter), "a");
        return this.list(Container.class, sql, paramMap);
    }

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter, Long userId, List<Long> projectIds) throws BoCloudDBException {
        String sql = "select a.id,a.name from container a where a.is_deleted = 0";
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        if (!ListTool.isEmpty(projectIds)) {
            sql += " and a.project_id in (:projectIds)";
            paramMap.put("projectIds", projectIds);
        }
        if (null != userId && 0L != userId) {
            sql += " and a.user_id = :userId";
            paramMap.put("userId", userId);
        }
        sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
        List<Container> objects = this.list(Container.class, sql, paramMap);
        return objects.stream().map(object -> new SimpleBean(object.getId(), object.getName())).collect(Collectors.toList());
    }

    public Container query(Long id) throws Exception {
        String sql = "select a.*,b.name vendor_name,c.name tenant_name,d.name project_name,e.name storage_account_name from container a left join cloud_vendor b on b.id = a.vendor_id left join tenant c on c.id = a.tenant_id left join project d on d.id = a.project_id left join storage_account e on e.id = a.storage_account_id where a.is_deleted = 0 and a.id = :id";
        Map<String, Object> param = MapTools.simpleMap(Common.ID, id);
        List<Container> list = this.list(Container.class, sql, param);
        return list.isEmpty() ? null : list.get(0);
    }

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

    public List<Container> listByCondition(Long vendorId, Long storageAccountId) throws BoCloudDBException {
        String sql = "select * from container where is_deleted = 0 and vendor_id = :vendorId ";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        if (null != storageAccountId && 0L != storageAccountId) {
            sql += " and storage_account_id = :storageAccountId";
            params.put("storageAccountId", storageAccountId);
        }
        return this.list(Container.class, sql, params);
    }

    public boolean removeByAccountId(Long storageAccountId, Long managerId) throws BoCloudDBException {
        String sql = "update container set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and storage_account_id = :storageAccountId";
        Map<String, Object> params = MapTools.simpleMap("storageAccountId", storageAccountId);
        params.put("menderId", managerId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

    public boolean remove(Long id, Long managerId) throws BoCloudDBException {
        String sql = "update container 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("menderId", managerId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

    public boolean removeByVendorId(Long vendorId, Long managerId) throws BoCloudDBException {
        String sql = "update container 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", managerId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

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