package com.palmnest.dao.release.impl;

import com.palmnest.application.bean.Page;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.dao.release.GoodsReleaseDao;
import com.palmnest.model.release.GoodsRelease;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

@Slf4j
@Repository("goodsReleaseDao")
public class GoodsReleaseDaoImpl extends GenericDaoHibernate<GoodsRelease, Long> implements GoodsReleaseDao {


	public GoodsReleaseDaoImpl() {
		super(GoodsRelease.class);
	}


	@Override
	public Page findRecordPageBy(Map<String, Object> params, int pageSize, int pageNo) {


		log.info("GoodsReleaseDaoImpl findRecordPageBy======");

		StringBuilder sql = new StringBuilder("SELECT org.`name` orgName, vir.name virName,DATE_FORMAT(bill.createTime,'%Y-%m-%d %H:%i') createTime,").append("\n");
		sql.append("gr.apply_user applyUser,gr.apply_tel applyTel,bill.temperature,").append("\n");
		sql.append("(case when bill.operationType='1' then '同意'  when bill.operationType='2' then '拒绝' else '' end ) status,appUser.FULL_NAME releaseUser").append("\n");
		sql.append("from tb_goods_release_op_record bill ").append("\n");
		sql.append("left join tb_goods_release gr on gr.id=bill.releasebillId").append("\n");
		sql.append("left join tb_virtualarchitecture vir on bill.companyid=vir.id").append("\n");
		sql.append("left join tb_organization org on org.id=bill.orgId").append("\n");
		sql.append("left join tb_app_user appUser on appUser.id=bill.releaseUseId").append("\n");


		sql.append(" where 1=1 ");

		if (params.get("orgIds") != null && StringUtils.isNotEmpty(params.get("orgIds").toString())) {
			sql.append(" and bill.orgId in (" + params.get("orgIds") + ")");
		}


		if (params.get("orgId") != null && !params.get("orgId").equals("")) {
			sql.append(" and bill.orgId = " + params.get("orgId")).append("\n");
		}

		if (params.get("virtualArchId") != null && !params.get("virtualArchId").equals("")) {
			sql.append(" and bill.companyId = " + params.get("virtualArchId")).append("\n");
		}

		if (params.get("applyUser") != null && !params.get("applyUser").equals("")) {
			sql.append(" and gr.apply_user like '%" + params.get("applyUser") + "%'");
		}

		if (params.get("applyTel") != null && !params.get("applyTel").equals("")) {
			sql.append(" and gr.apply_tel like '%" + params.get("applyTel") + "%'");
		}
		
		/*if (params.get("releaseStartDate") != null && !params.get("releaseStartDate").equals("")) {
			sql.append(" and bill.createTime >= str_to_date('" + params.get("releaseStartDate") + "','%Y-%m-%d') ").append("\n");
		}
		if (params.get("releaseEndDate") != null && !params.get("releaseEndDate").equals("")) {
			sql.append(" and bill.createTime <= str_to_date('" + params.get("releaseEndDate") + "','%Y-%m-%d') ").append("\n");
		}*/

		if (params.get("releaseStartDate") != null && !params.get("releaseStartDate").equals("")) {
			sql.append(" and bill.createTime >= str_to_date('" + params.get("releaseStartDate") + " 00:00:00','%Y-%m-%d %H:%i:%s') ");
		}
		if (params.get("releaseEndDate") != null && !params.get("releaseEndDate").equals("")) {
			sql.append(" and bill.createTime <= str_to_date('" + params.get("releaseEndDate") + " 23:59:59','%Y-%m-%d %H:%i:%s') ");
		}


		sql.append(" order by bill.createTime desc").append("\n");

		log.info("GoodsReleaseDaoImpl findRecordPageBy sql" + sql.toString());

		return pagedQueryByJdbcTemplate(sql.toString(), pageNo, pageSize);
	}


