package cn.hospital.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import cn.hospital.bo.Page;
import cn.hospital.pojo.Films;
import cn.hospital.pojo.Patient;
import cn.hospital.service.FilmsService;
import cn.hospital.utils.Globals;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
@Component("FilmsAction")
@Scope("prototype")
public class FilmsAction extends BaseAction {
	
	private int id;
	
	private int pageNumber=1;
	private int pageLength=Globals.PAGE_LENGTH;;
	private int recordSize;
	private int pageSize;
	
	private Page page;
	private File image;
	private String imageFileName; // 文件名称
	private String imageContentType; // 文件类型
	
	private File[] imageadd;
	private String[] imageaddFileName; // 文件名称
	private String[] imageaddContentType; // 文件类型
	
	private Films films;
	private List<Films>filmsList=new ArrayList<Films>();
	private int patientId;
	
	private FilmsService filmsService;
	private String oldImageName;
	private String serverRelativePath;
	private String[]  serverRelativePathes;
	private String message;
	
	private String oldFilesNum="";
	
	
	public String getOldFilesNum() {
		return oldFilesNum;
	}
	public void setOldFilesNum(String oldFilesNum) {
		this.oldFilesNum = oldFilesNum;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String toAddFilms(){
		System.out.println("----patientId:"+patientId);
		return Globals.SUCCESS;
	}
	
	public String addFilms(){
		for(int i=0;i<this.filmsList.size();i++){
			
			Films newFilms=filmsList.get(i);
			
			String condition=" where numFilms='"+newFilms.getNumFilms()+"'";
			if(filmsService.queryFilms(condition).size()!=0){
				message="该影像号已经存在,请重新添加";
				return Globals.FAILURE;
			}	
			
			String savePath = ServletActionContext.getServletContext().getRealPath(
					File.separator + Globals.SAVE_DIR + File.separator
							+ Globals.DIR_IMAGES);
			System.out.println("savePath:"+savePath);
			if (imageadd != null) {	
				imageaddFileName[i]=Globals.getString(6)+"-"+ imageaddFileName[i].replace(" ", "");;
				File saveFile = new File(new File(savePath), imageaddFileName[i]);
				if (!saveFile.getParentFile().exists()) {
					saveFile.getParentFile().mkdirs();
				}
				try {
					FileUtils.copyFile(imageadd[i], saveFile);
					newFilms.setImageUrl(imageaddFileName[i]);
				} catch (IOException e) {
					System.out.println("上传海报异常");
					e.printStackTrace();
				}
			}
			
			newFilms.setPatientId(patientId);
			this.filmsService.addFilms(newFilms);
		}
		
		return Globals.SUCCESS;
	}
	
	
	private String extraCondition="";
	public String lookFilms() throws UnsupportedEncodingException{
		
		String condition=" where patientId="+patientId;
		if(!"".equals(extraCondition))
			condition=condition+extraCondition;
		if(pageNumber < 1) pageNumber = 1;
		System.out.println("films----num:"+pageNumber+"--len:"+pageLength);
		Page page = this.filmsService.query(pageNumber, pageLength,condition);
		this.filmsList = (List<Films>)page.getDataList();
		recordSize = page.getRecordSize();
		pageSize = page.getPageSize();
		if(pageNumber > pageSize){
			pageNumber = pageSize;
			page = this.filmsService.query(pageNumber, pageLength,condition);
			System.out.println("films----page:"+page);
			filmsList = (List<Films>)page.getDataList();
			recordSize = page.getRecordSize();
			pageSize = page.getPageSize();
		}
		
		System.out.println("------filmsList:"+filmsList.size());
		if(filmsList.size()!=0){
			serverRelativePathes=new String[filmsList.size()];
			for(int i=0;i<serverRelativePathes.length;i++){
				serverRelativePathes[i]=Globals.SAVE_DIR+"//"+Globals.DIR_IMAGES+"//"+filmsList.get(i).getImageUrl();
				System.out.println("serverRelativePathes[i]::"+serverRelativePathes[i]);
			//serverRelativePathes[i]=new String(serverRelativePathes[i].getBytes("ISO-8859-1") , "utf-8"); 
			}
		}
	/*	String savePath = ServletActionContext.getServletContext().getRealPath(
				File.separator + Globals.SAVE_DIR + File.separator
						+ Globals.DIR_IMAGES);*/
		//filmsList=this.filmsService.lookFilms(patientId);
		//imageFileName=films.getImageUrl();
		//serverRelativePath=""+Globals.SAVE_DIR;
		//System.out.println("serverRelativePath:"+server+"//"+Globals.DIR_IMAGES+"//"+imageFileNameRelativePath);
		return Globals.SUCCESS;
	}
	
	
	public String lookFilmsByID(){
	
		films=this.filmsService.getFilmsById(id);
		String image_name=films.getImageUrl();
		serverRelativePath=""+Globals.SAVE_DIR+"//"+Globals.DIR_IMAGES+"//"+image_name;
		return Globals.SUCCESS;
		
		
	}
	
	
	
	public String updateFilms(){
		
		Films db_films=this.filmsService.getFilmsById(id);
		
		String condition=" where numFilms="+films.getNumFilms();
	
		if(!films.getNumFilms().equals(db_films.getNumFilms())){	//当号码还是数据库中的编号的时候,直接更改
			if(!oldFilesNum.equals(films.getNumFilms())){	//当号码不是数据库中的编号并且新旧号码不同的时候
				if(filmsService.queryFilms(condition).size()!=0){
					message="该影像号已经存在,请重新更改";
					films.setId(db_films.getId());
					this.patientId=db_films.getPatientId();
					films.setPatientId(patientId);
					return Globals.FAILURE;
				}
			}
		}
		
		String savePath = ServletActionContext.getServletContext().getRealPath(
				File.separator + Globals.SAVE_DIR + File.separator
						+ Globals.DIR_IMAGES);
		if (image != null) {
			imageFileName=Globals.getString(6)+"-"+ imageFileName.replace(" ", "");;
			File saveFile = new File(new File(savePath), imageFileName);
			if (!saveFile.getParentFile().exists()) {
				saveFile.getParentFile().mkdirs();
			}
			try {
				FileUtils.copyFile(image, saveFile);
				films.setImageUrl(imageFileName);
			} catch (IOException e) {
				System.out.println("上传海报异常");
				e.printStackTrace();
			}
			if(oldImageName !=null || oldImageName !=""){
				File oldFile = new File(new File(savePath), oldImageName);	//删除旧文件
				if(oldFile.exists()&&oldFile.isFile())
					oldFile.delete();
			}
			
		}
		
		films.setId(id);
		films.setPatientId(patientId);
		this.filmsService.updateFilms(films);
		return Globals.SUCCESS;
	}
	
	
	public String delFilms() throws UnsupportedEncodingException{
		
		String savePath = ServletActionContext.getServletContext().getRealPath(
				File.separator + Globals.SAVE_DIR + File.separator
						+ Globals.DIR_IMAGES);
		System.out.println("savePath:"+savePath+"--fileName:"+imageFileName);
		imageFileName=new String(imageFileName.getBytes("ISO8859-1"), "UTF-8");
		if (imageFileName != null) {
			File saveFile = new File(new File(savePath), imageFileName);
			if(saveFile.exists()&&saveFile.isFile())
				saveFile.delete();
		}
		this.filmsService.deleteFilms(id);
		return Globals.SUCCESS;
	}
	
	
	private String ids;
	public String deleteAllFilms(){
		
		System.out.println("ids:"+ids);
		String[] films_ids=ids.split(",");
		for(int i=0;i<films_ids.length;i++){
			this.filmsService.deleteFilms(Integer.parseInt(films_ids[i]));
		}
		return Globals.SUCCESS;
	}
	
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	
	
	public String downloadMyImage() throws IOException{
		
		String savePath = ServletActionContext.getServletContext().getRealPath(
				File.separator + Globals.SAVE_DIR + File.separator
						+ Globals.DIR_IMAGES);
		System.out.println("downloadMyImage----imageFileName111:"+imageFileName);
		//imageFileName=new String(imageFileName.getBytes("ISO-8859-1") , "utf-8"); 
		//System.out.println("downloadMyImage----imageFileName222:"+imageFileName);
		File saveFile = new File(new File(savePath), imageFileName);
		if (saveFile.exists()) {
			long len = saveFile.length();
			byte a[] = new byte[1024 * 1024];
			FileInputStream fin = new FileInputStream(saveFile);
			response.setHeader("Access-Control-Allow-Origin", "*\r\n");
			OutputStream outs = response.getOutputStream();
			
			System.out.println("saveFile.getName():"+saveFile.getName());
			String filename = new String(saveFile.getName().getBytes("UTF-8"), "ISO8859-1");
			response.setHeader("Content-Disposition", "attachment; filename=" + filename);
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Lenght", String.valueOf(len));
			int read = 0;
			while ((read = fin.read(a)) != -1) {
				outs.write(a, 0, read);
			}
			outs.close();
			fin.close();
		}
		return null;
	}
	
	private String filmsNum;
	
	public String testFilmsNum() throws IOException{
		//System.out.println("photoName:"+photoName);
		String condition=" where numFilms='"+filmsNum+"'";
		PrintWriter out = response.getWriter();
		String responseText="inFilms";
		if(this.filmsService.queryFilms(condition).size()==0)
			responseText="false";
		//System.out.println("responseText:"+responseText);
		out.println(responseText);
		out.flush();
		out.close();
		 return null;
	 }
	
	public String getExtraCondition() {
		return extraCondition;
	}
	public void setExtraCondition(String extraCondition) {
		this.extraCondition = extraCondition;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public File getImage() {
		return image;
	}
	public void setImage(File image) {
		this.image = image;
	}
	public String getImageFileName() {
		return imageFileName;
	}
	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}
	public String getImageContentType() {
		return imageContentType;
	}
	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}
	public Films getFilms() {
		return films;
	}
	public void setFilms(Films films) {
		this.films = films;
	}
	public int getPatientId() {
		return patientId;
	}
	public void setPatientId(int patientId) {
		this.patientId = patientId;
	}
	@JSON(serialize = false)
	public FilmsService getFilmsService() {
		return filmsService;
	}
	@Resource(name = "filmsService")
	public void setFilmsService(FilmsService filmsService) {
		this.filmsService = filmsService;
	}
	
