报餐信息写入消费机数据库

/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.consumption.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.consumption.constant.ConsumptionConstant;
import org.springblade.consumption.entity.ConsumptionMachine;
import org.springblade.consumption.entity.ConsumptionMeal;
import org.springblade.consumption.entity.ExcelConsumptionMachine;
import org.springblade.consumption.entity.WriteInConsumptionMachine;
import org.springblade.consumption.mapper.ConsumptionMachineMapper;
import org.springblade.consumption.service.IConsumptionMachineService;
import org.springblade.consumption.utils.CMDateTimeUtil;
import org.springblade.consumption.utils.ConsumptionUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 员工培训记录明细
 *
 * @author pei
 */
@Slf4j
@Service
@AllArgsConstructor
public class ConsumptionMachineServiceImpl extends ServiceImpl<ConsumptionMachineMapper, ConsumptionMachine> implements IConsumptionMachineService {
	ConsumptionMachineMapper consumptionMachineMapper;
	/**
	 * 同步消费机信息
	 * @param list
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean saveConsumptionMachine(List<ConsumptionMachine> list) {
		Date date= CMDateTimeUtil.getDateTimes();
		if(Func.isNotEmpty(list)){
			for(ConsumptionMachine bean:list){
				//根据用户编号获取员工工号
				String account= consumptionMachineMapper.getAccountByPersonCode(bean.getPersonCode());
				bean.setAccount(account);
				//同步时间
				bean.setSynchronizationTime(date);
				//数据来源
				bean.setDataSource("同步");
				bean.setId(CMDateTimeUtil.getMillisecond());
				bean.setTenantId(ConsumptionConstant.TENANT_ID);
				save(bean);
			}
		}
		return true;
	}
	/**
	 * 修改保存消费机信息
	 * @param bean
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean saveAndUpdateConsumptionMachine(ConsumptionMachine bean) {
		return saveOrUpdate(bean);
	}

	/**
	 *  删除消费机信息
	 * @param ids
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean deleteConsumptionMachine(String ids) {
		String[]ide=ids.split(",");
		for(String id:ide){
			removeById(id);
		}
		return true;
	}

	/**
	 * 报餐对比
	 * @param mealType
	 * @param posoptime
	 * @return
	 */
	@Override
	public List<ConsumptionMachine> getMealComparison(String mealType, Date posoptime) {
		//根据时间和餐别查询 报餐表
		List<ConsumptionMeal>mealList=consumptionMachineMapper.getMealComparison(mealType,posoptime);
		log.info("mealList.size====="+mealList.size());
		//根据时间和餐别查询 消费机信息表
		ConsumptionMachine bean =new ConsumptionMachine();
		bean.setTenantId(ConsumptionConstant.TENANT_ID);
		bean.setMealType(mealType);
		//bean.setPosoptime(posoptime);
		//将日期类型转换成字符 获取年月日
		bean.setStartDate(posoptime);
		bean.setEndDate(CMDateTimeUtil.getNowDateTime(posoptime," 23:59:59"));
		log.info("getNowDateTime===="+CMDateTimeUtil.getNowDateTime(posoptime," 23:59:59"));
		List<ConsumptionMachine>consumptionList=consumptionMachineMapper.getConsumptionMachineList(bean);
		log.info("consumptionList========="+consumptionList);
		List<ConsumptionMeal> toList=new ArrayList<>();
		if(Func.isNotEmpty(mealList)&&Func.isNotEmpty(consumptionList)){
			for(ConsumptionMeal meal:mealList){
				for(ConsumptionMachine consumption:consumptionList){
					//如果工号相同 从报餐list中移除
					if(meal.getMealUserCode().equals(consumption.getAccount())){
						toList.add(meal);
					}
				}
			}
		}
		// 循环移除正常的人员信息
		if(Func.isNotEmpty(toList)){
			if(mealList.containsAll(toList)){
				mealList.removeAll(toList);
			}
		}

		//删除后剩下的就是报餐没有吃饭的人
		log.info("mealList============"+mealList.size());
		List<ConsumptionMachine>returnList=new ArrayList<>();
		if(Func.isNotEmpty(toList)){
			for(ConsumptionMeal meal1:mealList){
				ConsumptionMachine machine=new ConsumptionMachine();
				machine.setDeptname(meal1.getApplyDeptName());
				machine.setName(meal1.getMealUserName());
				machine.setMealType(meal1.getMealTypeName());
				machine.setPosoptime(meal1.getMealDate());
				returnList.add(machine);
			}

		}
		return returnList;
	}
	/**
	 * 刷卡对比 查看谁没有报餐刷卡了
	 * @param mealType
	 * @param posoptime
	 * @return
	 */
	@Override
	public List<ConsumptionMachine> getConsumptionComparison(String mealType, Date posoptime) {
		//根据时间和餐别查询 报餐表
		List<ConsumptionMeal>mealList=consumptionMachineMapper.getMealComparison(mealType,posoptime);
		//根据时间和餐别查询 消费机信息表
		ConsumptionMachine bean =new ConsumptionMachine();
		bean.setTenantId(ConsumptionConstant.TENANT_ID);
		bean.setMealType(mealType);
		//bean.setPosoptime(posoptime);
		//将日期类型转换成字符 获取年月日
		bean.setStartDate(posoptime);
		bean.setEndDate(CMDateTimeUtil.getNowDateTime(posoptime," 23:59:59"));
		List<ConsumptionMachine>consumptionList=consumptionMachineMapper.getConsumptionMachineList(bean);
		log.info("consumptionList========="+consumptionList);
		List<ConsumptionMachine>toMachineList=new ArrayList<>(); //报餐了 也刷卡了的人员列表
		if(Func.isNotEmpty(mealList)&&Func.isNotEmpty(consumptionList)){
			for(ConsumptionMachine machine:consumptionList){
				for(ConsumptionMeal meal:mealList){
					if(meal.getMealUserCode().equals(machine.getAccount())){
						toMachineList.add(machine);
					}
				}
			}
		}
		//从消费机信息中移除报餐了同时也刷卡的人员,剩下的是刷卡了没有报餐
		if(Func.isNotEmpty(toMachineList)){
			if(consumptionList.containsAll(toMachineList)){
				consumptionList.removeAll(toMachineList);
			}
		}
		return consumptionList;
	}
	/**
	 * 报餐刷卡综合对比
	 * @param mealType
	 * @param posoptime
	 * @return
	 */
	@Override
	public List<ExcelConsumptionMachine> getSynthesizeComparison(String mealType, Date posoptime) {
		log.info("报餐刷卡综合对比mealType===="+mealType+"---posoptime===="+posoptime);
		//根据日期餐别查询报餐信息,如果没有餐别查询一日三餐
		List<ConsumptionMeal>mealList=consumptionMachineMapper.getMealComparison(mealType,posoptime);
		log.info("mealList======"+mealList);
		//将报餐信息转入消费实体类,放入新的消费list,
		List<ExcelConsumptionMachine>toList=new ArrayList<>();
		if(Func.isNotEmpty(mealList)){
			for(ConsumptionMeal meal:mealList){
				ExcelConsumptionMachine machine=new ExcelConsumptionMachine();
				machine.setMealDate(meal.getMealDate());
				machine.setName(meal.getMealUserName());
				machine.setMealType(meal.getMealTypeName());
				machine.setAccount(meal.getMealUserCode());
				toList.add(machine);
			}
		}
		//根据时间和餐别查询 消费机信息表 如果餐别为空查询一日三餐
		ConsumptionMachine bean =new ConsumptionMachine();
		bean.setTenantId(ConsumptionConstant.TENANT_ID);
		bean.setMealType(mealType);
		//bean.setPosoptime(posoptime);
		//将日期类型转换成字符 获取年月日
		bean.setStartDate(posoptime);
		bean.setEndDate(CMDateTimeUtil.getNowDateTime(posoptime," 23:59:59"));
		List<ExcelConsumptionMachine>consumptionList=consumptionMachineMapper.getExcelConsumptionMachineList(bean);
		log.info("根据时间和餐别查询 消费机信息表consumptionList======="+consumptionList);
		if(Func.isNotEmpty(toList)&& Func.isNotEmpty(consumptionList)){
			log.info("toList========"+toList);
			log.info("consumptionList===="+consumptionList);
			for(ExcelConsumptionMachine machine1:toList){
				for(ExcelConsumptionMachine machine2:consumptionList){
					//如果报餐人工号和消费工号相同,该用户报餐了也刷卡吃饭了
					if(machine1.getAccount().equals(machine2.getAccount())&& machine1.getMealType().equals(machine2.getMealType()) ){
						//正常
						machine1.setStatus(ConsumptionConstant.STATUS1);
						machine1.setPosoptime(machine2.getPosoptime());
						machine1.setDeptname("");
						//将信息和消费列表信息同步,用于筛选对象是否存在
						//就餐日期
						machine2.setMealDate(machine1.getMealDate());
						machine2.setStatus(ConsumptionConstant.STATUS1);
						machine2.setDeptname("");
					}

				}

			}
			//筛选出消费了没有报餐的人将信息放入返回list
			for(ExcelConsumptionMachine machine3:consumptionList){
				//如果list中不存在,用户消费了没有报餐
				if(!toList.contains(machine3)){
					//未报餐
					machine3.setStatus(ConsumptionConstant.STATUS2);
					toList.add(machine3);
				}
			}
			//如果刷卡时间为空,用户报餐了未刷卡
			for(ExcelConsumptionMachine machine4:toList){
				if(Func.isEmpty(machine4.getPosoptime())){
					//未刷卡
					machine4.setStatus(ConsumptionConstant.STATUS3);
				}
			}
			//将部门名称放入list中
			for(ExcelConsumptionMachine machine5:toList){
				//根据工号获取部门部门id
				List<String>strList=consumptionMachineMapper.getDeptId(ConsumptionConstant.TENANT_ID,machine5.getAccount());
				//根据部门id获取部门名称
				if(Func.isNotEmpty(strList)){
					String deptName=consumptionMachineMapper.getDeptName(ConsumptionConstant.TENANT_ID,Long.valueOf(strList.get(0).split(",")[0]));
					//部门名称
					machine5.setDeptname(deptName);
				}
			}
		}
		return toList;
	}

