package qdu.java.recruit.controller.user;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import javafx.application.Application;
import net.sf.json.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import qdu.java.recruit.constant.GlobalConst;
import qdu.java.recruit.controller.BaseController;
import qdu.java.recruit.entity.*;
import qdu.java.recruit.pojo.ApplicationPositionHRBO;
import qdu.java.recruit.pojo.FavorPositionBO;
import qdu.java.recruit.pojo.PositionCompanyBO;
import qdu.java.recruit.pojo.UserCommentBO;
import qdu.java.recruit.service.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
@Controller
@EnableAutoConfiguration
@Api("ajax返回json控制器")
public class DataController extends BaseController {
private static final Logger LOGGER = LogManager.getLogger();
@Resource
private PositionService positionService;
@Resource
private UserService userService;
@Resource
private CategoryService categoryService;
@Resource
private CommentService commentService;
@Resource
private DepartmentService departmentService;
@Resource
private CompanyService companyService;
@Resource
private ResumeService resumeService;
@Resource
private ApplicationService applicationService;
@Resource
private FavorService favorService;
/**
* 用户注册返回 0 -> 失败 1 -> 成功
*
* @return
*/
@PostMapping(value = "user/registerPost")
@ResponseBody
public int userRegister(@RequestParam String mobile, @RequestParam String password, @RequestParam String nickName) {
//验证mobile 和 password是否为空
if (mobile == null || password == null || nickName == null) {
return 0;
}
if (userService.getUserByMobile(mobile) != null) {
return 0;
}
UserEntity userEntity = new UserEntity();
userEntity.setMobile(mobile);
userEntity.setPassword(password);
userEntity.setNickname(nickName);
if (!userService.registerUser(userEntity)) {
return 0;
}
ResumeEntity resume = new ResumeEntity();
resume.setUserId(userService.getUserByMobile(mobile).getUserId());
if (!resumeService.createResume(resume)) {
return 0;
}
return 1;
}
/**
* 用户登录
*
* @return
*/
@PostMapping(value = "user/loginPost")
@ResponseBody
public int userLogin(HttpSession httpSession, @RequestParam String userName, @RequestParam String userPass) {
String mobile = userName;
String password = userPass;
if (mobile == null || password == null) {
return 0;
}
if (userService.loginUser(mobile, password)) {
httpSession.setAttribute("user", userService.getUserByMobile(mobile));
return 1;
}
return 0;
}
/**
* 主页分页输出 (用户信息,职位列表)
*
* @param page
* @param limit
* @return
*/
@PostMapping(value = "/page/{page}")
@ResponseBody
public String index(HttpSession httpSession, @PathVariable int page, @RequestParam(value = "limit", defaultValue = "6") int limit) {
//测试用户
UserEntity user = (UserEntity) httpSession.getAttribute("user");
HREntity hr = (HREntity) httpSession.getAttribute("hr");
// UserEntity user = new UserEntity();
// user = userService.getUser(5);
//推荐职位列表
page = (page < 1 || page > GlobalConst.MAX_PAGE) ? 1 : page;
Map output = new TreeMap();
try {
List<PositionCompanyBO> posInfo = positionService.recPosition(user, page, limit);
output.put("posInfo", posInfo);
} catch (Exception ex) {
return "out";
}
if (user != null) {
output.put("user", user);
}
if (hr != null) {
output.put("hr", hr);
}
JSONObject jsonObject = JSONObject.fromObject(output);
return jsonObject.toString();
}
/**
* 职位搜索页分页输出 (关键字,职位列表)
*
* @param request
* @param keyword
* @param page
* @param limit
* @return
*/
@PostMapping(value = "/search")
@ResponseBody
public String search(HttpServletRequest request, @RequestParam(value = "keyword",defaultValue="") String keyword,
@RequestParam(value="orderBy",defaultValue = "salaryUp") String orderBy,@RequestParam(value="page",defaultValue = "1") int page,
@RequestParam(value = "limit", defaultValue = "6") int limit) {
UserEntity user = this.getUser(request);
page = page < 1 || page > GlobalConst.MAX_PAGE ? 1 : page;
PageInfo<PositionCompanyBO> posInfo = positionService.searchPosition(keyword,orderBy, page, limit);
Map output = new TreeMap();
output.put("user",user);
output.put("title", ("第" + page + "页"));
output.put("keyword", keyword);
output.put("orderBy", orderBy);
output.put("posInfo", posInfo);
JSONObject jsonObject = JSONObject.fromObject(output);
return jsonObject.toString();
}
/**
* 职位分类页分页输出 (职位分类,职位列表)
*
* @param request
* @param id
* @param page
* @param limit
* @return
*/
@PostMapping(value = "/category/{id}/{page}")
@ResponseBody
public String list(HttpServletRequest request, @PathVariable int id, @PathVariable int page, @RequestParam(value = "limit", defaultValue = "12") int limit) {
page = page < 1 || page > GlobalConst.MAX_PAGE ? 1 : page;
CategoryEntity category = categoryService.getCategory(id);
if (category == null) {
this.errorDirect_404();
}
PageInfo<PositionCompanyBO> posInfo = positionService.listPosition(id, page, limit);
Map output = new TreeMap();
output.put("title", ("第" + page + "页"));
output.put("category", category);
output.put("posInfo", posInfo);
JSONObject jsonObject = JSONObject.fromObject(output);
return jsonObject.toString();
}
/**
* 职位细节页 评论分页输出 (职位,部门,公司,分类,评论列表)
*
* @param httpSession
* @param id
* @param page
* @param limit
* @return
*/
@PostMapping(value = "/position/{id}/{page}")
@ResponseBody
public String getPosition(HttpSession httpSession, @PathVariable int id, @PathVariable int page,
@RequestParam(value = "limit", defaultValue = "12") int limit) {
PositionEntity position = positionService.getPositionById(id);
if (position == null) {
this.errorDirect_404();
}
//当前用户信息
UserEntity user = (UserEntity) httpSession.getAttribute("user");
HREntity hr = (HREntity) httpSession.getAttribute("hr");
//所属部门信息
DepartmentEntity department = departmentService.getDepartment(position.getDepartmentId());
//所属公司信息
CompanyEntity company = companyService.getCompany(department.getCompanyId());
//职位所属分类信息
CategoryEntity category = categoryService.getCategory(position.getCategoryId());
//分页评论信息