数据图表的数据的统计

数据图表展示

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){
        //获取分页结果集
        //PageInfo pageInfo = customerReportService.selectCustomerReport(qo);
        //List list = pageInfo.getList();
        //获取所有数据(不分页)
        List<Map> list = customerReportService.listAll(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储y轴的数据
        ArrayList yList = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            yList.add(map.get("number"));
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        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);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储饼图的数据
        ArrayList yList = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        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); // {value: 335, name: '孙总'}
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        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;
    }
}

你可能感兴趣的:(图表数据,java)