package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
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 2023-11-02
 */
@Service
public class BomanReservatServiceImpl implements IBomanReservatService 
{
    @Autowired
    private BomanReservatMapper bomanReservatMapper;

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

    /**
     * 查询陵园预约列表
     * 
     * @param bomanReservat 陵园预约
     * @return 陵园预约
     */
    @Override
    public List<BomanReservat> selectBomanReservatList(BomanReservat bomanReservat)
    {
        String dataType = bomanReservat.getDataType();
        if ("1".equals(dataType)){
            bomanReservat.setVisitDate(DateUtils.getDate());
        }
        if ("2".equals(dataType)){
            bomanReservat.setVisitDate(DateUtils.getDateByDays(-1));
        }
        return bomanReservatMapper.selectBomanReservatList(bomanReservat);
    }

    /**
     * 新增陵园预约
     * 
     * @param bomanReservat 陵园预约
     * @return 结果
     */
    @Override
    public int insertBomanReservat(BomanReservat bomanReservat)
    {
        bomanReservat.setCreateTime(DateUtils.getNowDate());
        if("2".equals(bomanReservat.getReservatType())){
            bomanReservat.setVisitType("3");
        }
        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 reservat = bomanReservatMapper.selectBomanReservatByReservatId(bomanReservat.getReservatId());

        if("3".equals(bomanReservat.getVisitType()) && "Y".equals(reservat.getIsReception()) && StringUtils.isEmpty(reservat.getReceptionName())){
            return AjaxResult.error("请先分配接待人员");
        }
        bomanReservat.setUpdateTime(DateUtils.getNowDate());
        int i = bomanReservatMapper.updateBomanReservat(bomanReservat);
        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 预约首页统计
     * @param
     * @return
     */
    @Override
    public AjaxResult indexStatistics(BomanReservat bomanReservat) {
        Map<String,Object> map = new HashMap<>(4);
        map.put("canGuan",0);
        map.put("yuYue",0);
        map.put("todayYuYue",0);
        map.put("todayCanGuan",0);
        List<BomanReservat> bomanReservats = bomanReservatMapper.selectBomanReservatList(bomanReservat);
        if (bomanReservats != null && bomanReservats.size() > 0){
            //来访数
            long canGuan = bomanReservats.stream().filter(s -> "2".equals(s.getVisitStatus())).count();
            long todayYuYue = bomanReservats.stream().filter(s -> DateUtils.getDate().equals(s.getVisitDate())).count();
            long todayCanGuan = bomanReservats.stream().filter(s -> DateUtils.getDate().equals(s.getVisitDate())).filter(s -> "2".equals(s.getVisitStatus())).count();
            map.put("canGuan",canGuan);
            map.put("yuYue",bomanReservats.size());
            map.put("todayYuYue",todayYuYue);
            map.put("todayCanGuan",todayCanGuan);
        }
        return AjaxResult.success(map);
    }
}
