add export customers

This commit is contained in:
Shaun Chyxion 2018-04-04 23:02:51 +08:00
parent e6765c4462
commit de9af1b43e
4 changed files with 54 additions and 19 deletions

View File

@ -9,8 +9,8 @@ import me.chyxion.tigon.model.ViewModel;
import javax.validation.constraints.Max; import javax.validation.constraints.Max;
import javax.validation.constraints.Min; import javax.validation.constraints.Min;
import me.chyxion.tigon.model.ListResult; import me.chyxion.tigon.model.ListResult;
import me.chyxion.tigon.webmvc.ResourceModel;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import me.chyxion.tigon.webmvc.ResourceModel;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import com.pudonghot.ambition.crm.model.User; import com.pudonghot.ambition.crm.model.User;
@ -156,7 +156,7 @@ public class CustomerController
} }
@RequiresRoles(User.ROLE_ADMIN) @RequiresRoles(User.ROLE_ADMIN)
@RequestMapping(value = "/export", method = RequestMethod.POST) @RequestMapping("/export")
public ResourceModel exportCSV() { public ResourceModel exportCSV() {
return new ResourceModel(((CustomerService) queryService).exportCSV(getUserId()), null, null); return new ResourceModel(((CustomerService) queryService).exportCSV(getUserId()), null, null);
} }

View File

