package com.ruoyi.web.controller.common;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.BomanReservat;
import com.ruoyi.system.domain.BomanReservatConfigTime;
import com.ruoyi.system.service.IBomanNewsService;
import com.ruoyi.system.service.IBomanReservatConfigTimeService;
import com.ruoyi.system.service.IBomanReservatService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: tjf
 * @Date: 2023/11/2 17:10
 * @Describe:
 */
@RestController
@RequestMapping("/boman/common")
public class BomanCommonController extends BaseController {

    @Autowired
    private IBomanReservatConfigTimeService bomanReservatConfigTimeService;
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private IBomanReservatService bomanReservatService;

    @Autowired
    private IBomanNewsService bomanNewsService;

    /**
     * H5时段信息 查询预约时段配置列表
     */
    @PostMapping("/reservatConfig/list")
    public TableDataInfo list(@RequestBody BomanReservatConfigTime bomanReservatConfigTime)
    {
        //根据日期查询所有时段是否可约
        List<BomanReservatConfigTime> list = bomanReservatConfigTimeService.selectBomanReservatConfigTimeList(bomanReservatConfigTime);
        if (list != null && list.size() > 0){
            for (BomanReservatConfigTime reservatConfigTime : list) {
                //查询所有时段和最大人数
                //去redis找对应日期,对应时段id的预约数量 reservat_num:2023-10-10_1
                String reservatConfigDate = bomanReservatConfigTime.getReservatConfigDate();
                //已经预约人数
                Object  reservatNum= redisCache.getCacheObject(Constants.RESERVAT_NUM + reservatConfigDate + "_" + reservatConfigTime.getReservatConfigTimeId());
                if (ObjectUtils.isNotEmpty(reservatNum)){
                    //判断人数是否大于可预约人数
                    //如果 x==y 返回0；x<y 返回负数(-1)；x>y 返回正数(1)
                   if (NumberUtils.compare(reservatConfigTime.getReservatConfigNum(),(long)reservatNum) < 1){
                       reservatConfigTime.setReservatConfigStatus("Y");
                   }
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 预约首页统计
     */
    @PostMapping("/indexStatistics")
    public AjaxResult indexStatistics(BomanReservat bomanReservat) {
        return  bomanReservatService.indexStatistics(bomanReservat);
    }


    /**
     * H5时段信息 日历
     */
    @PostMapping("/reservatConfig/calendar")
    public AjaxResult calendar(@RequestBody BomanReservatConfigTime bomanReservatConfigTime)
    {

        //signList: [{date: "2023-11-10",title: "可预约",val:'0',info: "张三生日"},
        //           {date: "2023-11-11",title: "不可约",val:'1'},
        //           {date: "2023-11-12",title: "已约满",val:'2'},
        //           {date: "2023-11-13",title: "已结束",val:'3'},
        //           {date: "2023-11-14",title: "可预约",val:'0'},
        //           {date: "2023-11-29",title: "可预约",val:'0'}],

        AjaxResult calendar = bomanReservatConfigTimeService.getCalendar(bomanReservatConfigTime.getReservatConfigDate());
        return calendar;
    }


    /**
     * 发布首页统计
     */
    @PostMapping("/indexStatisticsInFor")
    public AjaxResult indexStatisticsInFor() {
        return  bomanNewsService.indexStatisticsInFor();
    }
}