	@Override
	public Page findStatisticsPageBy(Map<String, Object> params, int pageSize, int pageNo) {


		log.info("GoodsReleaseDaoImpl findStatisticsPageBy======");

		StringBuilder sql = new StringBuilder("SELECT  DATE_FORMAT(bill.createTime,'%Y-%m-%d')createTime,vir.name virName, ").append("\n");
		sql.append("count(DISTINCT bill.applyPersonId) applyPersonNum,").append("\n");
		sql.append("count(bill.id) passNum").append("\n");
		sql.append("from tb_goods_release_op_record bill").append("\n");
		sql.append("left join tb_goods_release gr on gr.id=bill.releasebillId").append("\n");
		sql.append("left join tb_virtualarchitecture vir on bill.companyid=vir.id").append("\n");


		sql.append(" where 1=1 ");

		if (params.get("orgIds") != null && StringUtils.isNotEmpty(params.get("orgIds").toString())) {
			sql.append(" and bill.orgId in (" + params.get("orgIds") + ")");
		}

		if (params.get("orgId") != null && !params.get("orgId").equals("")) {
			sql.append(" and bill.orgId = " + params.get("orgId")).append("\n");
		}

		if (params.get("operationType") != null && !params.get("operationType").equals("")) {
			sql.append(" and bill.operationType = " + params.get("operationType")).append("\n");
		}

		if (params.get("releaseStartDate") != null && !params.get("releaseStartDate").equals("")) {
			sql.append(" and bill.createTime >= str_to_date('" + params.get("releaseStartDate") + " 00:00:00','%Y-%m-%d %H:%i:%s') ");
		}
		if (params.get("releaseEndDate") != null && !params.get("releaseEndDate").equals("")) {
			sql.append(" and bill.createTime <= str_to_date('" + params.get("releaseEndDate") + " 23:59:59','%Y-%m-%d %H:%i:%s') ");
		}


		sql.append("GROUP BY vir.name, DATE_FORMAT(bill.createTime,'%Y-%m-%d')").append("\n");

		log.info("GoodsReleaseDaoImpl findStatisticsPageBy sql" + sql.toString());

		return pagedQueryByJdbcTemplate(sql.toString(), pageNo, pageSize);
	}


