package com.bocloud.ims.repository.resource;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
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.AccessControl;
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.List;
import java.util.Map;

/**
 * @author lyy
 * @version 1.0
 * @date 2021/1/14 10:11
 */
@Repository("accessControlRepository")
public class AccessControlRepository extends BasicGenericDao<AccessControl, Long> {
	public AccessControlRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate) {
		super(jdbcTemplate, npJdbcTemplate);
	}

	public List<AccessControl> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = "select a.* from access_control a  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(AccessControl.class, sql, paramMap);
	}

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name,a.acl_uuid from access_control 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<AccessControl> acls = this.list(AccessControl.class, sql, paramMap);
		List<SimpleBean> beans = Lists.newArrayList();
		for (AccessControl acl : acls) {
			beans.add(new SimpleBean(acl.getId(), acl.getName(), acl.getAclUuId()));
		}
		return beans;
	}

	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from access_control a where 1=1";
		sql = QueryBuilder.buildRaw(sql, params, null, "a");
		Map<String, Object> param = QueryBuilder.getParam(params);
		return this.countQuery(sql, param).intValue();
	}

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

	public List<AccessControl> listByVid(Long vendorId, String regionId) throws Exception {
		String sql = "select a.* from access_control a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.region_id = :regionId ";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("regionId", regionId);
		return this.list(AccessControl.class, sql, params);
	}

	public boolean remove(Long id, Long userId) throws Exception {
		String sql = "update access_control 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 AccessControl query(Long id) throws Exception {
		String sql = "select a.* from access_control a where a.is_deleted = 0 and a.id = :id ";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<AccessControl> list = this.list(AccessControl.class, sql, params);
		return ListTool.isEmpty(list) ? null : list.get(0);
	}
}
