package com.zelinonline.controller;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.zelinonline.po.Company;
import com.zelinonline.po.Department;
import com.zelinonline.po.Job;
import com.zelinonline.po.Jobcategory;
import com.zelinonline.po.Rencai;
import com.zelinonline.po.Subcompany;
import com.zelinonline.service.IRCService;
import com.zelinonline.service.IService;
@Controller
public class RencaiController {
@Autowired
private IService companyService;
@Autowired
private IRCService rcService;
@Autowired
private IService subcompanyService;
@Autowired
private IService departmentService;
@Autowired
private IService jobcService;
@Autowired
private IService jobService;
@RequestMapping("/addrencai.do")
public ModelAndView addRencai(@ModelAttribute Rencai rc) {
ModelAndView mv = new ModelAndView();
if (rc.getRcName() == null) {
List<Company> companys = companyService.queryForAll();
/*
* Set<Subcompany> subcompaniess=new HashSet<Subcompany>();
* Set<Department> departmentss=new HashSet<Department>();
* Set<Jobcategory> jobcategoriess=new HashSet<Jobcategory>();
* Set<Job> jobss=new HashSet<Job>();
*/
/*
* for (Company company : companys) { Set<Subcompany> subcompanies =
* company.getSubcompanies();
*
* for (Subcompany subcompany : subcompanies) {
* subcompaniess.add(subcompany); Set<Department> departments =
* subcompany.getDepartments(); for (Department department :
* departments) { departmentss.add(department); Set<Jobcategory>
* jobcategories = department.getJobcategories(); for (Jobcategory
* jobcategory : jobcategories) { jobcategoriess.add(jobcategory);
* Set<Job> jobs = jobcategory.getJobs(); for (Job job : jobs) {
* jobss.add(job); } } } } }
*/
mv.addObject("companys", companys);
/*
* mv.addObject("subcompanies", subcompaniess);
* mv.addObject("departments", departmentss);
* mv.addObject("jobcategories", jobcategoriess);
* mv.addObject("jobs", jobss);
*/
mv.setViewName("addrencai");
} else {
System.out.println("bbbbbbbbbbbbbbbbbbb");
boolean isSuccess = rcService.addRc(rc);
if (isSuccess) {
mv.setViewName("success");
} else {
mv.setViewName("fail");
}
}
return mv;
}
@RequestMapping("getsubcompany.do")
public String getSubcompany(@RequestParam String companyid,
HttpServletResponse response) {
PrintWriter out = null;
System.out.println(companyid);
int cid = Integer.parseInt(companyid);
List<Subcompany> subcompanys = subcompanyService.queryForInt(cid);
if (subcompanys != null) {
JSONArray jsonArray = new JSONArray();
for (Subcompany subcompany : subcompanys) {
System.out.println(subcompany.getSubcompanyname());
JSONObject jsonObject = new JSONObject();
jsonObject.put("subcompanyid", subcompany.getSubcompanyid());
jsonObject
.put("subcompanyname", subcompany.getSubcompanyname());
jsonArray.add(jsonObject);
}
try {
out = response.getWriter();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.write(jsonArray.toString());
} else {
out.write("null");
}
out.flush();
out.close();
return null;
}
@RequestMapping("getdepartment.do")
public String getDepartment(@RequestParam String subcompanyid,
HttpServletResponse response) {
PrintWriter out = null;
System.out.println(subcompanyid);
int sid = Integer.parseInt(subcompanyid);
List<Department> departments = departmentService.queryForInt(sid);
if (departments != null) {
JSONArray jsonArray = new JSONArray();
for (Department department : departments) {
System.out.println(department.getDepartmentname());
JSONObject jsonObject = new JSONObject();
jsonObject.put("departmentid", department.getDepartmentid());
jsonObject
.put("departmentname", department.getDepartmentname());
jsonArray.add(jsonObject);
}
try {
out = response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
out.write(jsonArray.toString());
} else {
out.write("null");
}
out.flush();
out.close();
return null;
}
@RequestMapping("getjobcategory.do")
public String getJobcategory(@RequestParam String jobcategoryid,
HttpServletResponse response) {
PrintWriter out = null;
System.out.println(jobcategoryid);
int jid = Integer.parseInt(jobcategoryid);
List<Jobcategory> jobcategorys = jobcService.queryForInt(jid);
if (jobcategorys != null) {
JSONArray jsonArray = new JSONArray();
for (Jobcategory jobcategory : jobcategorys) {
System.out.println(jobcategory.getCategoryname());
JSONObject jsonObject = new JSONObject();
jsonObject.put("jobcategoryid", jobcategory.getCategoryid());
jsonObject
.put("jobcategoryname", jobcategory.getCategoryname());
jsonArray.add(jsonObject);
}
try {
out = response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
out.write(jsonArray.toString());
} else {
out.write("null");
}
out.flush();
out.close();
return null;
}
@RequestMapping("getjob.do")
public String getJob(@RequestParam String jobid,
HttpServletResponse response) {
PrintWriter out = null;
System.out.println(jobid);
int jid = Integer.parseInt(jobid);
List<Job> jobs = jobService.queryForInt(jid);
if (jobs != null) {
JSONArray jsonArray = new JSONArray();
for (Job job : jobs) {
System.out.println(job.getJobname());
JSONObject jsonObject = new JSONObject();
jsonObject.put("jobid", job.getJobid());
jsonObject.put("jobname", job.getJobname());
jsonArray.add(jsonObject);
}
try {
out = response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
out.write(jsonArray.toString());
} else {
out.write("null");
}
out.flush();
out.close();
return null;
}
@RequestMapping(value = "/profile/uploadpicture.do", produces = "text/plain;charset=UTF-8")
@ResponseBody
public String ajaxUpload(
HttpServletRequest request, HttpServletResponse response)
throws IllegalStateException, IOException {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
String fileName = "";
String uploadPath = "uploadFile/head/";
String path = request.getSession().getServletContext().getRealPath("/")
+ uploadPath;
File file1 = new File(path);
if(!file1.exists()) file1.mkdirs();
String realPath = "";
for (Iterator it = multipartRequest.getFileNames(); it.hasNext();) {
String key = (String) it.next();
MultipartFile mulfile = multipartRequest.getFile(key);
fileName = mulfile.getOriginalFilename();
// fileName = handlerFileName(fileName);
File file = new File(path + fileName);
if (!file.exists())
file.createNewFile();
mulfile.transferTo(file);
}
realPa