ssjygl-xsct-service/ssjygl-xsx-common/src/main/java/com/cowr/ssjygl/prepay/PrepayService.java

695 lines
27 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.cowr.ssjygl.prepay;
import com.cowr.common.utils.DateTimeUtil;
import com.cowr.model.*;
import com.cowr.ssjygl.CacheData;
import com.cowr.common.Const;
import com.cowr.common.utils.DataUtil;
import com.cowr.common.view.PageParam;
import com.cowr.common.enums.OrderStateEnum;
import com.cowr.ssjygl.customer.receiver.CustomerReceiverService;
import com.cowr.ssjygl.prepay.prepaycustomer.PrepayCustomerService;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class PrepayService {
private static Log log = Log.getLog(PrepayService.class);
public static PrepayService me = new PrepayService();
/**
* 预付费用户列表
*
* @param pp 分页参数
* @param name 客户名称
* @param customer_id 客户id
* @param supermarket_id 超时id
* @param stm 开始时间
* @param etm 截止时间
* @param start 开始金额
* @param end 截止金额
* @return
*/
public Page<Record> find(PageParam pp, String name, Integer customer_id, Integer supermarket_id, String stm, String etm, Double start, Double end) {
String selectsql = " select p.*, c.name customer_name, c.address, c.texpayer_name, c.texpayer_num, c.memo, pc.surplus, s.name supermarket_name \n";
String fromsql = " from ( \n" +
" select t.customer_id, t.supermarket_id, max(t.create_time) maxtm, max(t.id) maxid from prepay_detail t \n" +
" where t.state = 2 \n" +
" group by t.customer_id, t.supermarket_id\n" +
" ) a \n" +
" left join prepay_detail p on p.id = a.maxid \n" +
" left join customer c on c.id = p.customer_id \n" +
" left join prepay_customer pc on pc.customer_id = p.customer_id and pc.supermarket_id = a.supermarket_id\n" +
" left join supermarket s on s.id = a.supermarket_id\n" +
" where 1 = 1";
List<Object> paraList = new ArrayList<>();
if (StrKit.notBlank(name)) {
fromsql += " and c.name like ? \n";
paraList.add("%" + name + "%");
}
if (customer_id != null) {
fromsql += " and a.customer_id = ? \n";
paraList.add(customer_id);
}
if (supermarket_id != null) {
fromsql += " and a.supermarket_id = ? \n";
paraList.add(supermarket_id);
}
if (StrKit.notBlank(stm)) {
fromsql += " and p.create_time >= ?";
paraList.add(stm);
}
if (StrKit.notBlank(etm)) {
fromsql += " and p.create_time <= ?";
paraList.add(etm);
}
if (start != null) {
fromsql += " and pc.surplus >= ?";
paraList.add(start);
}
if (end != null) {
fromsql += " and pc.surplus <= ?";
paraList.add(end);
}
String totalRowSql = "select count(*) " + fromsql;
String findSql = selectsql + fromsql;
// 前端传了排序字段,并且排序字段存在相关表中
if (StrKit.notBlank(pp.getSort_field()) &&
(PrepayDetail.dao.hasColunm(pp.getSort_field()) || Customer.dao.hasColunm(pp.getSort_field()))
) {
findSql += " order by p." + pp.getSort_field() + " is null, p." + pp.getSort_field();
if (Const.ORDER_BY_ASC.equals(pp.getSort_order())) {
findSql += " " + Const.ORDER_BY_ASC;
} else {
findSql += " " + Const.ORDER_BY_DESC;
}
} else {
findSql += " order by p.change_time desc";
}
return Db.paginateByFullSql(pp.getPage(), pp.getSize(), totalRowSql, findSql, paraList.toArray());
}
public Record prepayInfo(Transport transport) {
if (transport == null) {
return null;
}
PrepayTruck pt = PrepayTruck.dao.findFirst("select * from prepay_truck t \n" +
" where t.supermarket_id = ? \n" +
" and t.truck_license = ? \n" +
" and t.valid_date = ? ",
transport.getSupermarketId(),
transport.getTruckLicense(),
DateTimeUtil.sdf.get().format(transport.getInTime())
);
if (pt == null) {
return null;
}
Customer customer = Customer.dao.findById(pt.getCustomerId());
if (customer == null) {
return null;
}
if (customer.getType() == 0) {
log.error("当前用户不是预付用户 customer id " + customer.getId());
return null;
}
PrepayCustomer prepayCustomer = PrepayCustomerService.me.getPrepayCustomer(customer.getId());
if (prepayCustomer == null) {
log.debug("未找到预付费信息 %s, %s" + customer.getId() + ", " + transport.getSupermarketId());
return null;
}
PrepayDetail pd = PrepayDetail.dao.findFirst(
"select * from prepay_detail t where t.customer_id = ? \n" +
" order by t.create_time desc limit 0,1", customer.getId());
if (pd == null) {
return null;
}
Record out = new Record();
out.set("prepay_truck", true);
out.set("prepay_threshold", prepayCustomer.getThreshold());
out.set("prepay_truck_type", pt.getType());
out.set("prepay_customer_id", customer.getId());
out.set("prepay_customer_name", customer.getName());
out.set("prepay_contact_name", pd.getContactName());
out.set("prepay_contact_phone", pd.getContactPhone());
out.set("prepay_surplus", prepayCustomer.getSurplus());
out.set("prepay_customer", customer);
out.set("prepay_customer_info", prepayCustomer);
Truck truck = Truck.dao.findById(transport.getTruckLicense());
if(truck != null){
// 预付费关联集团客户订单
List<Record> list = Db.find("select t.*, c.`name` customer_name from ordercluster t\n" +
" left join customer c on c.id = t.customer_id\n" +
" where t.state < ? \n" +
" and t.supermarket_id = ? \n" +
" and t.trans_co_id = ? \n" +
" and t.customer_id = ? ",
OrderStateEnum.RECEIVED.getStateid(),
transport.getSupermarketId(),
truck.getTransCoId(),
pt.getCustomerId()
);
out.set("prepay_ordercluster", list);
}
List<Record> list = CustomerReceiverService.me.list(customer.getId(), null, null, transport.getSupermarketId());
out.set("prepay_customer_receiver", list);
return out;
}
/**
* 消费查询
*
* @param customer_id
* @param supermarket_id
* @param stm
* @param etm
* @return
*/
public List<Record> consumption(Integer customer_id, Integer supermarket_id, String stm, String etm) {
String sql = "select a.sn \n" +
" ,p.order_sn \n" +
" ,p.arrive_time \n" +
" ,p.type \n" +
" ,a.supermarket_id \n" +
" ,s.`name` supermarket_name \n" +
" ,a.truck_license \n" +
" ,a.weight \n" +
" ,a.total_price \n" +
" ,a.paid \n" +
" ,a.customer_id \n" +
" ,a.customer_name \n" +
" from ( \n" +
" select t.sn, t.supermarket_id, t.truck_license, t.weight, t.total_price, t.paid, c.id customer_id, c.name customer_name \n" +
" from order_sale t \n" +
" left join customer c on c.id = t.customer_id \n" +
" where t.state = ? \n" +
" and t.isprepaid = 1 \n" +
" and t.prepay_customer_id is not null \n" +
" and t.create_time >= ? \n" +
" and t.create_time <= ? \n" +
" union \n" +
" select t.sn, t.supermarket_id, t.truck_license, t.weight, t.total_price, t.paid, c.id customer_id, c.name customer_name \n" +
" from order_temp t \n" +
" left join customer c on c.id = t.customer_id \n" +
" where t.state = ? \n" +
" and t.isprepaid = 1 \n" +
" and t.prepay_customer_id is not null \n" +
" and t.create_time >= ? \n" +
" and t.create_time <= ? \n" +
" ) a \n" +
" left join supermarket s on s.id = a.supermarket_id \n" +
" left join transport p on p.order_sn = a.sn " +
" where 1 = 1 \n";
List<Object> paraList = new ArrayList<>();
paraList.add(OrderStateEnum.RECEIVED.getStateid());
paraList.add(stm);
paraList.add(etm);
paraList.add(OrderStateEnum.RECEIVED.getStateid());
paraList.add(stm);
paraList.add(etm);
if (supermarket_id != null && supermarket_id > 0) {
sql += " and a.supermarket_id = ?";
paraList.add(supermarket_id);
}
if (customer_id != null && customer_id > 0) {
sql += " and a.customer_id = ?";
paraList.add(customer_id);
}
return Db.find(sql, paraList.toArray());
}
public Workbook consumptionExport(Integer customer_id, Integer supermarket_id, String stm, String etm) {
Customer customer = Customer.dao.findById(customer_id);
String cname = "";
if (customer != null) {
cname = customer.getName();
}
List<Record> list = consumption(customer_id, supermarket_id, stm, etm);
list.sort(new Comparator<Record>() {
public int compare(Record o1, Record o2) {
long d1 = o1.getDate("arrive_time").getTime();
long d2 = o2.getDate("arrive_time").getTime();
if (d1 > d2) {
return 1;
} else if (d1 == d2) {
return 0;
}
return -1;
}
});
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet(cname + " 消费记录");
// 标题 start
Font font = wb.createFont();
font.setBold(true);
font.setFontHeight((short) (18 * 20));
font.setFontName("宋体");
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setFont(font);
cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue(CacheData.print_vendor);
row.setHeight((short) (40 * 20)); // POI中的行高单位是twips ,Office Excel行高单位是pt 设置的行高单位为1/20pt
cell.setCellStyle(cellStyle);
// 标题 end
// 副标题 start
String subtitle = cname;
try {
subtitle += DateTimeUtil.sdfymd.get().format(DateTimeUtil.sdfhms.get().parse(stm)) + " ~ " + DateTimeUtil.sdfymd.get().format(DateTimeUtil.sdfhms.get().parse(etm));
} catch (Exception e) {
log.error(e.getMessage(), e);
}
subtitle += "消费记录";
font = wb.createFont();
font.setFontHeight((short) (12 * 20));
font.setFontName("宋体");
cellStyle = wb.createCellStyle();
cellStyle.setFont(font);
cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
row = sheet.createRow(1);
row.setHeight((short) (28 * 20));
cell = row.createCell(0);
cell.setCellValue(subtitle);
cell.setCellStyle(cellStyle);
// 副标题 end
// 设置表头
// 表头 start
font = wb.createFont();
cellStyle = wb.createCellStyle();
font.setFontHeight((short) (10 * 20));
font.setFontName("宋体");
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
cellStyle.setFont(font);
row = sheet.createRow(2);
int a = 0;
row.createCell(a++).setCellValue("单号");
row.createCell(a++).setCellValue("时间");
row.createCell(a++).setCellValue("砂站");
row.createCell(a++).setCellValue("车牌号");
row.createCell(a++).setCellValue("购买量(吨)");
row.createCell(a++).setCellValue("金额");
// 表头 end
int end_col = 5;
int datalen = list.size();
double sum_weight = 0.0;
double sum_price = 0.0;
for (int i = 0; i < datalen; i++) {
Record record = list.get(i);
sum_weight += DataUtil.getDefaultByRecord(record, "weight");
sum_price += DataUtil.getDefaultByRecord(record, "total_price");
row = sheet.createRow(i + 3);
a = 0;
row.createCell(a++).setCellValue(record.getStr("sn"));
row.createCell(a++).setCellValue(DateTimeUtil.sdfhms.get().format(record.getDate("arrive_time")));
row.createCell(a++).setCellValue(record.getStr("supermarket_name"));
row.createCell(a++).setCellValue(record.getStr("truck_license"));
row.createCell(a++).setCellValue(DataUtil.getDefaultByRecord(record, "weight"));
row.createCell(a++).setCellValue(DataUtil.getDefaultByRecord(record, "total_price"));
}
// 合计 start
row = sheet.createRow(datalen + 3);
a = 0;
row.createCell(a++).setCellValue("合计");
row.createCell(a++).setCellValue("");
row.createCell(a++).setCellValue("");
row.createCell(a++).setCellValue("");
row.createCell(a++).setCellValue(sum_weight);
row.createCell(a++).setCellValue(sum_price);
// 合计 end
// 设置列宽
sheet.setColumnWidth(0, (int) ((12.5 + 0.71) * 256));
sheet.setColumnWidth(1, (int) ((20.5 + 0.71) * 256));
sheet.setColumnWidth(2, (int) ((12.5 + 0.71) * 256));
sheet.setColumnWidth(3, (int) ((8 + 0.71) * 256));
sheet.setColumnWidth(4, (int) ((10 + 0.71) * 256));
sheet.setColumnWidth(5, (int) ((6 + 0.71) * 256));
// 设置通用单元格格式
for (int r = 2; r < datalen + 4; r++) {
row = sheet.getRow(r);
if (row == null) {
row = sheet.createRow(r);
}
for (int c = 0; c < end_col + 1; c++) {
cell = row.getCell(c);
if (cell == null) {
cell = row.createCell(c);
}
cell.setCellStyle(cellStyle);
}
}
font = wb.createFont();
font.setFontHeight((short) (10 * 20));
font.setFontName("宋体");
cellStyle = wb.createCellStyle();
cellStyle.setFont(font);
cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
row = sheet.createRow(datalen + 4);
row.setHeight((short) (28 * 20));
cell = row.createCell(0);
cell.setCellValue("统计人签名:");
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("复核人签名:");
cell.setCellStyle(cellStyle);
sheet.addMergedRegion(new CellRangeAddress(datalen + 4, datalen + 4, 0, 1));// 下标从0开始 起始行号,终止行号, 起始列号,终止列号
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, end_col));// 下标从0开始 起始行号,终止行号, 起始列号,终止列号
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, end_col));// 下标从0开始 起始行号,终止行号, 起始列号,终止列号
// 每页打印表头
sheet.setRepeatingRows(new CellRangeAddress(0, 2, -1, -1));//前两位数是设置需要重复打印的行的范围,后两位设置重复打印的列的范围。-1代表不重复打印。用于表头表头换页重复打印
wb.setPrintArea(
0, //sheet index
0, //start column
end_col, //end column
0, //start row
sheet.getLastRowNum() //end row
);
return wb;
}
/**
* 客户流水
*
* @return
*/
public List<Record> flow(Integer customer_id, String stm, String etm) {
String sql = "select * from ( \n" +
" select a.sn id, p.arrive_time tm, 0 - a.total_price amount, '扣费' type, \n" +
" case when a.paid > 0 then concat('补差额', a.paid) else '' end memo, a.customer_id, a.customer_name \n" +
" from ( \n" +
" select t.sn, t.total_price, t.paid, t.customer_id, t.customer_name \n" +
" from order_sale t \n" +
" where t.isprepaid = 1 \n" +
" and t.state = ? \n" +
" and t.prepay_customer_id is not null\n" +
" and t.create_time >= ? \n" +
" and t.create_time <= ? \n" +
" union \n" +
" select t.sn, t.total_price, t.paid, t.customer_id, t.customer_name\n" +
" from order_temp t \n" +
" where t.isprepaid = 1 \n" +
" and t.state = ? \n" +
" and t.prepay_customer_id is not null\n" +
" and t.create_time >= ? \n" +
" and t.create_time <= ? \n" +
" ) a \n" +
" left join transport p on p.order_sn = a.sn \n" +
" union \n" +
" select concat('prepay_detail_', t.id) id, t.verify_time tm, t.amount, '付费' type, '' memo, c.id customer_id, c.name customer_name \n" +
" from prepay_detail t \n" +
" left join customer c on c.id = t.customer_id \n" +
" where t.state = 2 \n" +
" and t.verify_time >= ? \n" +
" and t.verify_time <= ? \n" +
" \n" +
" union \n" +
" select concat('refund_detail_', t.id) id, t.verify_time tm, 0 - t.amount amount, '退费' type, '' memo, c.id customer_id, c.name customer_name \n" +
" from refund_detail t \n" +
" left join customer c on c.id = t.customer_id \n" +
" where t.state = 3 \n" + // 退费有三个状态
" and t.verify_time >= ? \n" +
" and t.verify_time <= ? \n" +
" ) b \n" +
" where 1=1 ";
List<Object> paraList = new ArrayList<>();
paraList.add(OrderStateEnum.RECEIVED.getStateid());
paraList.add(stm);
paraList.add(etm);
paraList.add(OrderStateEnum.RECEIVED.getStateid());
paraList.add(stm);
paraList.add(etm);
paraList.add(stm);
paraList.add(etm);
paraList.add(stm);
paraList.add(etm);
if (customer_id != null && customer_id > 0) {
sql += " and b.customer_id = ? \n";
paraList.add(customer_id);
}
sql += " order by b.tm desc";
return Db.find(sql, paraList.toArray());
}
public Workbook flowExport(Integer customer_id, String stm, String etm) {
Customer customer = Customer.dao.findById(customer_id);
String cname = "";
if (customer != null) {
cname = customer.getName();
}
List<Record> list = flow(customer_id, stm, etm);
list.sort(new Comparator<Record>() {
public int compare(Record o1, Record o2) {
long d1 = o1.getDate("tm").getTime();
long d2 = o2.getDate("tm").getTime();
if (d1 > d2) {
return 1;
} else if (d1 == d2) {
return 0;
}
return -1;
}
});
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet(cname + " 流水");
// 标题 start
Font font = wb.createFont();
font.setBold(true);
font.setFontHeight((short) (18 * 20));
font.setFontName("宋体");
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setFont(font);
cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue(CacheData.print_vendor);
row.setHeight((short) (40 * 20)); // POI中的行高单位是twips ,Office Excel行高单位是pt 设置的行高单位为1/20pt
cell.setCellStyle(cellStyle);
// 标题 end
// 副标题 start
String subtitle = cname;
try {
subtitle += DateTimeUtil.sdfymd.get().format(DateTimeUtil.sdfhms.get().parse(stm)) + " ~ " + DateTimeUtil.sdfymd.get().format(DateTimeUtil.sdfhms.get().parse(etm));
} catch (Exception e) {
log.error(e.getMessage(), e);
}
subtitle += "资金流水";
font = wb.createFont();
font.setFontHeight((short) (12 * 20));
font.setFontName("宋体");
cellStyle = wb.createCellStyle();
cellStyle.setFont(font);
cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
row = sheet.createRow(1);
row.setHeight((short) (28 * 20));
cell = row.createCell(0);
cell.setCellValue(subtitle);
cell.setCellStyle(cellStyle);
// 副标题 end
// 设置表头
// 表头 start
font = wb.createFont();
cellStyle = wb.createCellStyle();
font.setFontHeight((short) (10 * 20));
font.setFontName("宋体");
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
cellStyle.setFont(font);
row = sheet.createRow(2);
int a = 0;
row.createCell(a++).setCellValue("时间");
row.createCell(a++).setCellValue("操作");
row.createCell(a++).setCellValue("金额");
row.createCell(a++).setCellValue("备注");
// 表头 end
int end_col = 3;
int datalen = list.size();
double sum_price = 0.0;
for (int i = 0; i < datalen; i++) {
Record record = list.get(i);
sum_price += DataUtil.getDefaultByRecord(record, "amount");
row = sheet.createRow(i + 3);
a = 0;
row.createCell(a++).setCellValue(DateTimeUtil.sdfhms.get().format(record.getDate("tm")));
row.createCell(a++).setCellValue(record.getStr("type"));
row.createCell(a++).setCellValue(DataUtil.getDefaultByRecord(record, "amount"));
row.createCell(a++).setCellValue(record.getStr("memo"));
}
// 合计 start
row = sheet.createRow(datalen + 3);
a = 0;
row.createCell(a++).setCellValue("合计");
row.createCell(a++).setCellValue("");
row.createCell(a++).setCellValue(sum_price);
// 合计 end
// 设置列宽
sheet.setColumnWidth(0, (int) ((20 + 0.71) * 256));
sheet.setColumnWidth(1, (int) ((20 + 0.71) * 256));
sheet.setColumnWidth(2, (int) ((20 + 0.71) * 256));
sheet.setColumnWidth(3, (int) ((20 + 0.71) * 256));
// 设置通用单元格格式
for (int r = 2; r < datalen + 4; r++) {
row = sheet.getRow(r);
if (row == null) {
row = sheet.createRow(r);
}
for (int c = 0; c < end_col + 1; c++) {
cell = row.getCell(c);
if (cell == null) {
cell = row.createCell(c);
}
cell.setCellStyle(cellStyle);
}
}
font = wb.createFont();
font.setFontHeight((short) (10 * 20));
font.setFontName("宋体");
cellStyle = wb.createCellStyle();
cellStyle.setFont(font);
cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
row = sheet.createRow(datalen + 4);
row.setHeight((short) (28 * 20));
cell = row.createCell(0);
cell.setCellValue("统计人签名:");
cell.setCellStyle(cellStyle);
cell = row.createCell(2);
cell.setCellValue("复核人签名:");
cell.setCellStyle(cellStyle);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, end_col));// 下标从0开始 起始行号,终止行号, 起始列号,终止列号
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, end_col));// 下标从0开始 起始行号,终止行号, 起始列号,终止列号
// 每页打印表头
sheet.setRepeatingRows(new CellRangeAddress(0, 2, -1, -1));//前两位数是设置需要重复打印的行的范围,后两位设置重复打印的列的范围。-1代表不重复打印。用于表头表头换页重复打印
wb.setPrintArea(
0, //sheet index
0, //start column
end_col, //end column
0, //start row
sheet.getLastRowNum() //end row
);
return wb;
}
// TODO: 客户流水 和 预付费车辆记录 这两个功能,对应的数据,都应该是该用户第一次预付费记录之后的订单记录
}