	@Override
	public Page findPageBy(Map<String, Object> params, int pageSize, int pageNo) {

		if (!params.containsKey("applyType") || params.get("applyType") == null) {
			params.put("applyType", 0);
		}

		log.info("GoodsReleaseDaoImpl findPageBy======");

		StringBuilder sql = new StringBuilder("select t.id id,vir.roomNames,t.processName,t.type,t.roleId,t.roleName,t.sort,t.code releaseCode,t.release_status releaseStatus,").append("\n");
		sql.append("(case when t.personType='1' then '返回本市人员'  when t.personType='2' then '返回本市人员' else '' end ) personTypeName,").append("\n");
		sql.append("(case when t.isToepidemic='3' then '无'  when t.isToepidemic='4' then '有' else '无' end ) isToepidemicName,").append("\n");
		
		
		sql.append("(case when t.isSevenDayCheckResult='0' then '阴'  when t.isSevenDayCheckResult='1' then '阳' when t.isSevenDayCheckResult='2' then '未检测' else '' end ) isSevenDayCheckResultName,").append("\n");		
		
		sql.append(" t.apply_user applyUser,t.apply_tel applyTel, ").append("\n");
		
		sql.append(" ( ").append("\n");
		sql.append(" SELECT GROUP_CONCAT(goodsName,'*',amount,if ( company IS NULL, '', concat('*',company)))  from tb_goods_detail  ").append("\n");
		sql.append(" where releaseId =  t.id ").append("\n");
		sql.append(" GROUP BY releaseId ").append("\n");
		sql.append(" ) releaseGoods, ").append("\n");
		
		sql.append(" ( ").append("\n");
		sql.append(" SELECT ").append("\n");
		sql.append(" GROUP_CONCAT( ").append("\n");
		sql.append(" CONCAT(createTime,' ',content,' ', ").append("\n");
		sql.append(" case when reasonType='1' then '审核通过'  when reasonType='0' then ' 拒绝放行' when reasonType='3' then ' 放行申请' else '修改通行申请' end 	)  ").append("\n");
		sql.append("  Separator '#@'  )operateStrs from tb_audit_enterprise  ").append("\n");
		sql.append(" where 	 type='3' and sourseId = t.id	order by createTime ").append("\n");
		sql.append("  ) operateStrs, ").append("\n");
		
		sql.append(" (case when t.release_status='1' then '待放行'  when t.release_status='5' then ' 已放行' when t.release_status='10' then ' 审核中' else '已拒绝' end ) releaseStatusName, ").append("\n");
		
		
		sql.append("t.nativePlace,t.personType,t.isSevenDayCheckResult,t.leaveCityTime,t.returnCityTime,t.habitation,t.isToepidemic,t.isHaveSymptom,t.applyType,t.symptomName,t.passNum,").append("\n");
		sql.append("CONCAT(t.apply_user,' ',t.apply_tel) applyUserInfo,t.company_headuser companyHeadUser,");//发起人信息
		sql.append("t.release_desc releaseDesc,").append("\n");
		sql.append("t.applypic_url applypicUrl,t.releasepic_url releasepicUrl,").append("\n");
		sql.append("vir.name companyName,org.name orgName,").append("\n");
		sql.append("DATE_FORMAT(t.create_time,'%Y-%m-%d %H:%i') createTime").append("\n");
		sql.append("from tb_goods_release t").append("\n");

		//sql.append("join tb_dictionary_item t2 on t.release_type=t2.id").append("\n");

		sql.append("left join tb_virtualarchitecture vir on t.companyid=vir.id").append("\n");
		sql.append("left join tb_organization org on org.id=t.orgId").append("\n");

		sql.append("left join tb_user u on u.id = t.userId").append("\n");
		sql.append("left join tb_department dp on dp.id = u.departmentId").append("\n");

		sql.append(" where 1=1 ");

		if (params.get("orgIds") != null && StringUtils.isNotEmpty(params.get("orgIds").toString())) {
			sql.append(" and t.orgId in (" + params.get("orgIds") + ")");
		}
		if (params.get("virAccountId") != null) {
			sql.append(" and t.virAccountId =" + params.get("virAccountId"));
		}
		if (params.get("userId") != null) {
			sql.append(" and t.userId =" + params.get("userId"));
		}
		if (params.get("releaseCode") != null && !params.get("releaseCode").equals("")) {
			//sql.append(" and t.code = '" + params.get("releaseCode") + "'").append("\n");
			sql.append(" and t.code like '%" + params.get("releaseCode") + "%'");
		}
		
		//匹配角色
		if (params.get("userPermId") != null && !params.get("userPermId").equals("")) {
			sql.append(" and t.roleId = '" + params.get("userPermId") + "'").append("\n");
		}
		
		//微信端-放行记录 查看自己的记录
		if (params.get("creatorBillId") != null && !params.get("creatorBillId").equals("")) {
			sql.append(" and t.creatorBillId = '" + params.get("creatorBillId") + "'").append("\n");
		}

		if (params.get("releaseStatus") != null && !params.get("releaseStatus").equals("")) {
			if (((String) params.get("releaseStatus")).indexOf(",") != -1) {
				sql.append("  and t.release_status in (" + params.get("releaseStatus") + ")").append("\n");
			} else {
				sql.append("  and t.release_status =" + params.get("releaseStatus")).append("\n");
			}
		}

		//发起时间
		if (params.get("billStartDate") != null && !params.get("billStartDate").equals("")) {
			//sql.append(" and t.create_time >= str_to_date('" + params.get("billStartDate") + "','%Y-%m-%d') ").append("\n");
			sql.append(" and t.create_time >= str_to_date('" + params.get("billStartDate") + " 00:00:00','%Y-%m-%d %H:%i:%s') ");
		}
		if (params.get("billEndDate") != null && !params.get("billEndDate").equals("")) {
			//sql.append(" and t.create_time <= str_to_date('" + params.get("billEndDate") + "','%Y-%m-%d') ").append("\n");
			sql.append(" and t.create_time <= str_to_date('" + params.get("billEndDate") + " 23:59:59','%Y-%m-%d %H:%i:%s') ");
		}

		//放行时间
		if (params.get("releaseStartDate") != null && !params.get("releaseStartDate").equals("")) {
			//sql.append(" and t.releaseDate >= str_to_date('" + params.get("releaseStartDate") + "','%Y-%m-%d') ").append("\n");
			sql.append(" and t.releaseDate >= str_to_date('" + params.get("releaseStartDate") + " 00:00:00','%Y-%m-%d %H:%i:%s') ");
		}
		if (params.get("releaseEndDate") != null && !params.get("releaseEndDate").equals("")) {
			//sql.append(" and t.releaseDate <= str_to_date('" + params.get("releaseEndDate") + "','%Y-%m-%d') ").append("\n");
			sql.append(" and t.releaseDate <= str_to_date('" + params.get("releaseEndDate") + " 23:59:59','%Y-%m-%d %H:%i:%s') ");
		}

		if (params.get("orgId") != null && !params.get("orgId").equals("")) {
			sql.append(" and t.orgId = " + params.get("orgId")).append("\n");
		}
		if (params.get("virtualArchId") != null && !params.get("virtualArchId").equals("")) {
			sql.append(" and t.companyid = " + params.get("virtualArchId")).append("\n");
		}

		//放行类型
		if (params.get("releaseType") != null && !params.get("releaseType").equals("")) {
			sql.append(" and t.release_type = " + params.get("releaseType")).append("\n");
		}

		if (params.get("applyType") != null && !params.get("applyType").equals("")) {
			sql.append(" and t.applyType = " + params.get("applyType")).append("\n");
		}

		//微信企业负责人待审批数量脚标
		if (params.get("virIds") != null && !params.get("virIds").equals("")) {
			sql.append("  and t.companyid  in (" + params.get("virIds") + ")").append("\n");
		}
		
		if (params.get("virtualArchName") != null && !params.get("virtualArchName").equals("")) {
			sql.append(" and vir.name like '%" + params.get("virtualArchName") + "%'");
		}
		
		//放行人-安保
		if (params.get("releaseUser") != null && !params.get("releaseUser").equals("")) {
			//sql.append(" and t.releaseUser = " + params.get("releaseUser")).append("\n");
			sql.append(" and t.releaseUser like '%" + params.get("releaseUser") + "%'");
		}

		if (params.get("applyUser") != null && !params.get("applyUser").equals("")) {
			sql.append(" and t.apply_user like '%" + params.get("applyUser") + "%'");
		}
		
		if (params.get("virtualArchName") != null && !params.get("virtualArchName").equals("")) {
			sql.append(" and vir.name like '%" + params.get("virtualArchName") + "%'");
		}

		//层级关系-组织
		if (params.get("rank") != null && !params.get("rank").equals("")) {
			sql.append(" and (  dp.rank = '"+params.get("rank")+"' or dp.rank like '%" + params.get("rank") + "-%' )");
		}
		if (params.get("companyHeadUser") != null && !params.get("companyHeadUser").equals("")) {
			sql.append(" and company_headuser is not null ").append("\n");
		}

		if (params.get("companyHeadUserIsNull") != null && !params.get("companyHeadUserIsNull").equals("")) {
			sql.append(" and company_headuser is null ").append("\n");//为空的表不没有处理过的
		}
		
		/*if(params.get("releaseStatus") != null) {
			sql.append(" and t.release_status = "+params.get("releaseStatus")).append("\n");
		}*/

		sql.append(" order by t.create_time desc").append("\n");

		log.info("GoodsReleaseDaoImpl findPageBy sql" + sql.toString());

		return pagedQueryByJdbcTemplate(sql.toString(), pageNo, pageSize);
	}

 
	@Override
	public List<Map<String, Object>> reportExcel(Map<String, Object> params) {
		log.info("GoodsReleaseDaoImpl reportExcel======");

		if (!params.containsKey("applyType") || params.get("applyType") == null) {
			params.put("applyType", 0);
		}

		log.info("GoodsReleaseDaoImpl findPageBy======");

		StringBuilder sql = new StringBuilder("select t.id id,t.type,t.roleId,t.sort,t.code releaseCode,").append("\n");
		sql.append("(case when t.release_status='1' then '待放行'  when t.release_status='5' then '已放行' when t.release_status='10' then '审核中' when t.release_status='15' then '已拒绝' end ) releaseStatus,").append("\n");
		sql.append("(case when t.personType='1' then '返回本市人员'  when t.personType='2' then '返回本市人员' else '' end ) personTypeName,").append("\n");
		
		
        sql.append("(case when t.isToepidemic='3' then '无'  when t.isToepidemic='4' then '有' else '无' end ) isToepidemicName,").append("\n");	
        sql.append("(case when t.isSevenDayCheckResult='0' then '阴'  when t.isSevenDayCheckResult='1' then '阳' when t.isSevenDayCheckResult='2' then '未检测' else '' end ) isSevenDayCheckResultName,").append("\n");
		
		sql.append("t.nativePlace,t.personType,t.leaveCityTime,t.returnCityTime,t.habitation,t.isToepidemic,t.isHaveSymptom,t.applyType,t.symptomName,t.passNum,").append("\n");
		sql.append("CONCAT(t.apply_user,' ',t.apply_tel) applyUserInfo,t.company_headuser companyHeadUser,");//发起人信息
		sql.append("t.release_desc releaseDesc,").append("\n");
		sql.append("t.applypic_url applypicUrl,t.releasepic_url releasepicUrl,").append("\n");
		sql.append("vir.name companyName,org.name orgName,").append("\n");
		sql.append("DATE_FORMAT(t.create_time,'%Y-%m-%d %H:%i') createTime").append("\n");
		sql.append("from tb_goods_release t").append("\n");

		//sql.append("join tb_dictionary_item t2 on t.release_type=t2.id").append("\n");

		sql.append("left join tb_virtualarchitecture vir on t.companyid=vir.id").append("\n");
		sql.append("left join tb_organization org on org.id=t.orgId").append("\n");

		sql.append("LEFT JOIN tb_user u on u.ID = t.userId").append("\n");
		sql.append("LEFT JOIN tb_department  dp on dp.ID = u.departmentId").append("\n");

		sql.append(" where 1=1 ");

		if (params.get("orgIds") != null && StringUtils.isNotEmpty(params.get("orgIds").toString())) {
			sql.append(" and t.orgId in (" + params.get("orgIds") + ")");
		}
		if (params.get("virAccountId") != null) {
			sql.append(" and t.virAccountId =" + params.get("virAccountId"));
		}
		if (params.get("userId") != null) {
			sql.append(" and t.userId =" + params.get("userId"));
		}
		if (params.get("releaseCode") != null && !params.get("releaseCode").equals("")) {
			sql.append(" and t.code = '" + params.get("releaseCode") + "'").append("\n");
		}

		//微信端-放行记录 查看自己的记录
		if (params.get("creatorBillId") != null && !params.get("creatorBillId").equals("")) {
			sql.append(" and t.creatorBillId = '" + params.get("creatorBillId") + "'").append("\n");
		}

		if (params.get("releaseStatus") != null && !params.get("releaseStatus").equals("")) {
			if (((String) params.get("releaseStatus")).indexOf(",") != -1) {
				sql.append("  and t.release_status in (" + params.get("releaseStatus") + ")").append("\n");
			} else {
				sql.append("  and t.release_status =" + params.get("releaseStatus")).append("\n");
			}
		}

		//发起时间
		if (params.get("billStartDate") != null && !params.get("billStartDate").equals("")) {
			//sql.append(" and t.create_time >= str_to_date('" + params.get("billStartDate") + "','%Y-%m-%d') ").append("\n");
			sql.append(" and t.create_time >= str_to_date('" + params.get("billStartDate") + " 00:00:00','%Y-%m-%d %H:%i:%s') ");
		}
		if (params.get("billEndDate") != null && !params.get("billEndDate").equals("")) {
			//sql.append(" and t.create_time <= str_to_date('" + params.get("billEndDate") + "','%Y-%m-%d') ").append("\n");
			sql.append(" and t.create_time  <= str_to_date('" + params.get("billEndDate") + " 23:59:59','%Y-%m-%d %H:%i:%s') ");
		}

		//放行时间
		if (params.get("releaseStartDate") != null && !params.get("releaseStartDate").equals("")) {
			//sql.append(" and t.releaseDate >= str_to_date('" + params.get("releaseStartDate") + "','%Y-%m-%d') ").append("\n");
			sql.append(" and t.releaseDate >= str_to_date('" + params.get("releaseStartDate") + " 00:00:00','%Y-%m-%d %H:%i:%s') ");
		}
		if (params.get("releaseEndDate") != null && !params.get("releaseEndDate").equals("")) {
			//sql.append(" and t.releaseDate <= str_to_date('" + params.get("releaseEndDate") + "','%Y-%m-%d') ").append("\n");
			sql.append(" and t.releaseDate <= str_to_date('" + params.get("releaseEndDate") + " 23:59:59','%Y-%m-%d %H:%i:%s') ");
		}

		if (params.get("orgId") != null && !params.get("orgId").equals("")) {
			sql.append(" and t.orgId = " + params.get("orgId")).append("\n");
		}
		if (params.get("virtualArchId") != null && !params.get("virtualArchId").equals("")) {
			sql.append(" and t.companyid = " + params.get("virtualArchId")).append("\n");
		}

		//放行类型
		if (params.get("releaseType") != null && !params.get("releaseType").equals("")) {
			sql.append(" and t.release_type = " + params.get("releaseType")).append("\n");
		}

		if (params.get("applyType") != null && !params.get("applyType").equals("")) {
			sql.append(" and t.applyType = " + params.get("applyType")).append("\n");
		}

		//微信企业负责人待审批数量脚标
		if (params.get("virIds") != null && !params.get("virIds").equals("")) {
			sql.append("  and t.companyid  in (" + params.get("virIds") + ")").append("\n");
		}

		//放行人-安保
		if (params.get("releaseUser") != null && !params.get("releaseUser").equals("")) {
			//sql.append(" and t.releaseUser = " + params.get("releaseUser")).append("\n");
			sql.append(" and t.releaseUser like '%" + params.get("releaseUser") + "%'");
		}

		if (params.get("applyUser") != null && !params.get("applyUser").equals("")) {
			sql.append(" and t.apply_user like '%" + params.get("applyUser") + "%'");
		}

		//层级关系-组织
		if (params.get("rank") != null && !params.get("rank").equals("")) {
			sql.append(" and (  dp.rank = "+params.get("rank")+" or dp.rank like '%" + params.get("rank") + "-%' )");
		}

		if (params.get("companyHeadUser") != null && !params.get("companyHeadUser").equals("")) {
			sql.append(" and company_headuser is not null ").append("\n");
		}

		if (params.get("companyHeadUserIsNull") != null && !params.get("companyHeadUserIsNull").equals("")) {
			sql.append(" and company_headuser is null ").append("\n");//为空的表不没有处理过的
		}
		
		/*if(params.get("releaseStatus") != null) {
			sql.append(" and t.release_status = "+params.get("releaseStatus")).append("\n");
		}*/

		sql.append(" order by t.create_time desc").append("\n");

		log.info("GoodsReleaseDaoImpl findPageBy sql" + sql.toString());


		return getJdbcTemplate().queryForList(sql.toString());
	}


