package cn.tsy.idsse.dsds.control.search;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.hme.framework.helper.lang.CollectionHelper;
import cn.hme.framework.helper.lang.StringHelper;
import cn.hme.framework.web.control.Ajax;
import cn.hme.framework.web.control.AjaxControl;
import cn.tsy.data.idsse.dsds.mapper.dict.DictDataMapper;
import cn.tsy.data.idsse.dsds.mapper.dict.DictEquipmentMapper;
import cn.tsy.data.idsse.dsds.mapper.dict.DictPlatformMapper;
import cn.tsy.data.idsse.dsds.mapper.voyage.VoyageRegMapper;

@RestController
@RequestMapping("search")
@CrossOrigin
public class DSDSSearchControl extends AjaxControl {
	@Autowired
	private VoyageRegMapper mVoyage;
	@Autowired
	private DictPlatformMapper mPlatform;
	@Autowired
	private DictEquipmentMapper mEquipment;
	@Autowired
	private DictDataMapper mData;

	@RequestMapping("page.htm")
	public Ajax page(Integer group, Integer limit, Integer start, String search_type, String order, String type, String return_type, String search_value, Integer[] ids) {
		// String where += "select t.tsy_id from sample_dataset t,voyage y where y.tsy_id=t.voyage_id";
		String where = "";
		if ("航次名称".equals(search_type)) {
			if (ids == null || ids.length < 1) {
				where += " and t.voyage_name like concat('%','" + search_value + "','%') ";
			} else {
				where += " and voyage_id in(" + CollectionHelper.join(ids, ",") + ") ";
			}
		} else if ("平台类型".equals(search_type)) {
			if (ids == null || ids.length < 1) {
				where += " and (platform_type like concat('%','" + search_value + "','%') or platform_alias like concat('%','" + search_value + "','%')) ";
			} else {
				where += " and platform_id in(" + CollectionHelper.join(ids, ",") + ") ";
			}
		} else if ("平台名称".equals(search_type)) {
			where += " and platform_name like concat('%','" + search_value + "','%') ";

		} else if ("设备类型".equals(search_type)) {
			if (ids == null || ids.length < 1) {
				where += " and (equipment_type like concat('%','" + search_value + "','%') or equipment_alias like concat('%','" + search_value + "','%')) ";
			} else {
				where += " and equipment_id in(" + CollectionHelper.join(ids, ",") + ") ";
			}
		} else if ("数据集名称".equals(search_type)) {
			where += " and (dataset_name like concat('%','" + search_value + "','%')) ";
		} else if ("数据样品类型".equals(search_type)) {
			if (ids == null || ids.length < 1) {
				where += " and (data_type like concat('%','" + search_value + "','%') or data_alias like concat('%','" + search_value + "','%')) ";
			} else {
				where += " and data_id in(" + CollectionHelper.join(ids, ",") + ") ";
			}
		} else if ("航次首席".equals(search_type)) {
			where += " and voyage_officer like concat('%'," + search_value + ",'%') ";
		}

		if ("数据样品集".equals(return_type)) {
			String sql = "select t.* from (select file_total,d.* from sample_dataset d " + "	left join (" + "	select dataset_id,count(1) file_total from sample_line group by dataset_id " + "	union select dataset_id,count(1) file_total from sample_station group by dataset_id " + "	) l" + "	on d.tsy_id=l.dataset_id) t,voyage_reg y where y.tsy_id=t.voyage_id " + where + " order by t.create_time desc";
			return new Ajax(this.mVoyage.pageRecordBySql(group, start, limit, sql));
		} else {
			// 航次
			String sql = "select t.* from voyage_reg t where tsy_id in (" + "select distinct t.voyage_id from sample_dataset t,voyage_reg y where y.tsy_id=t.voyage_id " + " " + where + ") order by t.create_time desc";
			return new Ajax(this.mVoyage.pageRecordBySql(group, start, limit, sql));

		}

	}

	@RequestMapping("mult.htm")
	public Ajax mult(String search_type, Integer group, Integer limit, Integer start, String order, String type, SearchParam param) {
		if ("航次名称".endsWith(search_type))
			return new Ajax(this.mVoyage.searchWithPage(group, start, limit, search_type, order, type, param));
		if ("平台类型".endsWith(search_type))
			return new Ajax(this.mPlatform.searchWithPage(group, start, limit, search_type, order, type, param));
		if ("设备类型".endsWith(search_type))
			return new Ajax(this.mEquipment.searchWithPage(group, start, limit, search_type, order, type, param));
		if ("数据样品类型".endsWith(search_type))
			return new Ajax(this.mData.searchWithPage(group, start, limit, search_type, order, type, param));
		return new Ajax();
	}

	@RequestMapping("result.htm")
	public Ajax result(Integer group, Integer limit, Integer start, String order, String type, String return_type, String search_type, String search_value, SearchParam param) {

		String where = "";
		if (StringHelper.hasValue(param.getVoyage_name()))
			where += " and t.voyage_name ='" + param.getVoyage_name() + "' ";
		if (StringHelper.hasValue(param.getPlatform_type()))
			where += " and platform_type ='" + param.getPlatform_type() + "' ";
		if (StringHelper.hasValue(param.getEquipment_type()))
			where += " and equipment_type ='" + param.getEquipment_type() + "' ";
		if (StringHelper.hasValue(param.getData_type()))
			where += " and data_type ='" + param.getData_type() + "' ";

		if (StringHelper.hasValue(search_value)) {
			if ("航次名称".equals(search_type)) {
				where += " and t.voyage_name like concat('%','" + search_value + "','%') ";
			} else if ("平台类型".equals(search_type)) {
				where += " and (platform_type like concat('%','" + search_value + "','%') or platform_alias like concat('%','" + search_value + "','%')) ";
			} else if ("平台名称".equals(search_type)) {
				where += " and platform_name like concat('%','" + search_value + "','%') ";
			} else if ("设备类型".equals(search_type)) {
				where += " and (equipment_type like concat('%','" + search_value + "','%') or equipment_alias like concat('%','" + search_value + "','%')) ";
			} else if ("数据集名称".equals(search_type)) {
				where += " and (dataset_name like concat('%','" + search_value + "','%')) ";
			} else if ("数据样品类型".equals(search_type)) {
				where += " and (data_type like concat('%','" + search_value + "','%') or data_alias like concat('%','" + search_value + "','%')) ";
			} else if ("航次首席".equals(search_type)) {
				where += " and voyage_officer like concat('%','" + search_value + "','%') ";
			}
		}

		if ("数据样品集".equals(return_type)) {
			String sql = "select t.* from (select file_total,d.* from sample_dataset d left join (	select dataset_id,count(1) file_total from sample_line group by dataset_id union select dataset_id,count(1) file_total from sample_station group by dataset_id ) l on d.tsy_id=l.dataset_id) t,voyage_reg y where y.tsy_id=t.voyage_id " + 
					where + " order by t.create_time desc";
			return new Ajax(this.mVoyage.pageRecordBySql(group, start, limit, sql));
		} else {
			// 航次
			String sql = "select t.* from voyage_reg t where tsy_id in (" + "select distinct t.voyage_id from sample_dataset t,voyage_reg y where y.tsy_id=t.voyage_id " + " " + where + ") order by t.create_time desc";
			return new Ajax(this.mVoyage.pageRecordBySql(group, start, limit, sql));

		}

	}
}