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.CloudVendor;
import com.bocloud.ims.entity.resource.SangforAz;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.*;

@Repository("sangforAzRepository")
public class SangforAzRepository extends BasicGenericDao<SangforAz, Long> {

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

    public boolean removeByVendor(Long vendorId, Long userId) {
        String sql = "update sangfor_az 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 List<SangforAz> listByVendor(Long vendorId) {
        String sql = "select a.* from sangfor_az a where a.is_deleted = 0";
        Map<String, Object> params = new HashMap<>(8);
        if (null != vendorId) {
            sql += " and a.vendor_id = :vendorId";
            params.put(Common.VENDORID, vendorId);
        }
        return this.list(SangforAz.class, sql, params);
    }

    public SangforAz query (String uuid){
        String sql = "select * from sangfor_az a where a.is_deleted = 0 and a.uuid= :uuid";
        Map<String,Object> paramMap = MapTools.simpleMap("uuid",uuid);
        List<SangforAz> list = this.list(SangforAz.class, sql, paramMap);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public boolean remove(Long id, Long userId) {
        String sql = "update sangfor_az 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 stCount(List<Param> params, Long vendorId){
        String sql = "select count(1) from sangfor_az a  where a.is_deleted=0";
        if (vendorId !=null){
            sql += " and a.vendor_id = '" + vendorId + "'  ";
        }
        sql = QueryBuilder.build(sql,params,null,"a");
        Map<String,Object> param = QueryBuilder.getParam(params);
        return this.countQuery(sql,param).intValue();
    }

    public List<SangforAz> stList(int page, int rows, List<Param> params, Map<String, String> sorter, Long vendorId) throws BoCloudDBException {
        String sql = "select * from sangfor_az a  where a.is_deleted=0";
        if (vendorId !=null){
            sql += " and a.vendor_id = '" + vendorId + "'  ";
        }
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(SangforAz.class, sql);
    }

    public List<SangforAz> list(int page, int rows, List<Param> params, Map<String, String> sorter, Long vendorId) throws BoCloudDBException {
        String sql = "select a.name,a.uuid from sangfor_az a  where a.is_deleted=0";
        if (vendorId !=null){
            sql += " and a.vendor_id = '" + vendorId + "'  ";
        }
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(SangforAz.class, sql);
    }

    public int count(List<Param> params) {
        String sql = "select count(1) from sangfor_az a where a.is_deleted = 0 ";
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        sql = QueryBuilder.build(sql, params, null, "a");
        return this.countQuery(sql, paramMap).intValue();
    }

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) {
        String sql = "select a.id,a.name,a.uuid from sangfor_az 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<SangforAz> azList = this.list(SangforAz.class, sql, paramMap);
        List<SimpleBean> beans = new ArrayList<>();
        for (SangforAz az : azList) {
            SimpleBean bean = new SimpleBean();
            bean.setUuid(az.getUuid());
            bean.setName(az.getName());
            beans.add(bean);
        }
        return beans;
    }

    public List<SangforAz> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
        String sql = "select a.* from sangfor_az a where a.is_deleted = 0 ";
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(SangforAz.class, sql, paramMap);
    }
}