	public String getOldImageName() {
		return oldImageName;
	}
	public void setOldImageName(String oldImageName) {
		this.oldImageName = oldImageName;
	}
	public String getServerRelativePath() {
		return serverRelativePath;
	}
	public void setServerRelativePath(String serverRelativePath) {
		this.serverRelativePath = serverRelativePath;
	}
	public List<Films> getFilmsList() {
		return filmsList;
	}
	public void setFilmsList(List<Films> filmsList) {
		this.filmsList = filmsList;
	}
	public int getPageNumber() {
		return pageNumber;
	}
	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}
	public int getPageLength() {
		return pageLength;
	}
	public void setPageLength(int pageLength) {
		this.pageLength = pageLength;
	}
	public int getRecordSize() {
		return recordSize;
	}
	public void setRecordSize(int recordSize) {
		this.recordSize = recordSize;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public String[] getServerRelativePathes() {
		return serverRelativePathes;
	}
	public void setServerRelativePathes(String[] serverRelativePathes) {
		this.serverRelativePathes = serverRelativePathes;
	}
	public File[] getImageadd() {
		return imageadd;
	}
	public void setImageadd(File[] imageadd) {
		this.imageadd = imageadd;
	}
	public String[] getImageaddFileName() {
		return imageaddFileName;
	}
	public void setImageaddFileName(String[] imageaddFileName) {
		this.imageaddFileName = imageaddFileName;
	}
	public String[] getImageaddContentType() {
		return imageaddContentType;
	}
	public void setImageaddContentType(String[] imageaddContentType) {
		this.imageaddContentType = imageaddContentType;
	}
	public String getFilmsNum() {
		return filmsNum;
	}
	public void setFilmsNum(String filmsNum) {
		this.filmsNum = filmsNum;
	}
	
}
 最近下载更多
