数据图表展示
Controller
package cn. wolfcode. crm. web. controller;
import cn. wolfcode. crm. query. CustomerReportQuery;
import cn. wolfcode. crm. service. ICustomerReportService;
import cn. wolfcode. crm. util. MessageUtil;
import com. alibaba. fastjson. JSON;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. stereotype. Controller;
import org. springframework. ui. Model;
import org. springframework. web. bind. annotation. ModelAttribute;
import org. springframework. web. bind. annotation. RequestMapping;
import java. util. ArrayList;
import java. util. HashMap;
import java. util. List;
import java. util. Map;
@Controller
@RequestMapping ( "/customerReport" )
public class CustomerReportController {
@Autowired
private ICustomerReportService customerReportService;
@RequestMapping ( "/list" )
public String list ( Model model, @ModelAttribute ( "qo" ) CustomerReportQuery qo) {
model. addAttribute ( "result" , customerReportService. selectCustomerReport ( qo) ) ;
return "customerReport/list" ;
}
@RequestMapping ( "/listByBar" )
public String listByBar ( Model model, @ModelAttribute ( "qo" ) CustomerReportQuery qo) {
List< Map> list = customerReportService. listAll ( qo) ;
ArrayList xList = new ArrayList ( ) ;
ArrayList yList = new ArrayList ( ) ;
for ( Map map : list) {
xList. add ( map. get ( "groupType" ) ) ;
yList. add ( map. get ( "number" ) ) ;
}
System. out. println ( JSON. toJSONString ( xList) ) ;
model. addAttribute ( "xList" , JSON. toJSONString ( xList) ) ;
model. addAttribute ( "yList" , JSON. toJSONString ( yList) ) ;
model. addAttribute ( "groupTypeName" , MessageUtil. changMsg ( qo) ) ;
return "customerReport/listByBar" ;
}
@RequestMapping ( "/listByPie" )
public String listByPie ( Model model, @ModelAttribute ( "qo" ) CustomerReportQuery qo) {
List< Map> list = customerReportService. listAll ( qo) ;
ArrayList xList = new ArrayList ( ) ;
ArrayList yList = new ArrayList ( ) ;
for ( Map map : list) {
xList. add ( map. get ( "groupType" ) ) ;
HashMap< String, Object> temp = new HashMap < > ( ) ;
temp. put ( "name" , map. get ( "groupType" ) ) ;
temp. put ( "value" , map. get ( "number" ) ) ;
yList. add ( temp) ;
}
model. addAttribute ( "xList" , JSON. toJSONString ( xList) ) ;
model. addAttribute ( "yList" , JSON. toJSONString ( yList) ) ;
model. addAttribute ( "groupTypeName" , MessageUtil. changMsg ( qo) ) ;
return "customerReport/listByPie" ;
}
}
实现类
package cn. wolfcode. crm. service. impl;
import cn. wolfcode. crm. mapper. CustomerReportMapper;
import cn. wolfcode. crm. query. QueryObject;
import cn. wolfcode. crm. service. ICustomerReportService;
import com. github. pagehelper. PageHelper;
import com. github. pagehelper. PageInfo;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. stereotype. Service;
import java. util. List;
import java. util. Map;
@Service
public class CustomerReportServiceImpl implements ICustomerReportService {
@Autowired
private CustomerReportMapper customerReportMapper;
public PageInfo selectCustomerReport ( QueryObject qo) {
PageHelper. startPage ( qo. getCurrentPage ( ) , qo. getPageSize ( ) ) ;
List< Map> list = customerReportMapper. selectCustomerReport ( qo) ;
return new PageInfo ( list) ;
}
public List< Map> listAll ( QueryObject qo) {
return customerReportMapper. selectCustomerReport ( qo) ;
}
}
mappper
< ? xml version= "1.0" encoding= "UTF-8" ? >
< ! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace= "cn.wolfcode.crm.mapper.CustomerReportMapper" >
< select id= "selectCustomerReport" resultType= "map" >
select ${ groupType} groupType, count ( c. id) number from customer c
left join employee e on c. seller_id = e. id
< where>
c. status = 0
< if test= "keyword!=null" >
and e. name like concat ( '%' , #{ keyword} , '%' )
< / if >
< if test= "beginDate!=null" >
and c. input_time >= #{ beginDate}
< / if >
< if test= "endDate!=null" >
and c. input_time & lt; = #{ endDate}
< / if >
< / where>
GROUP BY ${ groupType}
< / select>
< / mapper>
MessageUtil
package cn. wolfcode. crm. util;
import cn. wolfcode. crm. query. CustomerReportQuery;
public abstract class MessageUtil {
public static String changMsg ( CustomerReportQuery qo) {
String msg = null;
switch ( qo. getGroupType ( ) ) {
case "DATE_FORMAT(c.input_time, '%Y')" :
msg = "年份" ;
break ;
case "DATE_FORMAT(c.input_time, '%Y-%m')" :
msg = "月份" ;
break ;
case "DATE_FORMAT(c.input_time, '%Y-%m-%d')" :
msg = "日期" ;
break ;
default :
msg = "员工" ;
}
return msg;
}
}