package com.ruoyi.system.service.impl;

import java.util.*;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.OrderFood;
import com.ruoyi.system.mapper.OrderFoodMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BomanReservatMapper;
import com.ruoyi.system.domain.BomanReservat;
import com.ruoyi.system.service.IBomanReservatService;

/**
 * 预约Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-27
 */
@Service
public class BomanReservatServiceImpl implements IBomanReservatService 
{
    @Autowired
    private BomanReservatMapper bomanReservatMapper;
    @Autowired
    private OrderFoodMapper orderFoodMapper;

    /**
     * 查询预约
     * 
     * @param reservatId 预约主键
     * @return 预约
     */
    @Override
    public BomanReservat selectBomanReservatByReservatId(Long reservatId)
    {
        return bomanReservatMapper.selectBomanReservatByReservatId(reservatId);
    }

    /**
     * 查询预约列表
     * 
     * @param bomanReservat 预约
     * @return 预约
     */
    @Override
    public List<BomanReservat> selectBomanReservatList(BomanReservat bomanReservat)
    {
        return bomanReservatMapper.selectBomanReservatList(bomanReservat);
    }

    /**
     * 新增预约
     * 
     * @param bomanReservat 预约
     * @return 结果
     */
    @Override
    public int insertBomanReservat(BomanReservat bomanReservat)
    {

        bomanReservat.setCreateTime(DateUtils.getNowDate());
        return bomanReservatMapper.insertBomanReservat(bomanReservat);
    }

    /**
     * 修改预约
     * 
     * @param bomanReservat 预约
     * @return 结果
     */
    @Override
    public int updateBomanReservat(BomanReservat bomanReservat)
    {
        bomanReservat.setUpdateTime(DateUtils.getNowDate());
        return bomanReservatMapper.updateBomanReservat(bomanReservat);
    }

    /**
     * 批量删除预约
     * 
     * @param reservatIds 需要删除的预约主键
     * @return 结果
     */
    @Override
    public int deleteBomanReservatByReservatIds(Long[] reservatIds)
    {
        return bomanReservatMapper.deleteBomanReservatByReservatIds(reservatIds);
    }

    /**
     * 删除预约信息
     * 
     * @param reservatId 预约主键
     * @return 结果
     */
    @Override
    public int deleteBomanReservatByReservatId(Long reservatId)
    {
        return bomanReservatMapper.deleteBomanReservatByReservatId(reservatId);
    }

    @Override
    public AjaxResult examine(BomanReservat bomanReservat) {
        bomanReservat.setUpdateTime(DateUtils.getNowDate());
        int i = bomanReservatMapper.updateBomanReservat(bomanReservat);
        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /***
     * pc首页统计每周预约
     * @return
     */
    @Override
    public AjaxResult weekAppointment() {
        Map<String,Object> resultMap = new HashMap<>();
        //获取上周一
        String lastMonday = DateUtils.lastMonday();
        //获取上周日
        String lastSunday = DateUtils.lastSunday();
        //获取本周周一
        String monday = DateUtils.monday();
        //获取当前日期
        String day = DateUtils.getDate();
        //获取本周一到周日的时间
        List<String> strings = DateUtils.weekList();

        //预约人数
        int yy = 0;
        BomanReservat bomanReservat = new BomanReservat();
        bomanReservat.setStartTime(monday);
        bomanReservat.setEndTime(day);
        List<BomanReservat> lastList = bomanReservatMapper.selectBomanReservatTimeList(bomanReservat);
        //访客人数
        int fk = 0;
        if(lastList!=null && lastList.size()>0){
            yy = lastList.size();
        }
        //周一到周日的预约人数
        List<String> yyList = new ArrayList<>();
        //周一到周日的到访人数
        List<String> fkList = new ArrayList<>();

        for (String string : strings) {
            //周几的预约人数
            int weekyy = 0;
            //周几的访客人数
            int weekfk = 0;
            for (BomanReservat reservat : lastList) {
                if(string.equals(DateUtils.dateTime(reservat.getVisitDate()))){
                    weekyy++;
                    if("2".equals(reservat.getVisitStatus())){
                        weekfk++;
                        fk++;
                    }
                }
            }
            yyList.add(String.valueOf(weekyy));
            fkList.add(String.valueOf(weekfk));
        }
        resultMap.put("yyzs",yy);
        resultMap.put("fkzs",fk);
        resultMap.put("x",strings);
        resultMap.put("y1",yyList);
        resultMap.put("y2",fkList);
        return AjaxResult.success(resultMap);
    }

    @Override
    public AjaxResult yydc() {
        Map<String, Object> resultMap = new HashMap<>();
        //获取当前日期
        String day = DateUtils.getDate();

        BomanReservat bomanReservat = new BomanReservat();
        bomanReservat.setVisitTime(day);
        List<BomanReservat> lastList = bomanReservatMapper.selectBomanReservatTimeList(bomanReservat);
        //本月来访数
        int bylf = 0;
        //本月预约数
        int byyy = 0;
        //今日来访
        int jrlf = 0;
        //今日预约
        int jryy = 0;
        if(lastList!=null && lastList.size()>0){
            byyy = lastList.size();
            for (BomanReservat reservat : lastList) {
                if("2".equals(reservat.getVisitStatus())){
                    bylf++;
                    if(day.contains(DateUtils.dateTime(reservat.getVisitDate()))){
                        jrlf++;
                    }
                }
                if(day.contains(DateUtils.dateTime(reservat.getVisitDate()))){
                    jryy++;
                }
            }
        }

        //订餐统计
        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("bylf",bylf);
        resultMap.put("byyy",byyy);
        resultMap.put("jrlf",jrlf);
        resultMap.put("jryy",jryy);
        resultMap.put("dczs",dczs);
        resultMap.put("dcwc",dcwc);
        resultMap.put("dcDinner",dcDinner);
        return AjaxResult.success(resultMap);
    }

}