	/**
	 * 将报餐信息写入消费机
	 * @return
	 */
	@Override
	public List<ConsumptionMeal> writeInConsumptionMachine() {
		//获取当前日明天的日期(就餐日期)
		Date tomorrow=CMDateTimeUtil.getTomorrow();
		log.info("tomorrow========="+tomorrow);
		//根据就餐日期获取员工报餐信息,如果没有餐别查询一日三餐
		List<ConsumptionMeal>mealList=consumptionMachineMapper.getMealComparison("",tomorrow);
		log.info("如果没有餐别查询一日三餐mealList====="+mealList);
		log.info("mealList.size=================="+mealList.size());
		//没有录入系统人员信息员工列表
		List<ConsumptionMeal>noMsgPersonList=new ArrayList<>();
		if(Func.isNotEmpty(mealList)){
			for(ConsumptionMeal meal:mealList){
				//根据用户工号获取消费编号,工号是唯一的
				String personCode=consumptionMachineMapper.getPersonCode(ConsumptionConstant.TENANT_ID,meal.getMealUserCode());
				if(Func.isNotEmpty(personCode)){
					meal.setPersonCode(personCode);
					//根据消费编号获取消费机user_id
					String sql="select userid from userinfo where badgenumber= ? ";
					Map<String,String>map=new HashMap<>();
					map.put("personCode",personCode);
					List<Map<String,Object>>list=ConsumptionUtil.getSqlResult(sql,map);
					if(Func.isNotEmpty(list)){
						if(list.get(0).containsKey("userid")){
							meal.setUserid(Integer.valueOf(String.valueOf(list.get(0).get("userid"))));
							//log.info("name====="+meal.getMealUserName()+"----userid===="+list.get(0).get("userid"));
						}
					}
				}else{
					//将为录入系统的人员放入list中
					noMsgPersonList.add(meal);
				}
			}
            log.info("没有录入系统人员信息员工列表noMsgPersonList===================="+noMsgPersonList);
			//将没有录入系统的人员从mealList中删除
			if(Func.isNotEmpty(noMsgPersonList)){
				mealList.removeAll(noMsgPersonList);
			}
			//将信息转入写入消费的实体类,放入list中
			List<WriteInConsumptionMachine> writeBeanList=new ArrayList<>();
			for(ConsumptionMeal meal1:mealList){
				WriteInConsumptionMachine writeBean=new WriteInConsumptionMachine();
				String bookTime=CMDateTimeUtil.getStringDate(meal1.getMealDate());
				String bookDate=CMDateTimeUtil.getStringDate(meal1.getApplyTime());
				writeBean.setBookTime(bookTime.split(" ")[0]);
				writeBean.setBookDate(bookDate.split(" ")[0]);
				writeBean.setUserId(meal1.getUserid());
				writeBean.setMealId(Integer.valueOf(meal1.getMealType()));
				writeBeanList.add(writeBean);
				//log.info("meal1.getUserid()=================="+meal1.getUserid());
			}
			//log.info("writeBeanList============="+writeBeanList);
			log.info("size=============="+writeBeanList.size());
			//将报餐信息循环写入消费机
			boolean flag=true;
			String sql1="insert into ipos_bookdinner (book_time,book_date,userid_id,meal_id) values(?,?,?,?)";
			for(WriteInConsumptionMachine bean1:writeBeanList){
				flag=ConsumptionUtil.writeMsg(sql1,bean1);
				if(!flag){
					log.info("写入消费机失败信息bean1==============="+bean1);
				}
			}
		}
		return noMsgPersonList;
	}
	/**
	 *手动添加报餐信息到消费机
	 * @param bean
	 * @return
	 */
	@Override
	public boolean saveMsgToMachine(WriteInConsumptionMachine bean) {
		boolean flag=true;
		log.info("手动添加报餐信息到消费机bean====="+bean);
		if(Func.isNotEmpty(bean)){
			String  bookDate=CMDateTimeUtil.getYesterdays();
			String[] accounts=bean.getAccount().split(",");
			for(String account:accounts){
				//根据用户工号获取消费编号,工号是唯一的
				String personCode=consumptionMachineMapper.getPersonCode(ConsumptionConstant.TENANT_ID,account);
				//根据消费编号获取消费机user_id
				String sql="select userid from userinfo where badgenumber= ? ";
				Map<String,String>map=new HashMap<>();
				map.put("personCode",personCode);
				List<Map<String,Object>>list=ConsumptionUtil.getSqlResult(sql,map);
				if(Func.isNotEmpty(list)){
					if(list.get(0).containsKey("userid")){
						int userid=Integer.valueOf(String.valueOf(list.get(0).get("userid")));
						bean.setUserId(userid);
						bean.setBookDate(bookDate);
						String sql1="insert into ipos_bookdinner (book_time,book_date,userid_id,meal_id) values(?,?,?,?)";
						flag=ConsumptionUtil.writeMsg(sql1,bean);
						if(!flag){
							log.info("写入消费机失败=======");
							return false;
						}
					}
				}
			}
		}
		return true;
	}


}

  //日期工具类