	@Override
	public List<Map<String, Object>> reportStatistics(Map<String, Object> params) {
		log.info("GoodsReleaseDaoImpl reportStatistics======");

		StringBuilder sql = new StringBuilder("SELECT  DATE_FORMAT(bill.createTime,'%Y-%m-%d')createTime,vir.name virName, ").append("\n");
		sql.append("count(DISTINCT bill.applyPersonId) applyPersonNum,").append("\n");
		sql.append("count(bill.id) passNum").append("\n");
		sql.append("from tb_goods_release_op_record bill").append("\n");
		sql.append("left join tb_goods_release gr on gr.id=bill.releasebillId").append("\n");
		sql.append("left join tb_virtualarchitecture vir on bill.companyid=vir.id").append("\n");

		sql.append(" where 1=1 ");

		if (params.get("orgIds") != null && StringUtils.isNotEmpty(params.get("orgIds").toString())) {
			sql.append(" and bill.orgId in (" + params.get("orgIds") + ")");
		}

		if (params.get("orgId") != null && !params.get("orgId").equals("")) {
			sql.append(" and bill.orgId = " + params.get("orgId")).append("\n");
		}

		if (params.get("releaseStartDate") != null && !params.get("releaseStartDate").equals("")) {
			sql.append(" and bill.createTime >= str_to_date('" + params.get("releaseStartDate") + " 00:00:00','%Y-%m-%d %H:%i:%s') ");
		}
		if (params.get("releaseEndDate") != null && !params.get("releaseEndDate").equals("")) {
			sql.append(" and bill.createTime <= str_to_date('" + params.get("releaseEndDate") + " 23:59:59','%Y-%m-%d %H:%i:%s') ");
		}


		sql.append("GROUP BY vir.name, DATE_FORMAT(bill.createTime,'%Y-%m-%d')").append("\n");


		log.info("GoodsReleaseDaoImpl reportStatistics sql" + sql.toString());


		return getJdbcTemplate().queryForList(sql.toString());
	}


