package com.byr.warehouse.controller;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.byr.warehouse.Service.EntrepotStatusService;
import com.byr.warehouse.Service.ExcelService;
import com.byr.warehouse.Service.LogService;
import com.byr.warehouse.constant.ApplyStatus;
import com.byr.warehouse.constant.Operation;
import com.byr.warehouse.dao.ApplyOutPutRepository;
import com.byr.warehouse.dao.CommonRepository;
import com.byr.warehouse.dao.EntrepotStatusRepository;
import com.byr.warehouse.myexception.StoreException;
import com.byr.warehouse.pojo.ApplyEnter;
import com.byr.warehouse.pojo.ApplyOutPut;
import com.byr.warehouse.pojo.EntrepotStatus;
import com.byr.warehouse.pojo.User;
import com.byr.warehouse.util.PageUtil;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
@Controller
public class ApplyOutController {
Logger logger = LoggerFactory.getLogger(ApplyOutController.class.getName());
@Autowired
private EntrepotStatusRepository entrepotStatusRepository;
@Autowired
private ApplyOutPutRepository applyOutPutRepository;
@Autowired
private EntrepotStatusService entrepotStatusService;
@Resource
private ResourceLoader resourceLoader;
@Autowired
private ExcelService<ApplyOutPut> excelService;
// 通过@Resource注解引入JdbcTemplate对象
@Resource
private JdbcTemplate jdbcTemplate;
@Autowired
private CommonRepository<ApplyOutPut> commonRepository;
@Autowired
private LogService logService;
private Integer pagesize = 20;//每页显示的条数
/**
* @param applyOutPut
* @param pagenum
* @param modelMap
* @return
*/
@RequestMapping("/applyout-getHistory")
public String getHistory(ApplyOutPut applyOutPut, int pagenum, ModelMap modelMap) {
String page = "exit_apply_history";
if (applyOutPut != null) {
StringBuffer sql = null;
try {
sql = commonRepository.getFiledValues(applyOutPut, pagenum);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
sql.append("Status ='已确认' OR Status ='已退回'");
int totalpage = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper(ApplyOutPut.class)).size();
sql.append(" LIMIT " + (pagenum - 1) * pagesize + "," + pagesize);
List<ApplyOutPut> applyOutPuts = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper(ApplyOutPut.class));
modelMap.addAttribute("applys", applyOutPuts);
modelMap.addAttribute("page", pagenum);
modelMap.addAttribute("totalpage", PageUtil.getTotalPage(totalpage, pagesize));
} else {
Pageable pageable = PageRequest.of(pagenum, pagesize);
Page<ApplyOutPut> pager = applyOutPutRepository.findApplyOutPutByStatus(ApplyStatus.ENSURE, pageable);
modelMap.addAttribute("applys", pager.getContent());
modelMap.addAttribute("page", pagenum);
modelMap.addAttribute("totalpage", pager.getTotalPages());
}
return page;
}
//获得未确认的当前登陆人的申请信息
@RequestMapping("/applyout-getNotAllowed")
public String getNotAllowed(ApplyOutPut applyOutPut, int pagenum, ModelMap modelMap, HttpServletRequest request) throws Exception {
String page = "exit_apply";
//获得当前登陆用户的ID
User user = (User) request.getSession().getAttribute("user");
if(user==null){
throw new StoreException("用户尚未登录");
}
Long userId = user.getId();
if (applyOutPut != null) {
StringBuffer sql = null;
try {
sql = commonRepository.getFiledValues(applyOutPut, pagenum);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
sql.append(" Status !='已确认' AND Status !='已退回' AND applyPersonId = '" + user.getUsername()+"'");
int totalpage = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper(ApplyOutPut.class)).size();
sql.append(" LIMIT " + (pagenum - 1) * pagesize + "," + pagesize);
List<ApplyOutPut> applyEnters = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper(ApplyOutPut.class));
modelMap.addAttribute("applys", applyEnters);
modelMap.addAttribute("page", pagenum);
modelMap.addAttribute("totalpage", PageUtil.getTotalPage(totalpage, pagesize));
} else {
Pageable pageable = PageRequest.of(pagenum, pagesize);
Page<ApplyOutPut> pager = applyOutPutRepository.findApplyOutPutByStatusNot(ApplyStatus.ENSURE, pageable);
modelMap.addAttribute("applys", pager.getContent());
modelMap.addAttribute("page", pagenum);
modelMap.addAttribute("totalpage", pager.getTotalPages());
}
modelMap.addAttribute("username", user.getUsername());
return page;
}
//需要审批的申请
@RequestMapping("/applyout-getToBeEnsured")
public String getToBeEnsured(ApplyOutPut applyOutPut, int pagenum, ModelMap modelMap, HttpServletRequest request) {
String page = "exit_apply_wait";
//获得当前登陆用户的ID
if (applyOutPut != null) {
StringBuffer sql = null;
try {
sql = commonRepository.getFiledValues(applyOutPut, pagenum);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
sql.append(" Status !='已确认' AND Status != '被拒绝' AND Status != '已退回'");
int totalpage = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper(ApplyOutPut.class)).size();
sql.append(" LIMIT " + (pagenum - 1) * pagesize + "," + pagesize);
List<ApplyOutPut> applyEnters = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper(ApplyOutPut.class));
modelMap.addAttribute("applys", applyEnters);
modelMap.addAttribute("page", pagenum);
modelMap.addAttribute("totalpage", PageUtil.getTotalPage(totalpage, pagesize));
} else {
Pageable pageable = PageRequest.of(pagenum, pagesize);
Page<ApplyOutPut> pager = applyOutPutRepository.findApplyOutPutByStatusNot(ApplyStatus.ENSURE, pageable);
modelMap.addAttribute("applys", pager.getContent());
modelMap.addAttribute("page", pagenum);
modelMap.addAttribute("totalpage", pager.getTotalPages());
}
return page;
}
/**
* 新增入库申请 跳转待审核页面
*/
@RequestMapping(value = "/applyout-addapply", method = {RequestMethod.GET, RequestMethod.POST})
public String saveApply(ApplyOutPu