package cn.itcast.web.controller.cargo;
import cn.itcast.dao.cargo.entrustDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.BoxService;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.entrustService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.DownloadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
@Controller
@RequestMapping("/cargo/shipping")
public class entrustController extends BaseController {
@Reference
private BoxService boxService;
@Reference
private ExportService exportService;
@Reference
private entrustService entrustService;
@RequestMapping("/toAdd")
public String toAdd(String id){
//通过装箱id 查询 装箱对象
Box box = boxService.findById(id);
//把箱子的东西保存到域名里面
request.setAttribute("box",box );
//获取多条报运单
String exportIds = box.getExportIds();
//切割报运单 得到多个单包运单
String[] split = exportIds.split(",");
//因为要获取里面的 各大运输 而且 他们相同所有获取一条即可 查询报运单
Export export = exportService.findById(split[0]);
request.setAttribute("export",export);
return "cargo/entrust/entrust-add";
}
@RequestMapping("/add")
public String add(Entrust entrust){
entrust.setCompanyId(getLoginCompanyId());
entrust.setCompanyName(getLoginCompanyName());
//封装剩下的信息
entrustService.add(entrust);
return "redirect:/cargo/shipping/list.do";
}
@RequestMapping("/list")
public String list(@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "5") Integer pageSize){
EntrustExample entrustExample = new EntrustExample();
EntrustExample.Criteria criteria = entrustExample.createCriteria();
criteria.andCompanyIdEqualTo(getLoginCompanyId());
PageInfo pageInfo = entrustService.findByPage(entrustExample,pageNum,pageSize);
request.setAttribute("pageInfo",pageInfo );
return "cargo/entrust/entrust-list";
}
@RequestMapping("/delete")
public String delete(String id){
entrustService.delete(id);
return "redirect:/cargo/shipping/list.do";
}
@RequestMapping("/toInvoice")
public String toInvoice(String id){
return "";
}
@RequestMapping("/toExcel")
public void toExcel(String id) throws IOException {
//读取Excel的模板
InputStream inputStream = session.getServletContext()
.getResourceAsStream("/make/xlsprint/tuoyun.xlsx");
//1.创建工作簿
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
//2.获取第一张工作表
XSSFSheet sheet = workbook.getSheetAt(0);
//3.获取模板的内容行的每列的样式
CellStyle[] cellStyles = new CellStyle[14];
//获取标模板的样式
for (int i = 0; i < cellStyles.length; i++) {
cellStyles[i] = sheet.getRow(1).getCell(i).getCellStyle();
}
//查询数据
EntrustExample entrustExample = new EntrustExample();
EntrustExample.Criteria criteria = entrustExample.createCriteria();
if (id != null && !id.equals("a")) {
String[] split = id.substring(0, id.length()).split(",");
criteria.andShippingOrderIdIn(Arrays.asList(split));
}
criteria.andCompanyIdEqualTo(getLoginCompanyId());
List<Entrust> entrust = entrustService.findAll(entrustExample);
//创建内容行
XSSFCell cell = null;
/*
if (entrust!=null&&entrust.size()>0){
for (int i = 0; i < entrust.size(); i++) {
//取出每个数据
Entrust entrust1 = entrust.get(i);
XSSFRow row = sheet.createRow(2 + i);
//设置行高
row.setHeightInPoints(24);
//获取单元格
//设置里面的每个值
cell = row.createCell(0);
cell.setCellStyle(cellStyles[0]);
cell.setCellValue(entrust1.getShippingOrderId() != null ? entrust1.getShippingOrderId() : "");
cell = row.createCell(1);
cell.setCellStyle(cellStyles[1]);
cell.setCellValue(entrust1.getOrderType() != null ? entrust1.getOrderType() : "");
cell = row.createCell(2);
cell.setCellStyle(cellStyles[2]);
cell.setCellValue(entrust1.getShipper() != null ? entrust1.getShipper() : "");
cell = row.createCell(3);
cell.setCellStyle(cellStyles[3]);
cell.setCellValue(entrust1.getConsignee() != null ? entrust1.getConsignee() : "");
cell = row.createCell(4);
cell.setCellStyle(cellStyles[4]);
cell.setCellValue(entrust1.getNotifyParty() != null ? entrust1.getNotifyParty() : "");
cell = row.createCell(5);
cell.setCellStyle(cellStyles[5]);
cell.setCellValue(entrust1.getLcNo() != null ? entrust1.getLcNo() : "");
cell = row.createCell(6);
cell.setCellStyle(cellStyles[6]);
cell.setCellValue(entrust1.getPortOfLoading() != null ? entrust1.getPortOfLoading() : "");
cell = row.createCell(7);
cell.setCellStyle(cellStyles[7]);
cell.setCellValue(entrust1.getPortOfTrans() != null ? entrust1.getPortOfTrans() : "");
cell = row.createCell(8);
cell.setCellStyle(cellStyles[8]);
cell.setCellValue(entrust1.getPortOfDischar() != null ? entrust1.getPortOfDischar() : "");
cell = row.createCell(9);
cell.setCellStyle(cellStyles[9]);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd");
cell.setCellValue(entrust1.getLoadingDate() != null ? simpleDateFormat.format(entrust1.getLoadingDate()) : "");
cell = row.createCell(10);
cell.setCellStyle(cellStyles[10]);
cell.setCellValue(entrust1.getIsBatch() != null ? entrust1.getIsBatch() : "");
cell = row.createCell(11);
cell.setCellStyle(cellStyles[11]);
cell.setCellValue(entrust1.getIsTrans() != null ? entrust1.getIsTrans() : "");
cell = row.createCell(12);
cell.setCellStyle(cellStyles[12]);
cell.setCellValue(entrust1.getRemark() != null ? entrust1.getRemark() : "");
cell = row.createCell(13);
cell.setCellStyle(cellStyles[13]);
if (entrust1.getState() == null) {
cell.setCellValue("");
} else {
switch (entrust1.getState()) {
case 0:
cell.setCellValue("草稿");
break;
case 1:
cell.setCellValue("已经上报");
break;
case 2:
cell.setCellValue("已报运");
break;
}
}
}
*/
if (entrust != null && entrust.size() > 0) {
for (int i = 0; i < entrust.size(); i++) {
//取出每个数据
Entrust entrust1 = entrust.get(i);
XSSFRow row = sheet.createRow(2 + i);
//设置行高
row.setHeightInPoints(24);
//获取单元格
//设置里面的每个值
try {
cell = row.createCell(0);
cell.setCellStyle(cellStyles[0]);
cell.setCellValue(entrust1.getShippingOrderId());
cell = row.createCell(1);
cell.setCellStyle(cellStyles[1]);
cell.setCellValue(entrust1.getOrderType());
cell = row.createCell(2);
cell.setCellStyle(cellStyles[2]);
cell.setCellValue(entrust1.getShipper());
cell = row.createCell(3);
cell.setCellStyle(cellStyles[3]);
cell.setCellValue(entrust1.getConsignee());
cell = row.createCell(4);
cell.setCellStyle(cellStyles[4]);
cell.setCellValue(entrust1.getNotifyParty());
cell = row.createCell(5);
cell.setCellStyle(cellStyles[5]);
cell.setCellValue(entrust1.getLcNo());
cell = row.createCell(6);
cell.setCellStyle(cellStyles[6]);
cell.setCellValue(entrust1.getPortOfLoading());
cell = row.createCell(7);
cell.setCellStyle(cellStyles[7]);
cell.setCellValue(entrust1.getPortOfTrans());
cell = row.createCell(8);
cell.setCellStyle(cellStyles[8]);
cell.setCellValue(entrust1.getPortOfDischar());
cell = row.createCell(9);
cell.setCellStyle(cellStyles[9]);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd");
cell.setCellValue(simpleDateFormat.format(entrust1.getLoadingDate()));
cell = row.createCell(10);
cell.setCellStyle(cellStyles[10]);
cell.setCellValue(entrust1.getIsBatch());
cell = row.createCell(11);
cell.setCellStyle(cellStyles[11]);
cell.setCellValue(entrust1.getIsTrans());
cell = row.createCell(12);
cell.setCellStyle(cellStyles[12]);
cell.setCellValue(entrust1.getRemark());
cell = row.createCell(13);
cell.setCellStyle(cellStyles[13]);
switch (entrust1.getState()) {
case 0:
cell.setCellValue("草稿");
break;
case 1:
cell.setCellValue("已经上报");
break;
case 2:
cell.setCellValue("已报运");
break;
}
}catch (Exception e){
cell.setCellValue("");
}
}
//7.把工作簿作为文件流写出给用户(response)
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//把工作簿字节数据写入到ByteArrayOutputStream流
workbook.write(byteArrayOutputStream);
new DownloadUtil().download(byteArrayOutputStream,response,"出货表.xlsx");
}
}
}