	@Override
	public List<Map<String, Object>> reportRecord(Map<String, Object> params) {
		log.info("GoodsReleaseDaoImpl reportRecord======");

		StringBuilder sql = new StringBuilder("SELECT org.`name` orgName, vir.name virName,DATE_FORMAT(bill.createTime,'%Y-%m-%d %H:%i') createTime,").append("\n");
		sql.append("gr.apply_user applyUser,gr.apply_tel applyTel,bill.temperature,").append("\n");
		sql.append("(case when bill.operationType='1' then '同意'  when bill.operationType='2' then '拒绝' else '' end ) status,appUser.FULL_NAME releaseUser").append("\n");
		sql.append("from tb_goods_release_op_record bill ").append("\n");
		sql.append("left join tb_goods_release gr on gr.id=bill.releasebillId").append("\n");
		sql.append("left join tb_virtualarchitecture vir on bill.companyid=vir.id").append("\n");
		sql.append("left join tb_organization org on org.id=bill.orgId").append("\n");
		sql.append("left join tb_app_user appUser on appUser.id=bill.releaseUseId").append("\n");


		sql.append(" where 1=1 ");

		if (params.get("orgIds") != null && StringUtils.isNotEmpty(params.get("orgIds").toString())) {
			sql.append(" and bill.orgId in (" + params.get("orgIds") + ")");
		}


		if (params.get("orgId") != null && !params.get("orgId").equals("")) {
			sql.append(" and bill.orgId = " + params.get("orgId")).append("\n");
		}

		if (params.get("virtualArchId") != null && !params.get("virtualArchId").equals("")) {
			sql.append(" and bill.companyId = " + params.get("virtualArchId")).append("\n");
		}

		if (params.get("applyUser") != null && !params.get("applyUser").equals("")) {
			sql.append(" and gr.apply_user like '%" + params.get("applyUser") + "%'");
		}

		if (params.get("applyTel") != null && !params.get("applyTel").equals("")) {
			sql.append(" and gr.apply_tel like '%" + params.get("applyTel") + "%'");
		}
		
		/*if (params.get("releaseStartDate") != null && !params.get("releaseStartDate").equals("")) {
			sql.append(" and bill.createTime >= str_to_date('" + params.get("releaseStartDate") + "','%Y-%m-%d') ").append("\n");
		}
		if (params.get("releaseEndDate") != null && !params.get("releaseEndDate").equals("")) {
			sql.append(" and bill.createTime <= str_to_date('" + params.get("releaseEndDate") + "','%Y-%m-%d') ").append("\n");
		}*/

		if (params.get("releaseStartDate") != null && !params.get("releaseStartDate").equals("")) {
			sql.append(" and bill.createTime >= str_to_date('" + params.get("releaseStartDate") + " 00:00:00','%Y-%m-%d %H:%i:%s') ");
		}
		if (params.get("releaseEndDate") != null && !params.get("releaseEndDate").equals("")) {
			sql.append(" and bill.createTime <= str_to_date('" + params.get("releaseEndDate") + " 23:59:59','%Y-%m-%d %H:%i:%s') ");
		}


		sql.append(" order by bill.createTime desc").append("\n");


		log.info("GoodsReleaseDaoImpl reportRecord sql" + sql.toString());


		return getJdbcTemplate().queryForList(sql.toString());
	}


