1、JSP页面
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<script type="text/javascript">
$(function(){
$('#gys').combobox({
url:'${pageContext.request.contextPath}/admin/crmUnitController/allTreeNode.do',
valueField:'id',
textField:'name',
panelHeight:'100px',
editable:false,
panelWidth:155,
onSelect:function(record){
$('#gysid1').val(record.id);
}
});
$('#ckid').combobox({
url:'${pageContext.request.contextPath}/admin/drpStoreController/treeNodeAll.do',
valueField:'id',
textField:'name',
panelHeight:'100px',
editable:false,
panelWidth:155,
onSelect:function(record){
$('#ck1').val(record.id);
}
});
$('#jsr').combobox({
url:'${pageContext.request.contextPath}/admin/userController/allTreeNode.do',
valueField:'id',
textField:'nickname',
panelHeight:'100px',
editable:false,
panelWidth:155,
onSelect:function(record){
$('#jsrid1').val(record.id);
}
});
});
function toSub(){
var gys=$('#gysid1').val();
var ck=$('#ck1').val();
var jsr=$('#jsrid1').val();
var rkstart=$('#rkstart1').val();
var rkend=$('#rkend1').val();
var tu=$('#tu').val();
$.ajax({
url :'${pageContext.request.contextPath }/admin/inStockDetailController/chart2tj.do?gys='+gys+'&ckid='+ck+'&jsr='+jsr+'&rkstart='+rkstart+'&rkend='+rkend+'&tu='+tu,
dataType : 'json',
success : function(result) {
if (result.success) {
if(tu==1){
pie(result.msg);
}else{
bar(result.msg);
}
}
}
});
}
</script>
<script type="text/javascript" src='${pageContext.request.contextPath }/fusioncharts/FusionCharts.js'></script>
<script type="text/javascript">
//3D饼状图
function pie(str){
var chart = new FusionCharts('${pageContext.request.contextPath }/fusioncharts/FusionCharts/Pie3D.swf', "ChartId", "600", "350");
console.info(str);
chart.setDataXML(str);
chart.render("chartdiv");
}
//3D柱状图
function bar(str){
var chart = new FusionCharts('${pageContext.request.contextPath }/fusioncharts/FusionCharts/Column3D.swf', "ChartId", "600", "350");
chart.setDataXML(str);
chart.render("chartdiv");
}
function cgtj_cleanFun() {
$('#cgtj_searchForm input').val('');
$('#cgtj_searchForm').datagrid('load', {});
}
</script>
<div class="easyui-layout" data-options="fit : true,border : false">
<div data-options="region:'north',title:'查询条件',border:false" style="height: 185px;overflow: hidden;" align="center">
<form id="cgtj_searchForm">
<table class="tableForm">
<caption style="margin-top: 10px">
<span><font size="6">采购统计</font>
</span>
</caption>
<tr>
<th>供货商</th>
<td><input id="gys" name="gys" type="text"
class="easyui-combotree"
data-options="url:'${pageContext.request.contextPath}/admin/crmUnitController/allTreeNode.do',parentField : 'pid',lines : true,panelHeight:'100px'"
missingMessage="请选择供货商" style="width: 155px;" /></td>
<th>所在仓库</th>
<td><input id="ckid" name="ckid" type="text"
class="easyui-combotree"
data-options="url:'${pageContext.request.contextPath}/admin/drpStoreController/treeNodeAll.do',parentField : 'pid',lines : true,panelHeight:'100px'"
style="width: 155px;" /></td>
<th>统计图类型</th>
<td align="center"><select id="tu"><option value="1">饼状图</option>
<option value="2">柱状图</option>
</select></td>
</tr>
<tr>
<th>经手人</th>
<td><input id="gysid1" type="hidden"> <input id="ck1"
type="hidden"> <input id="jsrid1" type="hidden"> <input
id="jsr" name="jsr" class="easyui-combotree"
data-options="url:'${pageContext.request.contextPath}/admin/userController/allTreeNode.do',parentField : 'pid',lines : true,panelHeight:'100px'"
style="width: 155px;" />
</td>
<th>入库日期</th>
<td colspan="3"><input id="rkstart1"
name="modifydatetimeStart"
onFocus="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" />至<input
id="rkend1" name="modifydatetimeEnd"
onFocus="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" />
</td>
</tr>
</table>
<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="toSub();">过滤条件</a> <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:true" onclick="cgtj_cleanFun();">清空条件</a>
</form>
</div>
<div data-options="region:'center',border:false" align="center">
<table style="width: 100%">
<caption style="background-color:#E0ECFF;height: 30px;" > </caption>
<tr>
<td><div id="chartdiv" align="center"><br/><br/><br/><br/><br/><br/><br/>无数据显示</div> </td>
</tr>
</table>
</div>
</div>
2、Controller
package com.xx.controller.drp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xx.pageModel.base.DataGrid;
import com.xx.pageModel.base.Json;
import com.xx.pageModel.drp.InStockDetailPage;
import com.xx.service.drp.InStockDetailServiceI;
@Controller
@RequestMapping("/admin/inStockDetailController")
public class InStockDetailController {
private InStockDetailServiceI inStockDetailService;
public InStockDetailServiceI getInStockDetailService() {
return inStockDetailService;
}
@Autowired
public void setInStockDetailService(InStockDetailServiceI inStockDetailService) {
this.inStockDetailService = inStockDetailService;
}
/**
* 采购统计部分
* */
@RequestMapping("/chart2tj")
@ResponseBody
public Json chart2Bdrs(InStockDetailPage isp,HttpServletRequest request) {
//格式化数字
Json j = new Json();
List<InStockDetailPage> list = inStockDetailService.combobox2tj(isp);
StringBuffer strXML= new StringBuffer();
if(isp.getTu()==1 ){
strXML.append("<graph caption='采购商品数量统计' xAxisName='商品名称' yAxisName='数量' decimalPrecision='2' formatNumberScale='0' showNames='1' numberSuffix='个' baseFontSize='12' bgcolor='EEEFFF' aboutMenuItemLabel='关于我们' aboutMenuItemLink='http://www.lwkjsoft.com/'>");
for(InStockDetailPage i : list){
strXML.append("<set name='"+i.getGname()+"' value='"+i.getTnum()+"' color='#"+forColor()+"' />");
}
}else if(isp.getTu()==2){
strXML.append("<graph caption='采购商品数量比例统计' xAxisName='商品名称' yAxisName='百分比' decimalPrecision='2' formatNumberScale='0' showNames='1' numberSuffix='%' baseFontSize='12' bgcolor='FFFEEE' aboutMenuItemLabel='关于我们' aboutMenuItemLink='http://www.lwkjsoft.com/'>");
int countA = 0;
for(InStockDetailPage i:list){
countA += i.getTnum();
}
if(countA>0){
DecimalFormat df = new DecimalFormat("0.0000");
for(InStockDetailPage i:list){
if(i.getTnum()>0){
strXML.append("<set name='"+i.getGname()+"' value='"+df.format(i.getTnum().doubleValue()/countA*100)+"' color='#"+forColor()+"' />");
}else{
strXML.append("<set name='"+i.getGname()+"' value='0' color='#"+forColor()+"' />");
}
}
}
}
strXML.append("</graph>");
try {
j.setSuccess(true);
j.setMsg(strXML.toString());
j.setObj(null);
} catch (Exception e) {
e.printStackTrace();
}
return j;
}
//生成6位码颜色值
private String forColor(){
String[] strs ={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
StringBuffer color = new StringBuffer();
for(int i=0;i<=5;i++){
int j = (int) (Math.random()*10000%16);
color.append(strs[j]);
}
return color.toString();
}
}
package com.xx.service.impl.drp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.xx.dao.base.BaseDaoI;
import com.xx.model.base.Goods;
import com.xx.model.drp.InStockDetail;
import com.xx.pageModel.base.DataGrid;
import com.xx.pageModel.drp.InStockDetailPage;
import com.xx.service.drp.InStockDetailServiceI;
@Service("inStockDetailService")
public class InStockDetailServiceImpl implements InStockDetailServiceI {
private BaseDaoI<InStockDetail> inStockDetailDao;
public BaseDaoI<InStockDetail> getInStockDetailDao() {
return inStockDetailDao;
}
@Autowired
public void setInStockDetailDao(BaseDaoI<InStockDetail> inStockDetailDao) {
this.inStockDetailDao = inStockDetailDao;
}
private BaseDaoI<Goods> goodsDao;
public BaseDaoI<Goods> getGoodsDao() {
return goodsDao;
}
@Autowired
public void setGoodsDao(BaseDaoI<Goods> goodsDao) {
this.goodsDao = goodsDao;
}
@Override
public List<InStockDetailPage> combobox2tj( InStockDetailPage isp) {
List<InStockDetailPage> nl = new ArrayList<InStockDetailPage>();
StringBuffer sql = new StringBuffer();
sql.append( "select d.incode from drp_instock d where d.status=1 ");
if(isp.getCkid()!=null&&!isp.getCkid().equals("")){
sql.append( " and d.rhck='"+isp.getCkid()+"' ");
}
if(isp.getJsr()!=null&&!isp.getJsr().equals("")){
sql.append(" and d.jsr='"+isp.getJsr()+"' ");
}
if(isp.getRkstart()!=null&&!isp.getRkstart().equals("")){
sql.append(" and d.indate >='"+isp.getRkstart()+"' ");
}
if(isp.getRkend()!=null&&!isp.getRkend().equals("")){
sql.append(" and d.indate <='"+isp.getRkend()+"' ");
}
if(isp.getGys()!=null&&!isp.getGys().equals("")){
sql.append( " and d.gys >='"+isp.getGys()+"' ");
}
String hql = "select t.gid gid,sum(t.num) tsum from drp_instockdetail t where 1=1 and t.inid in ("+sql.toString()+") group by t.gid";
JdbcTemplate jt = goodsDao.getJdbcTemplate();
List<Map<String, Object>> list=jt.queryForList(hql, new Object[]{});
for (Map<String,Object> m :list) {
InStockDetailPage isdp = new InStockDetailPage();
isdp.setGid(m.get("gid").toString());
int num = 0;
try {
num =Integer.parseInt(m.get("tsum").toString());
} catch (Exception e) {
num = 0;
}
isdp.setGname(goodsDao.get(Goods.class, isdp.getGid()).getName());
isdp.setDid(UUID.randomUUID().toString());
isdp.setTnum(num);
nl.add(isdp);
}
return nl;
}
}
4、BaseDaoImpl
package com.xx.dao.impl.base;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.xx.dao.base.BaseDaoI;
import com.xx.util.base.BeanUtil;
@Repository("baseDao")
public class BaseDaoImpl<T> implements BaseDaoI<T> {
private JdbcTemplate jdbcTemplate;
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
private Session getCurrentSession() {
return this.sessionFactory.getCurrentSession();
}
@Override
public Serializable save(T o) {
return this.getCurrentSession().save(o);
}
@SuppressWarnings("unchecked")
@Override
public T get(Class<T> c, Serializable id) {
return (T) this.getCurrentSession().get(c, id);
}
@SuppressWarnings("unchecked")
@Override
public T get(String hql) {
Query q = this.getCurrentSession().createQuery(hql);
List<T> l = q.list();
if (l != null && l.size() > 0) {
return l.get(0);
}
return null;
}
@SuppressWarnings("unchecked")
@Override
public T get(String hql, Map<String, Object> params) {
Query q = this.getCurrentSession().createQuery(hql);
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
q.setParameter(key, params.get(key));
}
}
List<T> l = q.list();
if (l != null && l.size() > 0) {
return l.get(0);
}
return null;
}
@Override
public void delete(T o) {
this.getCurrentSession().delete(o);
}
@Override
public void update(T o) {
this.getCurrentSession().update(o);
}
@Override
public void saveOrUpdate(T o) {
this.getCurrentSession().saveOrUpdate(o);
}
@SuppressWarnings("unchecked")
@Override
public List<T> find(String hql) {
Query q = this.getCurrentSession().createQuery(hql);
return q.list();
}
@SuppressWarnings("unchecked")
@Override
public List<T> find(String hql, Map<String, Object> params) {
Query q = this.getCurrentSession().createQuery(hql);
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
q.setParameter(key, params.get(key));
}
}
return q.list();
}
@SuppressWarnings("unchecked")
@Override
public List<T> find(String hql, List<Object> param) {
Query q = this.getCurrentSession().createQuery(hql);
if (param != null && param.size() > 0) {
for (int i = 0; i < param.size(); i++) {
q.setParameter(i, param.get(i));
}
}
return q.list();
}
@SuppressWarnings("unchecked")
@Override
public List<T> find(String hql, List<Object> param,int page, int rows) {
Query q = this.getCurrentSession().createQuery(hql);
if (param != null && param.size() > 0) {
for (int i = 0; i < param.size(); i++) {
q.setParameter(i, param.get(i));
}
}
return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
}
@SuppressWarnings("unchecked")
@Override
public List<T> find(String hql, Map<String, Object> params, int page,
int rows) {
Query q = this.getCurrentSession().createQuery(hql);
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
q.setParameter(key, params.get(key));
}
}
return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
}
@SuppressWarnings("unchecked")
@Override
public List<T> find(String hql, int page, int rows) {
Query q = this.getCurrentSession().createQuery(hql);
return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
}
@Override
public Long count(String hql) {
Query q = this.getCurrentSession().createQuery(hql);
return (Long) q.uniqueResult();
}
@Override
public Long count(String hql, Map<String, Object> params) {
Query q = this.getCurrentSession().createQuery(hql);
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
q.setParameter(key, params.get(key));
}
}
return (Long) q.uniqueResult();
}
@Override
public Long count(String hql, List<Object> param) {
Query q = this.getCurrentSession().createQuery(hql);
if (param != null && param.size() > 0) {
for (int i = 0; i < param.size(); i++) {
q.setParameter(i, param.get(i));
}
}
return (Long) q.uniqueResult();
}
@Override
public int executeHql(String hql) {
Query q = this.getCurrentSession().createQuery(hql);
return q.executeUpdate();
}
@Override
public int executeHql(String hql, Map<String, Object> params) {
Query q = this.getCurrentSession().createQuery(hql);
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
q.setParameter(key, params.get(key));
}
}
return q.executeUpdate();
}
// execute sql
@Override
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
@Autowired
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/**
* 使用指定的检索标准检索数据并分页返回数据
*/
@Override
public List<Map<String, Object>> findForJdbc(String sql, int page, int rows) {
int beginNum = (page - 1) * rows;
sql = "select * from ( " + sql + ") sel_tab00 limit " + beginNum + ","
+ rows;
return this.jdbcTemplate.queryForList(sql);
}
/**
* 使用指定的检索标准检索数据并分页返回数据
*
* @throws IllegalAccessException
* @throws InstantiationException
*/
@Override
public List<T> findObjForJdbc(String sql, int page, int rows, Class<T> clazz) {
List<T> rsList = new ArrayList<T>();
int beginNum = (page - 1) * rows;
sql = "select * from ( " + sql + ") sel_tab00 limit " + beginNum + ","
+ rows;
List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql);
T po = null;
for (Map<String, Object> m : mapList) {
try {
po = clazz.newInstance();
BeanUtil.copyMap2Bean_Nobig(po, m);
rsList.add(po);
} catch (Exception e) {
e.printStackTrace();
}
}
return rsList;
}
/**
* 使用指定的检索标准检索数据并分页返回数据-采用预处理方式
*
* @param criteria
* @param firstResult
* @param maxResults
* @return
* @throws DataAccessException
*/
@Override
public List<Map<String, Object>> findForJdbcParam(String sql, int page,
int rows, Object... objs) {
int beginNum = (page - 1) * rows;
sql = "select * from ( " + sql + ") sel_tab00 limit " + beginNum + ","
+ rows;
return this.getJdbcTemplate().queryForList(sql, objs);
}
/**
* 使用指定的检索标准检索数据并分页返回数据For JDBC
*/
@Override
public Long getCountForJdbc(String sql) {
return this.getJdbcTemplate().queryForLong(sql);
}
/**
* 使用指定的检索标准检索数据并分页返回数据For JDBC-采用预处理方式
*
*/
@Override
public Long getCountForJdbcParam(String sql, Object[] objs) {
return this.getJdbcTemplate().queryForLong(sql, objs);
}
@Override
public List<Map<String, Object>> findForJdbc(String sql, Object... objs) {
return this.jdbcTemplate.queryForList(sql, objs);
}
@Override
public Integer executeSql(String sql, List<Object> param) {
return this.jdbcTemplate.update(sql, param);
}
@Override
public Integer executeSql(String sql, Object... param) {
return this.jdbcTemplate.update(sql, param);
}
@Override
public Integer countByJdbc(String sql, Object... param) {
return this.jdbcTemplate.queryForInt(sql, param);
}
@Override
public Map<String, Object> findOneForJdbc(String sql, Object... objs) {
try {
return this.jdbcTemplate.queryForMap(sql, objs);
} catch (EmptyResultDataAccessException e) {
return null;
}
}
@Override
public Connection getJdbcTemplateConnection(){
// this.sessionFactory.getCurrentSession().beginTransaction();
try {
return this.jdbcTemplate.getDataSource().getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}