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

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

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 org.springframework.web.multipart.MultipartFile;

import cn.hme.framework.helper.excel.ExcelReaderHelper;
import cn.hme.framework.helper.lang.StringHelper;
import cn.hme.framework.jdbc.provider.page.OrderBy;
import cn.hme.framework.jdbc.provider.page.Pageable;
import cn.hme.framework.web.control.Ajax;
import cn.hme.framework.web.control.AjaxControl;
import cn.hme.framework.web.exception.BusinessException;
import cn.tsy.data.idsse.dsds.mapper.voyage.VoyageMemberMapper;
import cn.tsy.data.idsse.dsds.mapper.voyage.VoyageRegMapper;
import cn.tsy.data.idsse.dsds.model.voyage.VoyageReg;
import cn.tsy.idsse.dsds.helper.DSDSWebHelper;
import cn.tsy.idsse.dsds.service.voyage.VoyageRegService;

@RestController
@RequestMapping("voyage/reg")
@CrossOrigin
public class VoyageRegControl extends AjaxControl {

	@Autowired
	private VoyageRegMapper mVoyage;

	@Autowired
	private VoyageRegService sVoyage;
	@Autowired
	private VoyageMemberMapper mMember;

	@RequestMapping("page.htm")
	public Ajax page(Integer group, Integer limit, Integer start, String query, String order, String type) {
		order = StringHelper.hasValue(order) ? order : "voyage_end_date";
		type = StringHelper.hasValue(type) ? type : "DESC";
		Pageable page = this.mVoyage.voyageWithPage(group, start, limit, query, order, type);
		return new Ajax(page);
	}

	@RequestMapping("edit.htm")
	public Ajax edit(VoyageReg voyage) {
		this.sVoyage.editVoyage(voyage, DSDSWebHelper.getFrameOptr(true));
		return new Ajax(voyage);
	}
	
	
	@RequestMapping("upload.htm")
	public Ajax upload(MultipartFile file) {
		// DSDSWebHelper.getFrameOptr(true);
		List<List<String>> values = ExcelReaderHelper.readSheet(file, 1, 40);
		if (values.size() == 0)
			throw new BusinessException("导入的文件中无有效数据，请检查！");
		return new Ajax(this.sVoyage.uploadVoyage(values).size());
	}

	@RequestMapping("info.htm")
	public Ajax info(String voyage_name) {
		VoyageReg voyage = this.mVoyage.findWithColumn("voyage_name", voyage_name);
		Map<String, Object> mapReturn = new HashMap<>();
		mapReturn.put("voyage", voyage);
		String sql = "select platform_type,platform_alias,platform_name,count(1) platform_total from sample_dataset where voyage_id=? group by platform_type,platform_alias,platform_name order by count(1) desc";
		mapReturn.put("PlatformArray", this.mVoyage.selectListMapBySql(sql,voyage.getTsy_id()));
		sql = "select equipment_type,equipment_alias,count(1) equipment_total from sample_dataset where voyage_id=? group by equipment_type,equipment_alias order by count(1) desc";
		mapReturn.put("EquipmentArray", this.mVoyage.selectListMapBySql(sql,voyage.getTsy_id()));
		sql="select dataset_name,platform_type,platform_alias,equipment_type,equipment_alias,count(1) data_total,data_type,data_alias,job_type from sample_dataset where voyage_id=? group by platform_type,platform_alias,equipment_type,equipment_alias,dataset_name,data_type,data_alias,job_type order by count(1) desc";
		mapReturn.put("DatasetArray", this.mVoyage.selectListMapBySql(sql,voyage.getTsy_id()));
		mapReturn.put("MemberArray", this.mMember.selectListByColumnAndSimpleOrder("voyage_id", voyage.getTsy_id(), OrderBy.of("tsy_id")));
		Ajax ajax = new Ajax();
		ajax.setMap(mapReturn);
		return ajax;
	}

	@RequestMapping("delete.htm")
	public Ajax delete(int tsy_id) {
		DSDSWebHelper.getFrameOptr(true);
		this.sVoyage.deleteVoyage(tsy_id);
		return new Ajax();
	}

}