	@Override
	public List<Map<String, Object>> getStatisticsData(Map<String, Object> params) {
		log.info("GoodsReleaseDaoImpl getStatisticsData======");


		StringBuilder sql = new StringBuilder("select count(DISTINCT applyPersonId) totalPersonNum, ##累计通行人数").append("\n");
		sql.append("count(*) totalPassNum, ##累计通行次数").append("\n");

		sql.append(" (select count(*) from tb_goods_release_op_record where operationType=2").append("\n");
		if (params.get("orgId") != null && !params.get("orgId").equals("")) {
			sql.append(" and orgId = " + params.get("orgId")).append("\n");
		}
		sql.append("  ) refusePersonNum, ##累积拒绝通行次数").append("\n");


		sql.append("(SELECT count(DISTINCT applyPersonId) todayPersonNum from tb_goods_release_op_record where to_days(createTime) = to_days(now())  ").append("\n");
		if (params.get("orgId") != null && !params.get("orgId").equals("")) {
			sql.append(" and orgId = " + params.get("orgId")).append("\n");
		}
		sql.append(" ) todayPersonNum,##今日通行人数").append("\n");

		sql.append(" sum(case when to_days(createTime) = to_days(now()) then 1 else 0 end) todayPassNum ##今日通行次数").append("\n");
		sql.append("from tb_goods_release_op_record where operationType=1").append("\n");

		if (params.get("orgIds") != null && StringUtils.isNotEmpty(params.get("orgIds").toString())) {
			sql.append(" and orgId in (" + params.get("orgIds") + ")");
		}
		if (params.get("virAccountId") != null) {
			sql.append(" and virAccountId =" + params.get("virAccountId"));
		}

		if (params.get("orgId") != null && !params.get("orgId").equals("")) {
			sql.append(" and orgId = " + params.get("orgId")).append("\n");
		}

		log.info("GoodsReleaseDaoImpl getStatisticsData sql" + sql.toString());


		return getJdbcTemplate().queryForList(sql.toString());
	}


