package com.ruoyi.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ClockUserInfo;
import com.ruoyi.system.domain.OrderFood;
import com.ruoyi.system.mapper.ClockUserInfoMapper;
import com.ruoyi.system.mapper.OrderFoodMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ClockRecordMapper;
import com.ruoyi.system.domain.ClockRecord;
import com.ruoyi.system.service.IClockRecordService;

/**
 * 打卡记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-06
 */
@Service
public class ClockRecordServiceImpl implements IClockRecordService 
{
    @Autowired
    private ClockRecordMapper clockRecordMapper;
    @Autowired
    private ClockUserInfoMapper clockUserInfoMapper;
    @Autowired
    private OrderFoodMapper orderFoodMapper;

    /**
     * 查询打卡记录
     * 
     * @param clockId 打卡记录主键
     * @return 打卡记录
     */
    @Override
    public ClockRecord selectClockRecordByClockId(Long clockId)
    {
        return clockRecordMapper.selectClockRecordByClockId(clockId);
    }

    /**
     * 查询打卡记录列表
     * 
     * @param clockRecord 打卡记录
     * @return 打卡记录
     */
    @Override
    public List<ClockRecord> selectClockRecordList(ClockRecord clockRecord)
    {
        return clockRecordMapper.selectClockRecordList(clockRecord);
    }

    /**
     * 新增打卡记录
     * 
     * @param clockRecord 打卡记录
     * @return 结果
     */
    @Override
    public int insertClockRecord(ClockRecord clockRecord)
    {
        clockRecord.setCreateTime(DateUtils.getNowDate());
        return clockRecordMapper.insertClockRecord(clockRecord);
    }

    /**
     * 修改打卡记录
     * 
     * @param clockRecord 打卡记录
     * @return 结果
     */
    @Override
    public int updateClockRecord(ClockRecord clockRecord)
    {
        clockRecord.setUpdateTime(DateUtils.getNowDate());
        return clockRecordMapper.updateClockRecord(clockRecord);
    }

    /**
     * 批量删除打卡记录
     * 
     * @param clockIds 需要删除的打卡记录主键
     * @return 结果
     */
    @Override
    public int deleteClockRecordByClockIds(Long[] clockIds)
    {
        return clockRecordMapper.deleteClockRecordByClockIds(clockIds);
    }

    /**
     * 删除打卡记录信息
     * 
     * @param clockId 打卡记录主键
     * @return 结果
     */
    @Override
    public int deleteClockRecordByClockId(Long clockId)
    {
        return clockRecordMapper.deleteClockRecordByClockId(clockId);
    }

    @Override
    public AjaxResult selectClockRecordListRl(ClockRecord clockRecord) {
        Map<String, Object> resultMap = new HashMap<>();
        //查询人员数据
        ClockUserInfo clockUserInfo = clockUserInfoMapper.selectClockUserInfoByUserId(clockRecord.getUserId());
        resultMap.put("name",clockUserInfo.getUserName());
        //查询最新打卡数据
        ClockRecord clockRecordNew = clockRecordMapper.selectClockRecordNew(clockRecord);
        resultMap.put("time",clockRecordNew.getRecordTime());
        //todo 上下班时间
        resultMap.put("workTime","");
        //查询查询月打卡数据
        List<ClockRecord> clockRecords = clockRecordMapper.selectOrderFoodListRl(clockRecord);
        //按日期分类
        Map<String, List<ClockRecord>> clockTime = new HashMap<>();
        if (clockRecords != null && clockRecords.size() > 0){
            clockTime = clockRecords.stream().collect(Collectors.groupingBy(ClockRecord::getRecordTime));
        }
        //查询传入月所有日期
        List<String> dayList = DateUtils.getDay(DateUtils.parseDate(clockRecord.getRecordTime()));
        //写入数据
        List<Map> mapList = new ArrayList<>();
        if (dayList.size()> 0){
            for (String day : dayList) {
                Map<String,Object> map = new HashMap<>(5);
                map.put("date",day);
                map.put("am","未打卡");
                //上午打卡标识 1：打卡，2：未打卡
                map.put("amex","2");
                map.put("pm","未打卡");
                //下午打卡标识 1：打卡，2：未打卡
                map.put("pmex","2");
                if (clockTime.size() > 0){
                    List<ClockRecord> clockRecordList = clockTime.get(day);
                    if (clockRecordList != null && clockRecordList.size() > 0){
                        for (ClockRecord clock : clockRecordList) {
                            if(StringUtils.isNotEmpty(clock.getWorkingTime())){
                                map.put("am",clock.getWorkingTime());
                                //上午打卡表示 1：打卡，2：未打卡
                                map.put("amex","1");
                            }
                            if(StringUtils.isNotEmpty(clock.getDutyTime())){
                                map.put("pm",clock.getDutyTime());
                                //下午打卡表示 1：打卡，2：未打卡
                                map.put("pmex","1");
                            }
                        }
                    }
                }
                mapList.add(map);
            }
        }
        //日历数据
        resultMap.put("rlData",mapList);
        return AjaxResult.success(resultMap);
    }

    @Override
    public AjaxResult kqdc() {
        Map<String, Object> resultMap = new HashMap<>();
        //获取当前日期
        String day = DateUtils.getDate();
        //查询当天应打卡人数
        int dkzs = 0;
        ClockRecord clockRecord = new ClockRecord();
        clockRecord.setRecordTime(day);
        List<ClockRecord> clockRecords = clockRecordMapper.selectClockRecordList(clockRecord);
        //已打卡人数
        int ydk = 0;
        //未打卡人数
        int wdk = 0;
        if(clockRecords!=null && clockRecords.size()>0){
            dkzs = clockRecords.size();
            for (ClockRecord record : clockRecords) {
                if(StringUtils.isNotEmpty(record.getWorkingTime()) || StringUtils.isNotEmpty(record.getDutyTime())){
                    ydk++;
                }else{
                    wdk++;
                }
            }
        }
        //订餐统计
        OrderFood orderFood = new OrderFood();
        orderFood.setOrderFoodTime(DateUtils.parseDate(day));
        List<OrderFood> orderFoods = orderFoodMapper.selectOrderFoodListR(orderFood);
        //订餐总人数
        int dczs = 0;
        //订餐午餐人数
        int dcwc = 0;
        //订餐晚餐人数
        int dcDinner = 0;
        if(orderFoods!=null && orderFoods.size()>0){
            dczs = orderFoods.size();
            for (OrderFood food : orderFoods) {
                if("1".equals(food.getOrderFoodType())){
                    dcwc++;
                }
                if("2".equals(food.getOrderFoodType())){
                    dcDinner++;
                }
            }
        }
        resultMap.put("dkzs",dkzs);
        resultMap.put("ydk",ydk);
        resultMap.put("wdk",wdk);
        resultMap.put("dczs",dczs);
        resultMap.put("dcwc",dcwc);
        resultMap.put("dcDinner",dcDinner);
        return AjaxResult.success(resultMap);
    }
}
