package com.bocloud.ims.repository.manageone;

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.manageone.ManageOneProject;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

/**
 * manage one 项目数据访问
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年2月27日
 *
 */
@Repository("manageOneProjectRepository")
public class ManageOneProjectRepository extends BasicGenericDao<ManageOneProject, Long> {

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

	public List<ManageOneProject> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = "select a.*,b.name vdc_name from manage_one_project a left join manage_one_vdc b on b.id = a.vdc_id where 1 = 1 ";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.list(ManageOneProject.class, sql, paramMap);
	}

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name from manage_one_project a where 1 = 1 ";
		sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		List<ManageOneProject> groups = this.list(ManageOneProject.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<>();
		for (ManageOneProject group : groups) {
			beans.add(new SimpleBean(group.getId(), group.getName()));
		}
		return beans;
	}

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

	public ManageOneProject query(Long id) throws Exception {
		String sql = "select a.*,b.name vdc_name from manage_one_project a left join manage_one_vdc b on b.id = a.vdc_id where a.id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<ManageOneProject> list = this.list(ManageOneProject.class, sql, params);
		return ListTool.isEmpty(list) ? null : list.get(0);
	}

	public List<ManageOneProject> listByVdcId(Long vdcId) {
		String sql = "select a.*,b.name vdc_name from manage_one_project a left join manage_one_vdc b on b.id = a.vdc_id where a.vdc_id = :vdcId";
		Map<String, Object> paramMap = MapTools.simpleMap("vdcId", vdcId);
		return this.list(ManageOneProject.class, sql, paramMap);
	}

	public List<ManageOneProject> listByVendorId(Long vendorId) {
		String sql = "select a.*,b.name vdc_name from manage_one_project a left join manage_one_vdc b on b.id = a.vdc_id where a.vendor_id = :vendorId";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.list(ManageOneProject.class, sql, paramMap);
	}

	public boolean removeByVendorId(Long vendorId) {
		String sql = "delete from manage_one_project where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.execute(sql, params) > 0;
	}

	public boolean removeByVdcId(Long vdcId) {
		String sql = "delete from manage_one_project where vdc_id = :vdcId";
		Map<String, Object> params = MapTools.simpleMap("vdcId", vdcId);
		return this.execute(sql, params) > 0;
	}

	public boolean removeByProjectId(String projectId, Long vendorId) {
		String sql = "delete from manage_one_project where project_id = :projectId and vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("projectId", projectId);
		params.put("vendorId", vendorId);
		return this.execute(sql, params) > 0;
	}

	public boolean remove(Long id) {
		String sql = "delete from manage_one_project where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		return this.execute(sql, params) > 0;
	}

}
