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

import java.util.ArrayList;
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.Excel2007Writer;
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.VoyageMember;
import cn.tsy.data.idsse.dsds.model.voyage.VoyageReg;
import cn.tsy.idsse.dsds.helper.DSDSWebHelper;
import cn.tsy.idsse.dsds.service.voyage.VoyageMemberService;

@RestController
@RequestMapping("voyage/member")
@CrossOrigin
public class VoyageMemberControl extends AjaxControl {

	@Autowired
	private VoyageRegMapper mReg;
	@Autowired
	private VoyageMemberMapper mMember;

	@Autowired
	private VoyageMemberService sMember;

	@RequestMapping("page.htm")
	public Ajax page(int voyage_id, Integer group, Integer limit, Integer start, String query, String order, String type) {
		order = StringHelper.hasValue(order) ? order : "tsy_id";
		Pageable page = this.mMember.memberWithPage(group, start, limit, query, order, type, voyage_id);
		return new Ajax(page);
	}

	@RequestMapping("edit.htm")
	public Ajax edit(VoyageMember member) {
		DSDSWebHelper.getFrameOptr(true);
		this.sMember.editMember(member);
		return new Ajax();
	}
	
	@RequestMapping("delete.htm")
	public Ajax delete(int member_id) {
		DSDSWebHelper.getFrameOptr(true);
		this.sMember.deleteMember(member_id);
		return new Ajax();
	}
	
	@RequestMapping("list.htm")
	public Ajax list(int voyage_id) {
		return new Ajax(this.mMember.selectListByColumnAndSimpleOrder("voyage_id", voyage_id, OrderBy.of("tsy_id")));
	}
	
	
	@CrossOrigin
	@RequestMapping("upload.htm")
	public Ajax upload(int voyage_id, MultipartFile file) {
		DSDSWebHelper.getFrameOptr(true);
		List<List<String>> values = ExcelReaderHelper.readSheet(file,1, 12);
		List<VoyageMember> list = this.sMember.uploadMember(voyage_id, values, DSDSWebHelper.getFrameOptr(true));
		if (list.size() == 0)
			throw new BusinessException("未找到有效的参航人员记录！");
		return new Ajax();
	}

	@RequestMapping("download.htm")
	public void download(int voyage_id) {
		VoyageReg voyage = this.mReg.find(voyage_id);
		List<VoyageMember> array = this.mMember.selectListByColumnAndSimpleOrder("voyage_id", voyage_id, OrderBy.of("tsy_id"));
		// 姓名	性别	出生日期	单位	岗位职责	船上床号	联系电话	证件号码	银卡名称	银行卡号码	开户行	备注信息
		Map<String, Integer> titleMap = Excel2007Writer.createTitleMap("姓名", 20);
		titleMap.put("性别", 8);
		titleMap.put("出生日期", 14);
		titleMap.put("单位", 30);
		titleMap.put("岗位职责", 20);

		List<List<Object>> values = new ArrayList<List<Object>>();
		for(VoyageMember m:array) {
			List<Object> vs = new ArrayList<Object>();
			vs.add(m.getMember_name());
			vs.add(m.getMember_sex());
			vs.add(m.getMember_birthday());
			vs.add(m.getMember_unit());
			vs.add(m.getMember_dept());
			values.add(vs);
		}
		Excel2007Writer.downloadAsExcel(this.getResponse(),voyage.getVoyage_name() + "_参航人员名单",values, titleMap);
	}

}
