package com.wyxeainn.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import com.wyxeainn.pojo.*;
import com.wyxeainn.service.*;
import com.wyxeainn.tools.SMSUtil;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import sun.misc.BASE64Decoder;
import javax.servlet.http.HttpSession;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.*;
import java.io.File;
@Controller
public class SeekerController {
private SeekerService seekerService = new SeekerServiceImpl();
private CodeService codeService = new CodeServiceImpl();
private IndustryService industryService = new IndustryServiceImpl();
private CityService cityService = new CityServiceImpl();
private SocialService socialService = new SocialServiceImpl();
private EduService eduService = new EduServiceImpl();
private ProjectService projectService = new ProjectServiceImpl();
private PracticeService practiceService = new PracticeServiceImpl();
private HopeService hopeService = new HopeServiceImpl();
private BossService bossService = new BossServiceImpl();
/**
* 判断求职者信息表中手机号码是否存在
*
* @param params
* @return
*/
@RequestMapping(value = "/jobSeeker/accountExist.action")
public @ResponseBody boolean accountExist(@RequestBody String params) {
JSONObject obj = JSON.parseObject(params);
String phone = obj.getString("phone");
//seekerService = new SeekerServiceImpl();
boolean flag = seekerService.accountExist(phone);
return flag;
}
@RequestMapping(value = "/seeker/updatePhone.action")
public @ResponseBody boolean updatePhone(@RequestBody String params) {
boolean flag = true;
JSONObject obj = JSON.parseObject(params);
String code = obj.getString("code");
String oldPhone = obj.getString("oldPhone");
String newPhone = obj.getString("newPhone");
String id = obj.getString("id");
String identity = obj.getString("identity");
//codeService = new CodeServiceImpl();
//seekerService = new SeekerServiceImpl();
String smsCode = codeService.getCode(newPhone);
if(smsCode.equals(code)) {
try {
if (identity.equals("seeker")) {
Seeker seeker = new Seeker();
seeker.setPhone(newPhone);
seeker.setId(Integer.parseInt(id));
seekerService.updatePhoneById(seeker);
} else {
Boss boss = new Boss();
boss.setPhone(newPhone);
boss.setId(Integer.parseInt(id));
bossService.updatePhoneById(boss);
}
} catch (Exception ex) {
ex.printStackTrace();
flag = false;
}
} else {
flag = false;
}
return flag;
}
@RequestMapping(value = "/sendMessage/sendToSeeker.action")
public @ResponseBody boolean sendMessageToSeeker(@RequestBody String params) {
JSONObject obj = JSON.parseObject(params);
String phone = obj.getString("phone");
String code = "";
int x;
for(int i = 0; i < 6; i++) {
x = (int)(Math.random()*10);
code += x;
}
String content = "您的验证码是" + code + ",在1分钟内输入有效。如非本人操作请忽略本短信。【知心招聘】";
SMSUtil smsUtil = new SMSUtil();
boolean flag = smsUtil.send(phone,content);
//codeService = new CodeServiceImpl();
if(flag) {
boolean exist = codeService.phoneExist(phone);
Code c = new Code();
c.setId(phone);
c.setCode(code);
//存在,更新动态码字段
if(exist==true) {
codeService.updateCode(c);
}else {
//否则新插入一条记录
codeService.insertCode(c);
}
}
return flag;
}
@RequestMapping(value = "/userRegister/checkCode.action")
public @ResponseBody Boolean checkCode(@RequestBody String params){
JSONObject obj = JSON.parseObject(params);
String phone = obj.getString("phone");
String code = obj.getString("code");
//codeService = new CodeServiceImpl();
String c = codeService.getCode(phone);
if(code.equals(c)) {
return true;
}else {
return false;
}
}
//注册功能
@RequestMapping(value = "/jobSeeker/register.action")
public @ResponseBody boolean jobSeekerRegister(@RequestBody String params) {
JSONObject obj = JSON.parseObject(params);
String phone = obj.getString("phone");
Seeker seeker = new Seeker();
seeker.setPhone(phone);
Boss boss = new Boss();
boss.setPhone(phone);
//codeService = new CodeServiceImpl();
//bossService = new BossServiceImpl();
//seekerService = new SeekerServiceImpl();
boolean flag = true;
try {
if(codeService.phoneExist(phone)==false) {
Code code = new Code();
code.setId(phone);
code.setCode("123456");
codeService.insertCode(code);
}
seekerService.insertSeeker(seeker);
bossService.registerBoss(boss);
}catch(Exception ex) {
flag = false;
}
return flag;
}
/**
* 进入到职位搜索界面
* @param session
* @return
*/
@RequestMapping(value = "seeker/searchJob.action")
public String searchJob (HttpSession session) {
setData(session);
return "/Jobseeker/searchJob.jsp";
}
/**
* 设置需要的数据
* @param session
*/
public void setData(HttpSession session) {
session.setAttribute("Categorys",getCategory());
session.setAttribute("Industrys",getFullIndustry());
session.setAttribute("Citys",getCitys());
}
/**
* 求职者首页
* @param session
* @return
*/
@RequestMapping(value = "seeker/homepage.action")
public String seekerHomepage(HttpSession session) {
setData(session);
return "/Common/seeker_homepage.jsp";
}
/**
* 求职者首页导航栏链接。
* @param session
* @param cateId
* @param compName
* @param city
* @return
*/
@RequestMapping(value = "seeker/searchJobWithCondition.action")
public String searchJobWithCondition(HttpSession session,String cateId,String cateName,String compName,String city) {
setData(session);
return "/Jobseeker/searchJob.jsp";
}
@RequestMapping(value = "user/login.action")
public String userLogin(HttpSession session, String chose, String phone) {
String targetUrl = "";
if(chose.equals("求职者")) {
//seekerService = new SeekerServiceImpl();
Seeker seeker = seekerService.selectSeekerByPhone(phone);
session.setAttribute("uid",seeker.getId());
session.setAttribute("Categorys",getCategory());
session.setAttribute("Industrys",getFullIndustry());
session.setAttribute("Citys",getCitys());
session.setAttribute("identity","seeker");
targetUrl = "/Common/seeker_homepage.jsp";
}else if(chose.equals("