@ -1,18 +1,15 @@
package com.pudonghot.ambition.crm.service.support; package com.pudonghot.ambition.crm.service.support;
import java.io.*;
import java.util.*; import java.util.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import me.chyxion.tigon.mybatis.Search; import me.chyxion.tigon.mybatis.Search;
import me.chyxion.tigon.model.ViewModel; import me.chyxion.tigon.model.ViewModel;
import org.apache.commons.csv.CSVRecord; import org.apache.commons.csv.CSVRecord;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import com.pudonghot.ambition.crm.model.*; import com.pudonghot.ambition.crm.model.*;
import me.chyxion.tigon.model.ListResult; import me.chyxion.tigon.model.ListResult;
import com.pudonghot.ambition.crm.mapper.*; import com.pudonghot.ambition.crm.mapper.*;
@ -66,7 +63,8 @@ public class CustomerServiceSupport
CSVFormat.DEFAULT.withHeader( CSVFormat.DEFAULT.withHeader(
"ID", "ID",
"Name", "Name",
"Sales(3 Years)", "Sum Sales(3 Years)",
"Sales",
"Comment", "Comment",
"Application", "Application",
"Status", "Status",
@ -441,16 +439,22 @@ public class CustomerServiceSupport
*/ */
@Override @Override
public File exportCSV(final String operator) { public File exportCSV(final String operator) {
// final File file = new File(FileUtils.getTempDirectory(), final File file = new File(FileUtils.getTempDirectory(),
final File file = new File(FileUtils.getUserDirectory(), "Customers_" + DateFormatUtils.format(System.currentTimeMillis(),
DateFormatUtils.format(System.currentTimeMillis(),
"yyyy-MM-dd_HHmmss") + ".csv"); "yyyy-MM-dd_HHmmss") + ".csv");
try (final FileWriter fileWriter = new FileWriter(file); try (final OutputStreamWriter fileWriter =
new OutputStreamWriter(new FileOutputStream(file), CSVUtils.GBK);
final CSVPrinter printer = EXPORT_FORMAT.print(fileWriter)) { final CSVPrinter printer = EXPORT_FORMAT.print(fileWriter)) {
scan(512, new Search(), this::exportList, this::exportCount, c -> {
scan(512, new Search()
.setAttr("APPLICATION_NOT_NULL", true)
.desc(Customer.SUM_YTD_SALES),
this::exportList,
this::exportCount, c -> {
// "ID", // "ID",
// "Name", // "Name",
// "Sales(3 Years)", // "Sum Sales(3 Years)",
// "Sales",
// "Comment", // "Comment",
// "Application", // "Application",
// "Status", // "Status",
@ -464,10 +468,11 @@ public class CustomerServiceSupport
// "Sales Person"); // "Sales Person");
try { try {
log.info("Customer [{}].", c); log.info("Export Customer [{}].", c);
printer.printRecord(c.getId(), printer.printRecord(c.getId(),
c.getName(), c.getName(),
c.getYtdSales(), c.getSumYtdSales(),
joinYtdSales(c.getYears(), c.getYtdSales()),
extractIssue(c.getLastIssue()), extractIssue(c.getLastIssue()),
joinApplicationNames(c.getApplicationNames()), joinApplicationNames(c.getApplicationNames()),
c.getStatusText(), c.getStatusText(),
@ -502,6 +507,7 @@ public class CustomerServiceSupport
int exportCount(final Search search) { int exportCount(final Search search) {
return mapper.countForShow(search, null); return mapper.countForShow(search, null);
} }
private String extractIssue(final String issue) { private String extractIssue(final String issue) {
if (StringUtils.isNotBlank(issue)) { if (StringUtils.isNotBlank(issue)) {
return issue.split(SPLITTER)[1]; return issue.split(SPLITTER)[1];
@ -516,6 +522,22 @@ public class CustomerServiceSupport
return ""; return "";
} }
private String joinYtdSales(final String strYears, final String strYtdSales) {
if (StringUtils.isNotBlank(strYears) &&
StringUtils.isNotBlank(strYtdSales)) {
final String[] years = strYears.split(SPLITTER);
final String[] sales = strYtdSales.split(SPLITTER);
final List<String> ytdSales = new ArrayList<>(years.length);
int i = 0;
for (final String year : years) {
ytdSales.add(year + ": " + sales[i++]);
}
return StringUtils.join(ytdSales, "\n");
}
return "";
}
private String dateFormat(Date date, String pattern) { private String dateFormat(Date date, String pattern) {
return date != null ? DateFormatUtils.format(date, pattern) : ""; return date != null ? DateFormatUtils.format(date, pattern) : "";
} }

View File

@ -153,14 +153,14 @@
and enabled = 1) year, and enabled = 1) year,
<!-- all years list --> <!-- all years list -->
(select group_concat(year order by year asc separator 0x1d) (select group_concat(year order by year desc separator 0x1d)
from crm_customer_year_to_date_sale from crm_customer_year_to_date_sale
where customer_id = customer.id where customer_id = customer.id
and enabled = 1 and enabled = 1
group by customer_id) years, group by customer_id) years,
<!-- all sales list --> <!-- all sales list -->
(select group_concat(ytd_sale order by year asc separator 0x1d) (select group_concat(ytd_sale order by year desc separator 0x1d)
from crm_customer_year_to_date_sale from crm_customer_year_to_date_sale
where customer_id = customer.id where customer_id = customer.id
and enabled = 1 and enabled = 1
@ -214,6 +214,13 @@
</sql> </sql>
<sql id="showFilter"> <sql id="showFilter">
<!-- Application not null -->
<if test="s.getAttr('APPLICATION_NOT_NULL') != null">
join (select distinct customer_id
from crm_customer_application) app_not_null
on customer.id = app_not_null.customer_id
</if>
<if test="s.getAttr('APPLICATIONS') != null"> <if test="s.getAttr('APPLICATIONS') != null">
join (select distinct customer_id join (select distinct customer_id
from crm_customer_application from crm_customer_application

View File

@ -22,6 +22,12 @@
Import Sales Data Import Sales Data
{{/link-to}} {{/link-to}}
</li> </li>
<li>
<a href="/customer/export" target="_blank">
<i class="ace-icon fa fa-download bigger-110"></i>
Export Customers
</a>
</li>
{{/if}} {{/if}}
<li> <li>
{{#link-to 'customer.list.advanced-query'}} {{#link-to 'customer.list.advanced-query'}}