package com.sw.mes.sap.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.hibernate.query.internal.NativeQueryImpl;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sw.mes.core.utils.DateUtil;
import com.sw.mes.sap.dto.statistics.StatisticsDto;
import com.sw.mes.sap.dto.statistics.StatisticsLjDto;
import com.sw.mes.utils.StringUtils;


@Repository
public class ReportSapGoodsmvtDaoImpl {
	
	@Autowired
    private EntityManager entityManager;

	public List<StatisticsDto> statisticYbcshdb(StatisticsDto dto) {
		
		if(dto==null || StringUtils.isNotBlank(dto.getProcName())) {
			return new ArrayList();
		}
		
		String sql = "select sum(menge) totalmenge, sum(if(`status` = TRUE,`menge`,0)) yfmenge , sum(if((`status` = FALSE),`menge`,0)) AS `wfmenge`,umlgo,umcha,matnr from report_sap_goodsmvt";
		
		String where = " where 1=1";
		
		if(StringUtils.isNotBlank(dto.getProcName())) {
			where = where + " and proc_name='"+dto.getProcName()+"'";
		}
		
		if(StringUtils.isNotBlank(dto.getUmcha())) {
			where = where + " and umcha like '%"+dto.getUmcha()+"%'";
		}
		
		if(StringUtils.isNotBlank(dto.getMatnr())) {
			where = where + " and matnr like '%"+dto.getMatnr()+"%'";
		}
		
		if(StringUtils.isNotBlank(dto.getUmlgo())) {
			where = where + " and umlgo like '%"+dto.getUmlgo()+"%'";
		}
		
		String group = " GROUP BY `status`,umlgo,umcha,matnr";
		
		StringBuffer bf = new StringBuffer(sql);
		
		bf.append(where).append(group);
		Query query = entityManager.createNativeQuery(bf.toString());
		query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.aliasToBean(StatisticsDto.class));
		List list = query.getResultList();
		return list;
	}
	
	
	
	public List<StatisticsLjDto> statisticLj(StatisticsLjDto dto) {
		if(dto.getStartDate()==null) {
			return new ArrayList();
		}
		
		String startDate = DateUtil.formatDate(dto.getStartDate(), DateUtil.longFormat);
		String endDate = DateUtil.formatDate(dto.getEndDate(), DateUtil.longFormat);
		
		String sql = "select `t3`.`tid` AS `tid`,`t3`.`did` AS `did`,`t3`.`raw_type` AS `raw_type`,`t3`.`sap_no` AS `sap_no`,`t3`.`equipment_code` AS `equipment_code`,`t3`.`lot_no` AS `lot_no`,`t3`.`again_num` AS `again_num`,`t3`.`material_code` AS `material_code`,`t3`.`batch_no` AS `batch_no`,`t3`.`weight` AS `weight`,`t3`.`fill_time` AS `fill_time`,`t3`.`confirm_time` AS `confirm_time` from (select `t1`.`tid` AS `tid`,concat('0-',`t1`.`did`) AS `did`,'初始装料' AS `raw_type`,`t1`.`sap_no` AS `sap_no`,`t1`.`equipment_code` AS `equipment_code`,`t1`.`lot_no` AS `lot_no`,`t1`.`again_num` AS `again_num`,`t1`.`material_code` AS `material_code`,`t1`.`batch_no` AS `batch_no`,`t1`.`weight` AS `weight`,`t1`.`fill_time` AS `fill_time`,`t1`.`confirm_time` AS `confirm_time` from (select `lt`.`id` AS `tid`,`fd`.`id` AS `did`,`lt`.`sap_no` AS `sap_no`,`lt`.`equipment_code` AS `equipment_code`,`lt`.`lot_no` AS `lot_no`,0 AS `again_num`,`fd`.`material_code` AS `material_code`,`fd`.`batch_no` AS `batch_no`,`fd`.`weight` AS `weight`,`fd`.`install_time` AS `fill_time`,`lt`.`confirm_time` AS `confirm_time` from (`jinko_mes`.`production_blast_furnace_detail` `fd` join `jinko_mes`.`production_loading_task` `lt` on((`fd`.`blast_furnace_id` = `lt`.`id`))) where ((`lt`.`raw_status` = 4) and (`lt`.`confirm` = 1) and (`lt`.`confirm_time` between '"+startDate+"' and '"+endDate+"'))) `t1` union all select `t2`.`tid` AS `tid`,concat('1-',`t2`.`did`) AS `did`,'复投灌料' AS `raw_type`,`t2`.`sap_no` AS `sap_no`,`t2`.`equipment_code` AS `equipment_code`,`t2`.`lot_no` AS `lot_no`,`t2`.`again_num` AS `again_num`,`t2`.`material_code` AS `material_code`,`t2`.`batch_no` AS `batch_no`,`t2`.`weight` AS `weight`,`t2`.`fill_time` AS `fill_time`,`t2`.`confirm_time` AS `confirm_time` from (select `atd`.`id` AS `tid`,`fd`.`id` AS `did`,`atd`.`sap_no` AS `sap_no`,`atd`.`equipment_code` AS `equipment_code`,`atd`.`lot_no` AS `lot_no`,`atd`.`again_num` AS `again_num`,`fd`.`material_code` AS `material_code`,`fd`.`batch_no` AS `batch_no`,`fd`.`weight` AS `weight`,`ifb`.`start_time` AS `fill_time`,`ifb`.`confirm_time` AS `confirm_time` from ((`jinko_mes`.`production_input_furnace_detail` `fd` join `jinko_mes`.`production_input_furnace_burden` `ifb` on((`fd`.`input_furnace_id` = `ifb`.`id`))) join `jinko_mes`.`production_again_task_detail` `atd` on((`fd`.`again_task_detail_id` = `atd`.`id`))) where ((`atd`.`raw_status` = 4) and (`ifb`.`confirm` = 1) and (`ifb`.`confirm_time` between '"+startDate+"' and '"+endDate+"'))) `t2`) `t3` order by `t3`.`confirm_time`,`t3`.`equipment_code`,`t3`.`material_code`,`t3`.`weight`";
		
		StringBuffer bf = new StringBuffer(sql);
		
		Query query = entityManager.createNativeQuery(bf.toString());
		query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.aliasToBean(StatisticsLjDto.class));
		List list = query.getResultList();
		return list;
	}
	
	
	
	/**
	 * select `b`.`aufnr` AS `aufnr`,`b`.`matnr` AS `matnr`,`b`.`charg` AS `charg`,`b`.`sapMenge` AS `sapMenge`,`c`.`sap_no` AS `sap_no`,`c`.`material_code` AS `material_code`,`c`.`batch_no` AS `batch_no`,`c`.`totalWeight` AS `totalWeight` from (((select `jinko_mes`.`report_sap_goodsmvt`.`aufnr` AS `aufnr`,`jinko_mes`.`report_sap_goodsmvt`.`matnr` AS `matnr`,`jinko_mes`.`report_sap_goodsmvt`.`charg` AS `charg`,sum(`jinko_mes`.`report_sap_goodsmvt`.`menge`) AS `sapMenge` from `jinko_mes`.`report_sap_goodsmvt` where ((`jinko_mes`.`report_sap_goodsmvt`.`budat` between ?1 and ?2) and (`jinko_mes`.`report_sap_goodsmvt`.`proc_name` = '拉晶')) group by `jinko_mes`.`report_sap_goodsmvt`.`aufnr`,`jinko_mes`.`report_sap_goodsmvt`.`matnr`,`jinko_mes`.`report_sap_goodsmvt`.`charg`)) `b` left join (select `a`.`sap_no` AS `sap_no`,`a`.`material_code` AS `material_code`,`a`.`batch_no` AS `batch_no`,sum(`a`.`weight`) AS `totalWeight` from (select `t3`.`tid` AS `tid`,`t3`.`did` AS `did`,`t3`.`raw_type` AS `raw_type`,`t3`.`sap_no` AS `sap_no`,`t3`.`equipment_code` AS `equipment_code`,`t3`.`lot_no` AS `lot_no`,`t3`.`again_num` AS `again_num`,`t3`.`material_code` AS `material_code`,`t3`.`batch_no` AS `batch_no`,`t3`.`weight` AS `weight`,`t3`.`fill_time` AS `fill_time`,`t3`.`confirm_time` AS `confirm_time` from (select `t1`.`tid` AS `tid`,concat('0-',`t1`.`did`) AS `did`,'初始装料' AS `raw_type`,`t1`.`sap_no` AS `sap_no`,`t1`.`equipment_code` AS `equipment_code`,`t1`.`lot_no` AS `lot_no`,`t1`.`again_num` AS `again_num`,`t1`.`material_code` AS `material_code`,`t1`.`batch_no` AS `batch_no`,`t1`.`weight` AS `weight`,`t1`.`fill_time` AS `fill_time`,`t1`.`confirm_time` AS `confirm_time` from (select `lt`.`id` AS `tid`,`fd`.`id` AS `did`,`lt`.`sap_no` AS `sap_no`,`lt`.`equipment_code` AS `equipment_code`,`lt`.`lot_no` AS `lot_no`,0 AS `again_num`,`fd`.`material_code` AS `material_code`,`fd`.`batch_no` AS `batch_no`,`fd`.`weight` AS `weight`,`fd`.`install_time` AS `fill_time`,`lt`.`confirm_time` AS `confirm_time` from (`jinko_mes`.`production_blast_furnace_detail` `fd` join `jinko_mes`.`production_loading_task` `lt` on((`fd`.`blast_furnace_id` = `lt`.`id`))) where ((`lt`.`raw_status` = 4) and (`lt`.`confirm` = 1) and (`lt`.`confirm_time` between ?1 and ?2))) `t1` union all select `t2`.`tid` AS `tid`,concat('1-',`t2`.`did`) AS `did`,'复投灌料' AS `raw_type`,`t2`.`sap_no` AS `sap_no`,`t2`.`equipment_code` AS `equipment_code`,`t2`.`lot_no` AS `lot_no`,`t2`.`again_num` AS `again_num`,`t2`.`material_code` AS `material_code`,`t2`.`batch_no` AS `batch_no`,`t2`.`weight` AS `weight`,`t2`.`fill_time` AS `fill_time`,`t2`.`confirm_time` AS `confirm_time` from (select `atd`.`id` AS `tid`,`fd`.`id` AS `did`,`atd`.`sap_no` AS `sap_no`,`atd`.`equipment_code` AS `equipment_code`,`atd`.`lot_no` AS `lot_no`,`atd`.`again_num` AS `again_num`,`fd`.`material_code` AS `material_code`,`fd`.`batch_no` AS `batch_no`,`fd`.`weight` AS `weight`,`ifb`.`start_time` AS `fill_time`,`ifb`.`confirm_time` AS `confirm_time` from ((`jinko_mes`.`production_input_furnace_detail` `fd` join `jinko_mes`.`production_input_furnace_burden` `ifb` on((`fd`.`input_furnace_id` = `ifb`.`id`))) join `jinko_mes`.`production_again_task_detail` `atd` on((`fd`.`again_task_detail_id` = `atd`.`id`))) where ((`atd`.`raw_status` = 4) and (`ifb`.`confirm` = 1) and (`ifb`.`confirm_time` between ?1 and ?2))) `t2`) `t3` order by `t3`.`confirm_time`,`t3`.`equipment_code`,`t3`.`material_code`,`t3`.`weight`) `a` group by `a`.`sap_no`,`a`.`material_code`,`a`.`batch_no`) `c` on(((`b`.`aufnr` = `c`.`sap_no`) and (`b`.`matnr` = `c`.`material_code`) and (`b`.`charg` = `c`.`batch_no`))))
	 */
	public List<Map<Object, Object>> djshStatistics(Date startDate,Date endDate, String aufnr, String matnr, String charg){
		
		String sql = "select `b`.`aufnr` AS `aufnr`,`b`.`matnr` AS `matnr`,`b`.`charg` AS `charg`,`b`.`sapMenge` AS `sapMenge`,`b`.`ysb` AS `ysb`,`b`.`wsb` AS `wsb`,`c`.`sap_no` AS `sap_no`,`c`.`material_code` AS `material_code`,`c`.`batch_no` AS `batch_no`,`c`.`totalWeight` AS `totalWeight` from (((select `jinko_mes`.`report_sap_goodsmvt`.`aufnr` AS `aufnr`,`jinko_mes`.`report_sap_goodsmvt`.`matnr` AS `matnr`,`jinko_mes`.`report_sap_goodsmvt`.`charg` AS `charg`,sum(`jinko_mes`.`report_sap_goodsmvt`.`menge`) AS `sapMenge`,sum(if((`jinko_mes`.`report_sap_goodsmvt`.`status` = 1),`jinko_mes`.`report_sap_goodsmvt`.`menge`,0)) AS `ysb`,sum(if((`jinko_mes`.`report_sap_goodsmvt`.`status` = 0),`jinko_mes`.`report_sap_goodsmvt`.`menge`,0)) AS `wsb` from `jinko_mes`.`report_sap_goodsmvt`  where ((`jinko_mes`.`report_sap_goodsmvt`.`budat` between :startDate and :endDate) and (`jinko_mes`.`report_sap_goodsmvt`.`proc_name` = '拉晶')) group by `jinko_mes`.`report_sap_goodsmvt`.`aufnr`,`jinko_mes`.`report_sap_goodsmvt`.`matnr`,`jinko_mes`.`report_sap_goodsmvt`.`charg`)) `b` left join (select `a`.`sap_no` AS `sap_no`,`a`.`material_code` AS `material_code`,`a`.`batch_no` AS `batch_no`,sum(`a`.`weight`) AS `totalWeight` from (select `t3`.`tid` AS `tid`,`t3`.`did` AS `did`,`t3`.`raw_type` AS `raw_type`,`t3`.`sap_no` AS `sap_no`,`t3`.`equipment_code` AS `equipment_code`,`t3`.`lot_no` AS `lot_no`,`t3`.`again_num` AS `again_num`,`t3`.`material_code` AS `material_code`,`t3`.`batch_no` AS `batch_no`,`t3`.`weight` AS `weight`,`t3`.`fill_time` AS `fill_time`,`t3`.`confirm_time` AS `confirm_time` from (select `t1`.`tid` AS `tid`,concat('0-',`t1`.`did`) AS `did`,'初始装料' AS `raw_type`,`t1`.`sap_no` AS `sap_no`,`t1`.`equipment_code` AS `equipment_code`,`t1`.`lot_no` AS `lot_no`,`t1`.`again_num` AS `again_num`,`t1`.`material_code` AS `material_code`,`t1`.`batch_no` AS `batch_no`,`t1`.`weight` AS `weight`,`t1`.`fill_time` AS `fill_time`,`t1`.`confirm_time` AS `confirm_time` from (select `lt`.`id` AS `tid`,`fd`.`id` AS `did`,`lt`.`sap_no` AS `sap_no`,`lt`.`equipment_code` AS `equipment_code`,`lt`.`lot_no` AS `lot_no`,0 AS `again_num`,`fd`.`material_code` AS `material_code`,`fd`.`batch_no` AS `batch_no`,`fd`.`weight` AS `weight`,`fd`.`install_time` AS `fill_time`,`lt`.`confirm_time` AS `confirm_time` from (`jinko_mes`.`production_blast_furnace_detail` `fd` join `jinko_mes`.`production_loading_task` `lt` on((`fd`.`blast_furnace_id` = `lt`.`id`))) where ((`lt`.`raw_status` = 4) and (`lt`.`confirm` = 1) and (`lt`.`confirm_time` between :startDate and :endDate))) `t1` union all select `t2`.`tid` AS `tid`,concat('1-',`t2`.`did`) AS `did`,'复投灌料' AS `raw_type`,`t2`.`sap_no` AS `sap_no`,`t2`.`equipment_code` AS `equipment_code`,`t2`.`lot_no` AS `lot_no`,`t2`.`again_num` AS `again_num`,`t2`.`material_code` AS `material_code`,`t2`.`batch_no` AS `batch_no`,`t2`.`weight` AS `weight`,`t2`.`fill_time` AS `fill_time`,`t2`.`confirm_time` AS `confirm_time` from (select `atd`.`id` AS `tid`,`fd`.`id` AS `did`,`atd`.`sap_no` AS `sap_no`,`atd`.`equipment_code` AS `equipment_code`,`atd`.`lot_no` AS `lot_no`,`atd`.`again_num` AS `again_num`,`fd`.`material_code` AS `material_code`,`fd`.`batch_no` AS `batch_no`,`fd`.`weight` AS `weight`,`ifb`.`start_time` AS `fill_time`,`ifb`.`confirm_time` AS `confirm_time` from ((`jinko_mes`.`production_input_furnace_detail` `fd` join `jinko_mes`.`production_input_furnace_burden` `ifb` on((`fd`.`input_furnace_id` = `ifb`.`id`))) join `jinko_mes`.`production_again_task_detail` `atd` on((`fd`.`again_task_detail_id` = `atd`.`id`))) where ((`atd`.`raw_status` = 4) and (`ifb`.`confirm` = 1) and (`ifb`.`confirm_time` between :startDate and :endDate))) `t2`) `t3` order by `t3`.`confirm_time`,`t3`.`equipment_code`,`t3`.`material_code`,`t3`.`weight`) `a` group by `a`.`sap_no`,`a`.`material_code`,`a`.`batch_no`) `c` on(((`b`.`aufnr` = `c`.`sap_no`) and (`b`.`matnr` = `c`.`material_code`) and (`b`.`charg` = `c`.`batch_no`))))";
		
		StringBuffer bf = new StringBuffer(sql);
		
		StringBuffer whereString = new StringBuffer(" where 1=1 ");
		
		if(StringUtils.isNotBlank(aufnr)) {
			whereString.append(" and b.aufnr like '%"+aufnr+"%'");
		}
		
		if(StringUtils.isNotBlank(matnr)) {
			whereString.append(" and b.matnr like '%"+matnr+"%'");
		}
		
		if(StringUtils.isNotBlank(charg)) {
			whereString.append(" and b.charg like '%"+charg+"%'");
		}
		
		bf.append(whereString);
		
		Query query = entityManager.createNativeQuery(bf.toString());
		query.setParameter("startDate", startDate);
		query.setParameter("endDate", endDate);
		
		query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		//query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.aliasToBean(Map.class));
		List list = query.getResultList();
		return list;
	}
	
	
	
	
	/**
	 * select `b`.`aufnr` AS `aufnr`,`b`.`matnr` AS `matnr`,`b`.`charg` AS `charg`,`b`.`sapMenge` AS `sapMenge`,`c`.`sap_no` AS `sap_no`,`c`.`material_code` AS `material_code`,`c`.`batch_no` AS `batch_no`,`c`.`totalWeight` AS `totalWeight` from (((select `jinko_mes`.`report_sap_goodsmvt`.`aufnr` AS `aufnr`,`jinko_mes`.`report_sap_goodsmvt`.`matnr` AS `matnr`,`jinko_mes`.`report_sap_goodsmvt`.`charg` AS `charg`,sum(`jinko_mes`.`report_sap_goodsmvt`.`menge`) AS `sapMenge` from `jinko_mes`.`report_sap_goodsmvt` where ((`jinko_mes`.`report_sap_goodsmvt`.`budat` between ?1 and ?2) and (`jinko_mes`.`report_sap_goodsmvt`.`proc_name` = '拉晶')) group by `jinko_mes`.`report_sap_goodsmvt`.`aufnr`,`jinko_mes`.`report_sap_goodsmvt`.`matnr`,`jinko_mes`.`report_sap_goodsmvt`.`charg`)) `b` left join (select `a`.`sap_no` AS `sap_no`,`a`.`material_code` AS `material_code`,`a`.`batch_no` AS `batch_no`,sum(`a`.`weight`) AS `totalWeight` from (select `t3`.`tid` AS `tid`,`t3`.`did` AS `did`,`t3`.`raw_type` AS `raw_type`,`t3`.`sap_no` AS `sap_no`,`t3`.`equipment_code` AS `equipment_code`,`t3`.`lot_no` AS `lot_no`,`t3`.`again_num` AS `again_num`,`t3`.`material_code` AS `material_code`,`t3`.`batch_no` AS `batch_no`,`t3`.`weight` AS `weight`,`t3`.`fill_time` AS `fill_time`,`t3`.`confirm_time` AS `confirm_time` from (select `t1`.`tid` AS `tid`,concat('0-',`t1`.`did`) AS `did`,'初始装料' AS `raw_type`,`t1`.`sap_no` AS `sap_no`,`t1`.`equipment_code` AS `equipment_code`,`t1`.`lot_no` AS `lot_no`,`t1`.`again_num` AS `again_num`,`t1`.`material_code` AS `material_code`,`t1`.`batch_no` AS `batch_no`,`t1`.`weight` AS `weight`,`t1`.`fill_time` AS `fill_time`,`t1`.`confirm_time` AS `confirm_time` from (select `lt`.`id` AS `tid`,`fd`.`id` AS `did`,`lt`.`sap_no` AS `sap_no`,`lt`.`equipment_code` AS `equipment_code`,`lt`.`lot_no` AS `lot_no`,0 AS `again_num`,`fd`.`material_code` AS `material_code`,`fd`.`batch_no` AS `batch_no`,`fd`.`weight` AS `weight`,`fd`.`install_time` AS `fill_time`,`lt`.`confirm_time` AS `confirm_time` from (`jinko_mes`.`production_blast_furnace_detail` `fd` join `jinko_mes`.`production_loading_task` `lt` on((`fd`.`blast_furnace_id` = `lt`.`id`))) where ((`lt`.`raw_status` = 4) and (`lt`.`confirm` = 1) and (`lt`.`confirm_time` between ?1 and ?2))) `t1` union all select `t2`.`tid` AS `tid`,concat('1-',`t2`.`did`) AS `did`,'复投灌料' AS `raw_type`,`t2`.`sap_no` AS `sap_no`,`t2`.`equipment_code` AS `equipment_code`,`t2`.`lot_no` AS `lot_no`,`t2`.`again_num` AS `again_num`,`t2`.`material_code` AS `material_code`,`t2`.`batch_no` AS `batch_no`,`t2`.`weight` AS `weight`,`t2`.`fill_time` AS `fill_time`,`t2`.`confirm_time` AS `confirm_time` from (select `atd`.`id` AS `tid`,`fd`.`id` AS `did`,`atd`.`sap_no` AS `sap_no`,`atd`.`equipment_code` AS `equipment_code`,`atd`.`lot_no` AS `lot_no`,`atd`.`again_num` AS `again_num`,`fd`.`material_code` AS `material_code`,`fd`.`batch_no` AS `batch_no`,`fd`.`weight` AS `weight`,`ifb`.`start_time` AS `fill_time`,`ifb`.`confirm_time` AS `confirm_time` from ((`jinko_mes`.`production_input_furnace_detail` `fd` join `jinko_mes`.`production_input_furnace_burden` `ifb` on((`fd`.`input_furnace_id` = `ifb`.`id`))) join `jinko_mes`.`production_again_task_detail` `atd` on((`fd`.`again_task_detail_id` = `atd`.`id`))) where ((`atd`.`raw_status` = 4) and (`ifb`.`confirm` = 1) and (`ifb`.`confirm_time` between ?1 and ?2))) `t2`) `t3` order by `t3`.`confirm_time`,`t3`.`equipment_code`,`t3`.`material_code`,`t3`.`weight`) `a` group by `a`.`sap_no`,`a`.`material_code`,`a`.`batch_no`) `c` on(((`b`.`aufnr` = `c`.`sap_no`) and (`b`.`matnr` = `c`.`material_code`) and (`b`.`charg` = `c`.`batch_no`))))
	 */
	public List<Map<Object, Object>> jdxhStatistics(Date startDate,Date endDate, String aufnr, String matnr, String charg){
		
		String sql = "select `a`.`aufnr` AS `aufnr`,`a`.`matnr` AS `matnr`,`a`.`charg` AS `charg`,`a`.`sapMenge` AS `sapMenge`,`a`.`ysb` AS `ysb`,`a`.`wsb` AS `wsb`,`b`.`订单号` AS `sap_no`,`b`.`物料号` AS `material_code`,`b`.`批次号` AS `batch_no`,`b`.`totalWeight` AS `totalWeight` from (((select `cmr`.`gmt_create` AS `上报时间`,`cmr`.`section_no` AS `单晶编号`,`cmmr`.`order_no` AS `订单号`,`cmmr`.`material_code` AS `物料号`,`plc`.`batch_no` AS `批次号`,sum(`yid`.`theory_weight`) AS `totalWeight` from (((`jinko_mes`.`crystal_material_relation` `cmr` left join `jinko_mes`.`yb_inspect_data` `yid` on((`yid`.`section_no` = `cmr`.`section_no`))) left join `jinko_mes`.`production_long_crystal` `plc` on((`plc`.`crystal_no` = `yid`.`crystal_no`))) left join `jinko_mes`.`crystal_material_relation` `cmmr` on(((`yid`.`crystal_no` = `cmmr`.`section_no`) and (`cmmr`.`device_type` = 4)))) where ((`cmr`.`gmt_create` between :startDate and :endDate) and (`cmr`.`device_type` = 3)) group by `cmmr`.`material_code`,`plc`.`batch_no`,`cmmr`.`order_no`)) `b` left join (select `jinko_mes`.`report_sap_goodsmvt`.`aufnr` AS `aufnr`,`jinko_mes`.`report_sap_goodsmvt`.`matnr` AS `matnr`,`jinko_mes`.`report_sap_goodsmvt`.`charg` AS `charg`,sum(`jinko_mes`.`report_sap_goodsmvt`.`menge`) AS `sapMenge`,sum(if((`jinko_mes`.`report_sap_goodsmvt`.`status` = 1),`jinko_mes`.`report_sap_goodsmvt`.`menge`,0)) AS `ysb`,sum(if((`jinko_mes`.`report_sap_goodsmvt`.`status` = 0),`jinko_mes`.`report_sap_goodsmvt`.`menge`,0)) AS `wsb` from `jinko_mes`.`report_sap_goodsmvt` where ((`jinko_mes`.`report_sap_goodsmvt`.`budat` between :startDate and :endDate) and (`jinko_mes`.`report_sap_goodsmvt`.`proc_name` = '截断收货')) group by `jinko_mes`.`report_sap_goodsmvt`.`aufnr`,`jinko_mes`.`report_sap_goodsmvt`.`matnr`,`jinko_mes`.`report_sap_goodsmvt`.`charg`) `a` on(((`a`.`aufnr` = `b`.`订单号`) and (`a`.`matnr` = `b`.`物料号`) and (`a`.`charg` = `b`.`批次号`))))";
		
		StringBuffer bf = new StringBuffer(sql);
		
		StringBuffer whereString = new StringBuffer(" where 1=1 ");
		
		if(StringUtils.isNotBlank(aufnr)) {
			whereString.append(" and a.aufnr like '%"+aufnr+"%'");
		}
		
		if(StringUtils.isNotBlank(matnr)) {
			whereString.append(" and a.matnr like '%"+matnr+"%'");
		}
		
		if(StringUtils.isNotBlank(charg)) {
			whereString.append(" and a.charg like '%"+charg+"%'");
		}
		
		bf.append(whereString);
		
		Query query = entityManager.createNativeQuery(bf.toString());
		query.setParameter("startDate", startDate);
		query.setParameter("endDate", endDate);
		
		query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		//query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.aliasToBean(Map.class));
		List list = query.getResultList();
		return list;
	}
	
	public List<Map<Object, Object>> jdshStatistics(Date startDate, Date endDate, String aufnr, String matnr, String charg){
		
		String sql = "select `a`.`aufnr` AS `aufnr`,`a`.`matnr` AS `matnr`,`a`.`charg` AS `charg`,`a`.`sapMenge` AS `sapMenge`,`a`.`ysb` AS `ysb`,`a`.`wsb` AS `wsb`,`b`.`订单号` AS `sap_no`,`b`.`物料号` AS `material_code`,`b`.`批次号` AS `batch_no`,`b`.`totalWeight` AS `totalWeight` from (((select `cmr`.`gmt_create` AS `上报时间`,`cmr`.`section_no` AS `单晶编号`,`cmr`.`order_no` AS `订单号`,`plc`.`product_code` AS `物料号`,`plc`.`batch_no` AS `批次号`,sum(`plc`.`weight`) AS `totalWeight` from (`jinko_mes`.`crystal_material_relation` `cmr` left join `jinko_mes`.`production_long_crystal` `plc` on((`plc`.`crystal_no` = `cmr`.`section_no`))) where ((`cmr`.`gmt_create` between :startDate and :endDate) and (`cmr`.`device_type` = 4)) group by `plc`.`product_code`,`plc`.`batch_no`,`cmr`.`order_no`)) `b` left join (select `jinko_mes`.`report_sap_goodsmvt`.`aufnr` AS `aufnr`,`jinko_mes`.`report_sap_goodsmvt`.`matnr` AS `matnr`,`jinko_mes`.`report_sap_goodsmvt`.`charg` AS `charg`,sum(`jinko_mes`.`report_sap_goodsmvt`.`menge`) AS `sapMenge`,sum(if((`jinko_mes`.`report_sap_goodsmvt`.`status` = 1),`jinko_mes`.`report_sap_goodsmvt`.`menge`,0)) AS `ysb`,sum(if((`jinko_mes`.`report_sap_goodsmvt`.`status` = 0),`jinko_mes`.`report_sap_goodsmvt`.`menge`,0)) AS `wsb` from `jinko_mes`.`report_sap_goodsmvt` where ((`jinko_mes`.`report_sap_goodsmvt`.`budat` between :startDate and :endDate) and (`jinko_mes`.`report_sap_goodsmvt`.`proc_name` = '截断消耗')) group by `jinko_mes`.`report_sap_goodsmvt`.`aufnr`,`jinko_mes`.`report_sap_goodsmvt`.`matnr`,`jinko_mes`.`report_sap_goodsmvt`.`charg`) `a` on(((`a`.`aufnr` = `b`.`订单号`) and (`a`.`matnr` = `b`.`物料号`) and (`a`.`charg` = `b`.`批次号`))))";
		
		StringBuffer bf = new StringBuffer(sql);
		
		StringBuffer whereString = new StringBuffer(" where 1=1 ");
		
		if(StringUtils.isNotBlank(aufnr)) {
			whereString.append(" and a.aufnr like '%"+aufnr+"%'");
		}
		
		if(StringUtils.isNotBlank(matnr)) {
			whereString.append(" and a.matnr like '%"+matnr+"%'");
		}
		
		if(StringUtils.isNotBlank(charg)) {
			whereString.append(" and a.charg like '%"+charg+"%'");
		}
		
		bf.append(whereString);
		
		Query query = entityManager.createNativeQuery(bf.toString());
		query.setParameter("startDate", startDate);
		query.setParameter("endDate", endDate);
		
		query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		//query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.aliasToBean(Map.class));
		List list = query.getResultList();
		return list;
	}
	
	
	public List<Map<Object, Object>> dpxhStatistics(Date startDate,Date endDate, String aufnr, String matnr, String charg){
		
		String sql = "select `a`.`aufnr` AS `aufnr`,`a`.`matnr` AS `matnr`,`a`.`charg` AS `charg`,`a`.`sapMenge` AS `sapMenge`,`a`.`ysb` AS `ysb`,`a`.`wsb` AS `wsb`,`b`.`订单号` AS `sap_no`,`b`.`物料号` AS `material_code`,`b`.`批次号` AS `batch_no`,`b`.`totalWeight` AS `totalWeight` from (((select `cmr`.`gmt_create` AS `上报时间`,`cmr`.`section_no` AS `单晶编号`,`cmr`.`order_no` AS `订单号`,`cmmr`.`material_code` AS `物料号`,`plc`.`batch_no` AS `批次号`,sum(`yid`.`theory_weight`) AS `totalWeight` from (((`jinko_mes`.`crystal_material_relation` `cmr` left join `jinko_mes`.`yb_inspect_data` `yid` on((`yid`.`section_no` = `cmr`.`section_no`))) left join `jinko_mes`.`production_long_crystal` `plc` on((`plc`.`crystal_no` = `yid`.`crystal_no`))) left join `jinko_mes`.`crystal_material_relation` `cmmr` on(((`yid`.`crystal_no` = `cmmr`.`section_no`) and (`cmmr`.`device_type` = 4)))) where ((`cmr`.`gmt_create` between :startDate and :endDate) and (`cmr`.`device_type` = 3)) group by `cmmr`.`material_code`,`plc`.`batch_no`,`cmr`.`order_no`)) `b` left join (select `jinko_mes`.`report_sap_goodsmvt`.`aufnr` AS `aufnr`,`jinko_mes`.`report_sap_goodsmvt`.`matnr` AS `matnr`,`jinko_mes`.`report_sap_goodsmvt`.`charg` AS `charg`,sum(`jinko_mes`.`report_sap_goodsmvt`.`menge`) AS `sapMenge`,sum(if((`jinko_mes`.`report_sap_goodsmvt`.`status` = 1),`jinko_mes`.`report_sap_goodsmvt`.`menge`,0)) AS `ysb`,sum(if((`jinko_mes`.`report_sap_goodsmvt`.`status` = 0),`jinko_mes`.`report_sap_goodsmvt`.`menge`,0)) AS `wsb` from `jinko_mes`.`report_sap_goodsmvt` where ((`jinko_mes`.`report_sap_goodsmvt`.`budat` between :startDate and :endDate) and (`jinko_mes`.`report_sap_goodsmvt`.`proc_name` = '刀剖消耗')) group by `jinko_mes`.`report_sap_goodsmvt`.`aufnr`,`jinko_mes`.`report_sap_goodsmvt`.`matnr`,`jinko_mes`.`report_sap_goodsmvt`.`charg`) `a` on(((`a`.`aufnr` = `b`.`订单号`) and (`a`.`matnr` = `b`.`物料号`) and (`a`.`charg` = `b`.`批次号`))))";
		
		StringBuffer bf = new StringBuffer(sql);
		
		StringBuffer whereString = new StringBuffer(" where 1=1 ");
		
		if(StringUtils.isNotBlank(aufnr)) {
			whereString.append(" and a.aufnr like '%"+aufnr+"%'");
		}
		
		if(StringUtils.isNotBlank(matnr)) {
			whereString.append(" and a.matnr like '%"+matnr+"%'");
		}
		
		if(StringUtils.isNotBlank(charg)) {
			whereString.append(" and a.charg like '%"+charg+"%'");
		}
		
		bf.append(whereString);
		
		Query query = entityManager.createNativeQuery(bf.toString());
		query.setParameter("startDate", startDate);
		query.setParameter("endDate", endDate);
		
		query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		//query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.aliasToBean(Map.class));
		List list = query.getResultList();
		return list;
	}

}
