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

import java.io.File;
import java.time.LocalDateTime;

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.date.LocalDateHelper;
import cn.hme.framework.helper.file.FileHelper;
import cn.hme.framework.helper.lang.GsonHelper;
import cn.hme.framework.helper.lang.StringHelper;
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.helper.WebHelper;
import cn.tsy.data.idsse.dsds.mapper.rescue.RescueFileMapper;
import cn.tsy.data.idsse.dsds.mapper.rescue.RescueRegMapper;
import cn.tsy.data.idsse.dsds.model.rescue.RescueFile;
import cn.tsy.data.idsse.dsds.model.rescue.RescueReg;
import cn.tsy.idsse.dsds.helper.DSDSParamHelper;
import cn.tsy.idsse.dsds.helper.DSDSWebHelper;
import cn.tsy.idsse.dsds.service.rescue.RescueRegService;

@RestController
@RequestMapping("rescue/reg")
@CrossOrigin
public class RescueRegControl extends AjaxControl {

	@Autowired
	private RescueRegMapper mRescue;
	@Autowired
	private RescueFileMapper mFile;

	@Autowired
	private RescueRegService sRescue;

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

	@CrossOrigin
	@RequestMapping("edit.htm")
	public Ajax edit(RescueReg rescue) {
		this.sRescue.editRescue(rescue);
		return new Ajax(rescue);
	}
	@CrossOrigin
	@RequestMapping("find.htm")
	public Ajax find(int rescue_id) {
		return new Ajax(this.mRescue.find(rescue_id));
	}
	
	@CrossOrigin
	@RequestMapping("file/upload.htm")
	public Ajax upload(int rescue_id,  MultipartFile file) {
		new File(DSDSParamHelper.RescueFileRoot).mkdirs();
		RescueReg reg = this.mRescue.find(rescue_id);
		String file_name = reg.getRescue_code() +LocalDateHelper.formatNoSplit(LocalDateTime.now())+"."+FileHelper.getFileSuffix(file);
		File f = new File(DSDSParamHelper.RescueFileRoot+File.separator+file_name);
		FileHelper.fromUpload(file, f);
		RescueFile rFile = new RescueFile();
		rFile.setRescue_id(rescue_id);
		rFile.setFile_length(f.length()+"");
		rFile.setFile_org_name(file.getOriginalFilename());
		rFile.setFile_path(f.getPath());
		rFile.setFile_real_name(f.getName());
		this.mFile.insert(rFile);		
		return new Ajax(rFile);
	}
	
	@CrossOrigin
	@RequestMapping("file/view.htm")
	public void view(int file_id) {
		RescueFile f = this.mFile.find(file_id);
		File file= new File(DSDSParamHelper.RescueFileRoot+File.separator+f.getFile_real_name());
		if(file.exists()) 
			WebHelper.viewImage(file, file.getName(), false);
	}
	
	@CrossOrigin
	@RequestMapping("file/list.htm")
	public Ajax list(int rescue_id) {
		return new Ajax(this.mFile.selectListByColumn("rescue_id", rescue_id));
	}
	@CrossOrigin
	@RequestMapping("file/download.htm")
	public void download(int file_id) {
		RescueFile f = this.mFile.find(file_id);
		File file= new File(DSDSParamHelper.RescueFileRoot+File.separator+f.getFile_real_name());
		if(file.exists()) 
			WebHelper.downloadFile(file, file.getName());
	}

	@CrossOrigin
	@RequestMapping("delete.htm")
	public Ajax delete(int tsy_id) {
		this.sRescue.deleteRescue(tsy_id);
		return new Ajax();
	}

	

	@CrossOrigin
	@RequestMapping("total")
	public Ajax total(int tsy_id) {
		this.sRescue.deleteRescue(tsy_id);
		return new Ajax();
	}

}