package org.springblade.consumption.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CMDateTimeUtil {
	/**
	 * 获取当前时间精确到秒
	 * @return
	 */
	public static Date getDateTimes(){
		Date date=null;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String times=df.format(new Date());// new Date()为获取当前系统时间
		//将字符串转换成成日期格式
		try {
			date= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(times);
		}catch (Exception e){
			e.printStackTrace();
		}
		return date;
	}
	/**
	 * 获取当前时间戳精确到毫秒
	 */
	public static String getMillisecond(){
		return new SimpleDateFormat("yyyyMMddHHmmssSSS") .format(new Date() );
	}
	/**
	 * 获取当前日期 日期格式
	 */
	public static Date getNowDate(){
		Date date=null;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		String times=df.format(new Date());// new Date()为获取当前系统时间
		//将字符串转换成成日期格式
		try {
			date= new SimpleDateFormat("yyyy-MM-dd").parse(times);
		}catch (Exception e){
			e.printStackTrace();
		}
		return date;
	}

	/**获取给定日期的开始时间
	 *  和结束时间 日期类型
	 * @param currentTime
	 * @param time
	 * @return
	 */
	public static Date getNowDateTime(Date currentTime,String time) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(currentTime);
		dateString+=time;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=null;
		try {
			date = simpleDateFormat.parse(dateString);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
	/**
	 * 获取明天日期
	 * @return
	 */
	public static Date getTomorrow(){
		Date date=null;
		try {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			System.out.println("当前日期:"+sf.format(c.getTime()));
			c.add(Calendar.DAY_OF_MONTH, 1);
			String time=sf.format(c.getTime());
			//System.out.println("增加一天后日期:"+sf.format(c.getTime()));
			//将字符串类型转换成日期类型
			SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");
			date=sf1.parse(time);
			//System.out.println("date============="+date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}
	/**
	 * 获取昨天
	 * @return
	 */
	public static Date getYesterday(){
		Date date=null;
		try {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			System.out.println("当前日期:"+sf.format(c.getTime()));
			c.add(Calendar.DAY_OF_MONTH, -1);
			String time=sf.format(c.getTime());
			//System.out.println("增加一天后日期:"+sf.format(c.getTime()));
			//将字符串类型转换成日期类型
			SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");
			date=sf1.parse(time);
			//System.out.println("date============="+date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}
	/**
	 * 获取昨天 string类型
	 * @return
	 */
	public static String  getYesterdays(){
		String dates=null;
		try {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			//System.out.println("当前日期:"+sf.format(c.getTime()));
			c.add(Calendar.DAY_OF_MONTH, -1);
			String time=sf.format(c.getTime());
			//System.out.println("增加一天后日期:"+sf.format(c.getTime()));
			dates=sf.format(c.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dates;
	}

	/**
	 * 将日期类型转换成字符串类型
	 * @param date
	 * @return
	 */
	public static String getStringDate(Date date) {
		//设置要获取到什么样的时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//获取String类型的时间
		String createdate = sdf.format(date);
		//System.out.println("createdate========="+createdate);
		return createdate;
	}

}

  //创建数据连接

package org.springblade.consumption.utils;
import org.springblade.consumption.constant.ConsumptionConstant;
import org.springblade.consumption.entity.WriteInConsumptionMachine;
import org.springblade.core.tool.utils.Func;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ConsumptionUtil {
	/**
	 * 消费机模块 连接数据库获取信息
	 * @return
	 */
	public static List<Map<String,Object>> getSqlResult(String sql,Map<String,String>map){
		List<Map<String,Object>> list = new ArrayList();
		try {
			//一开始必须填一个已经存在的数据库
			String url = ConsumptionConstant.url;
			Connection conn = DriverManager.getConnection(url, ConsumptionConstant.userName, ConsumptionConstant.password);// 连接数据库
			//Statement stat = conn.createStatement();//创建一个 Statement 对象来将 SQL 语句发送到数据库。
			PreparedStatement stat = conn.prepareStatement(sql);
			if(Func.isNotEmpty(map)){
				//餐别
				if(map.containsKey("mealType")){
					stat.setString(1, map.get("mealType"));
				}
				//消费时间
				if(map.containsKey("date")){
					stat.setDate(2,Date.valueOf(map.get("date")));
				}
				//用户号
				if(map.containsKey("personCode")){
					stat.setString(1,String.valueOf(map.get("personCode")));
				}
			}

			ResultSet resultSet=stat.executeQuery();

			ResultSetMetaData md = resultSet.getMetaData();
			int columnCount = md.getColumnCount();
			while (resultSet.next()) {
				Map<String,Object> rowData = new HashMap();
				for (int i = 1; i <= columnCount; i++) {
					rowData.put(md.getColumnName(i), resultSet.getObject(i));
				}
				list.add(rowData);
			}
			System.out.println("list====="+list);
			resultSet.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
     return list;
	}
	/**
	 * 将信息写入消费机
	 */
   public static boolean writeMsg(String sql,WriteInConsumptionMachine bean){
   	  boolean flage=true;
	   try {
		   //一开始必须填一个已经存在的数据库
		   String url = ConsumptionConstant.url;
		   Connection conn = DriverManager.getConnection(url, ConsumptionConstant.userName, ConsumptionConstant.password);// 连接数据库
		   //Statement stat = conn.createStatement();//创建一个 Statement 对象来将 SQL 语句发送到数据库。
		   PreparedStatement stat = conn.prepareStatement(sql);
		   if(Func.isNotEmpty(bean)){
			   stat.setDate(1, Date.valueOf(bean.getBookTime()));
			   stat.setDate(2,  Date.valueOf(bean.getBookDate()));
			   stat.setInt(3,bean.getUserId());
			   stat.setInt(4,bean.getMealId());
		   }
		   stat.executeUpdate();
		   conn.close();
	   } catch (Exception e) {
		   flage=false;
		   e.printStackTrace();
	   }
	   return flage;
   }
}

  

posted @ 2021-01-28 13:41  红尘沙漏  阅读(140)  评论(0编辑  收藏  举报