	@Override
	public Integer findCountBy(Map<String, Object> params) {

		if (!params.containsKey("applyType") || params.get("applyType") == null) {
			params.put("applyType", 0);
		}

		log.info("GoodsReleaseDaoImpl findCountBy======");

		StringBuilder sql = new StringBuilder("select count(id) total ").append("\n");
		sql.append("from tb_goods_release").append("\n");
		;//发起人信
		sql.append("where release_status='10'").append("\n");
		;


//		if(params.get("releaseType") != null && !params.get("releaseType").equals("")) {
//			sql.append(" and release_type = "+params.get("releaseType")).append("\n");
//		}

		if (params.get("companyHeadUser") != null && !params.get("companyHeadUser").equals("")) {
			sql.append(" and company_headuser is not null ").append("\n");
		}

		if (params.get("companyHeadUserIsNull") != null && !params.get("companyHeadUserIsNull").equals("")) {
			sql.append(" and company_headuser is null ").append("\n");//为空的表不没有处理过的
		}

		//微信企业负责人待审批数量脚标
		if (params.get("virIds") != null && !params.get("virIds").equals("")) {
			sql.append("  and companyid  in (" + params.get("virIds") + ")").append("\n");
		}
		if (params.get("orgIds") != null && !params.get("orgIds").equals("")) {
			sql.append("  and orgId  in (" + params.get("orgIds") + ")").append("\n");
		}

		if (params.get("applyType") != null && !params.get("applyType").equals("")) {
			sql.append(" and applyType = " + params.get("applyType")).append("\n");
		}

		log.info("GoodsReleaseDaoImpl findCountBy sql" + sql.toString());
		BigInteger i = (BigInteger) getSession().createSQLQuery(sql.toString()).uniqueResult();
		return i.intValue();
	}

