报餐统计

/*
 *      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 startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<ConsumptionMachine> getMealComparison(String mealType, String startDate,String endDate) {
		Date startDate1=CMDateTimeUtil.getDateFromString(startDate+" 00:00:00");
		Date endDate1= CMDateTimeUtil.getDateFromString(endDate+" 23:59:59");
		log.info("startDate1========"+startDate1+"----endDate1");
		//根据时间和餐别查询 报餐表
		List<ConsumptionMeal>mealList=consumptionMachineMapper.getMealComparisonByStartEnd(mealType,startDate1,endDate1);
		log.info("mealList========"+mealList);
		log.info("mealList.size====="+mealList.size());
		//根据时间和餐别查询 消费机信息表
		ConsumptionMachine bean =new ConsumptionMachine();
		bean.setTenantId(ConsumptionConstant.TENANT_ID);
		bean.setMealType(mealType);
		//bean.setPosoptime(posoptime);
		//将日期类型转换成字符 获取年月日
		bean.setStartDate(startDate1);
		bean.setEndDate(endDate1);
		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 startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<ConsumptionMachine> getConsumptionComparison(String mealType,String startDate,String endDate) {
		Date startDate1=CMDateTimeUtil.getDateFromString(startDate+" 00:00:00");
		Date endDate1= CMDateTimeUtil.getDateFromString(endDate+" 23:59:59");
		log.info("刷卡对比 查看谁没有报餐刷卡了startDate1========"+startDate1+"----endDate1");
		//根据时间和餐别查询 报餐表
		List<ConsumptionMeal>mealList=consumptionMachineMapper.getMealComparisonByStartEnd(mealType,startDate1,endDate1);
		//根据时间和餐别查询 消费机信息表
		ConsumptionMachine bean =new ConsumptionMachine();
		bean.setTenantId(ConsumptionConstant.TENANT_ID);
		bean.setMealType(mealType);
		//bean.setPosoptime(posoptime);
		//将日期类型转换成字符 获取年月日
		bean.setStartDate(startDate1);
		bean.setEndDate(endDate1);
		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 startDate
	 * @param endDate
	 */
	@Override
	public List<ExcelConsumptionMachine> getSynthesizeComparison(String mealType,String startDate,String endDate) {
		Date startDate1=CMDateTimeUtil.getDateFromString(startDate+" 00:00:00");
		Date endDate1= CMDateTimeUtil.getDateFromString(endDate+" 23:59:59");
		log.info("报餐刷卡综合对比mealType====="+mealType+"-----startDate1========"+startDate1+"----endDate1===="+endDate1);
		//根据日期餐别查询报餐信息,如果没有餐别查询一日三餐
		List<ConsumptionMeal>mealList=consumptionMachineMapper.getMealComparisonByStartEnd(mealType,startDate1,endDate1);
		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(startDate1);
		bean.setEndDate(endDate1);
		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){
					//就餐日期
					String mealDate=CMDateTimeUtil.getStringDateNoTime(machine1.getMealDate());
					//刷卡日期
					String posoptime=CMDateTimeUtil.getStringDateNoTime(machine2.getPosoptime());
					log.info("mealDate======"+mealDate+"----posoptime===="+posoptime);
					//如果报餐人工号和消费工号相同,该用户报餐了也刷卡吃饭了
					if(machine1.getAccount().equals(machine2.getAccount())&& machine1.getMealType().equals(machine2.getMealType()) && mealDate.equals(posoptime)){
						//正常
						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;
	}


}

  

posted @ 2021-02-04 11:37  红尘沙漏  阅读(95)  评论(0编辑  收藏  举报