最近下载更多
                
                wuying8208     LV15
                2024年10月23日
            
            
        
                Mr_VVcat     LV9
                2024年4月16日
            
            
        
                zhaoming200677     LV13
                2023年4月18日
            
            
        
                xiquyiyuan     LV10
                2023年3月29日
            
            
        
                adfsxz     LV3
                2022年8月29日
            
            
        
                wanglinddad     LV55
                2021年12月29日
            
            
        
                17683946472     LV9
                2021年11月26日
            
            
        
                圈子圈     LV3
                2021年4月10日
            
            
        
                lhw549624     LV3
                2021年1月9日
            
            
        
                2639891331     LV8
                2021年1月4日
            
            
         最近浏览更多
最近浏览更多
                
                d3834632     LV2
                7月14日
            
            
        
                wuying8208     LV15
                2024年10月23日
            
            
        
                abdkfksdkf     LV16
                2024年8月26日
            
            
        
                yyhrhv     LV8
                2024年7月24日
            
            
        
                jiyun2021     LV9
                2024年7月15日
            
            
        
                quartz     LV8
                2024年7月1日
            
            
        
                疯狂小太阳    
                2024年6月28日
            
            
                    暂无贡献等级
            
        
                ss3508507551     LV2
                2024年6月3日
            
            
        
                不嘻嘻     LV8
                2024年5月11日
            
            
        
                hmf1989    
                2024年4月29日
            
            
                    暂无贡献等级
            
        
 
                 
                 
                