	@Override
	public List<GoodsRelease> getGoodsReleaseByParams(Map<String, Object> params) {

		StringBuilder sql = new StringBuilder(" from GoodsRelease where 1=1 ");

		if (params.get("releaseStatus") != null && !params.get("releaseStatus").equals("")) {
			sql.append("  and release_status  in (" + params.get("releaseStatus") + ")").append("\n");
		}
		if (params.get("userId") != null && !params.get("userId").equals("")) {
			sql.append(" and userId =" + params.get("userId"));
		}

		if (params.get("applyType") != null && !params.get("applyType").equals("")) {//applyType
			sql.append(" and applyType = " + params.get("applyType")).append("\n");
		}

		if (params.get("virtualArchId") != null && !params.get("virtualArchId").equals("")) {
			sql.append(" and companyid =" + params.get("virtualArchId"));
		}


		return getSession().createQuery(sql.toString()).list();
	}


	@Override
	public List<Map<String, Object>> getVirIdsByOpenId(String openId, Integer isReleasePower) {

		log.info("GoodsReleaseDaoImpl getVirIdsByOpenId======");

		StringBuilder sql = new StringBuilder("select vu.virId from tb_wxuser wx ").append("\n");
		sql.append("left join tb_user tu on wx.userId=tu.id ").append("\n");
		;//发起人信息
		sql.append("left join tb_virtuser vu on tu.id=vu.userId").append("\n");
		sql.append("where wx.userId is not null ").append("\n");
		sql.append("and tu.userMobile is not null").append("\n");
		sql.append("and wx.openId='" + openId + "' ").append("\n");

		if (null != isReleasePower && isReleasePower == 1) {
			sql.append("and tu.isReleasePower= " + isReleasePower).append("\n");
		}


		log.info("GoodsReleaseDaoImpl getVirIdsByOpenId sql" + sql.toString());

		List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());

		if (list.size() == 0) {
			return null;
		} else if (list.size() == 1 && StringUtils.isEmpty(String.valueOf(list.get(0).get("virId")))) {
			return null;
		} else {
			return list;
		}

	}

}
