package com.bonus.sharedstation.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.sharedstation.app.domain.TherapyAppointments;
import com.bonus.sharedstation.app.dto.TherapyAppointmentsDTO;
import com.bonus.sharedstation.app.entity.MlConfigurationDate;
import com.bonus.sharedstation.app.service.ITherapyAppointmentsService;
import com.bonus.sharedstation.web.mapper.MlConfigurationDateMapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Tag(name = "app-理疗预约")
@RestController
@RequestMapping("/information/therapyappointments/app")
public class TherapyAppointmentsAppController {

    @Resource
    private ITherapyAppointmentsService service;

    /**
     * Dao
     */
    @Resource
    private MlConfigurationDateMapper dao;


    @PostMapping(value = "/add", produces = "application/json; charset=utf-8")
    @ApiOperation("理疗预约-新增")
    public AjaxResult add(@RequestBody TherapyAppointments dto) {
        return service.insert(dto);
    }


    @PostMapping(value = "/delete", produces = "application/json; charset=utf-8")
    @ApiOperation("理疗预约-删除")
    public AjaxResult deleteById(@RequestBody TherapyAppointments dto) {
        return service.delete(dto);
    }

    @ApiOperation("理疗预约-修改")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody @Valid TherapyAppointments dto) {
        return service.updateInfoById(dto);
    }


    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @ApiOperation("理疗预约-查询")
    @PostMapping("/getList")
    public AjaxResult getList(@RequestBody TherapyAppointmentsDTO dto) {
        return service.getListPage(dto);
    }


    /**
     * 详情
     *
     * @param
     * @return
     */
    @PostMapping(value = "selectById", produces = "application/json; charset=utf-8")
    @ApiOperation("理疗预约-查询详情")
    public AjaxResult selectInfoById(@RequestBody TherapyAppointments dto) {
        return service.selectInfoById(dto);
    }


    /**
     * 详情
     *
     * @param
     * @return
     */
    @PostMapping(value = "selectByDate", produces = "application/json; charset=utf-8")
    @ApiOperation("理疗预约-查询通过日期")
    public AjaxResult selectByDate(@RequestBody TherapyAppointments dto) {
        return service.selectByDate(dto);
    }

    /**
     * 详情
     *
     * @param
     * @return
     */
    @PostMapping(value = "selectDirCode", produces = "application/json; charset=utf-8")
    @ApiOperation("理疗预约-查询通过日期")
    public AjaxResult selectDirCode(@RequestBody TherapyAppointments dto) {
        return service.selectDirCode(dto);
    }
//
//    /**
//     * 查询日期 每月的 10 - 20 号 是不是节假日
//     */
//    @PostMapping(value = "selectMonthHoliday", produces = "application/json; charset=utf-8")
//    @Operation(summary = "查询配置日期")
//    @OperateLog(type = GreenLogOperateTypeEnum.QUERY)
//    public RestResult selectDirCode() {
//        List<Date> dates = new ArrayList<>();
//        List<SysHoliday> sysHolidays = attSourceDataMapper.selectHolidaysOfCurrentMonth();
//        //转化为 String
//        List<String> strings = new ArrayList<>();
//        SimpleDateFormat das = new SimpleDateFormat("yyyy-MM-dd");
//        if (sysHolidays != null) {
//            for (SysHoliday sysHoliday : sysHolidays) {
//
//                String format = das.format(sysHoliday.getDate());
//                strings.add(format);
//            }
//        }
//        // 创建一个Calendar实例
//        Calendar calendar = Calendar.getInstance();
//        Calendar calendar2 = Calendar.getInstance();
//
//
//        // 设置为当前年月的10号
//        calendar.set(Calendar.DAY_OF_MONTH, ToDoEnum.MONTH_OF_HAVE_ONE);
//        calendar2.set(Calendar.DAY_OF_MONTH, ToDoEnum.MONTH_OF_HAVE_TWO);
//
//        // 获取Date对象
//        Date day10 = calendar.getTime(); // 不需要显式声明为 final
//        Date day220 = calendar2.getTime();
//        // 检查并顺延10号和20号
//        Date shiftedDay10 = checkAndShiftHoliday(strings, day10);
//        Date shiftedDay220 = checkAndShiftHoliday(strings, day220);
//
//        String format = das.format(shiftedDay10);
//        String dsf = das.format(shiftedDay220);
//        System.err.println(format);
//        System.err.println(dsf);
//        dates.add(shiftedDay10);
//        dates.add(shiftedDay220);
//        return RestResult.success(dates);
//    }
//

    Date checkAndShiftHoliday(List<String> holidays, Date date) {

        Date currentDate = new Date(date.getTime());
        while (true) {
            SimpleDateFormat das = new SimpleDateFormat("yyyy-MM-dd");
            String format = das.format(currentDate);
            boolean isHoliday = holidays.stream()
                    .anyMatch(s -> s.equals(format));
            //如果不是节假日
            if (!isHoliday) {
                break;
            }
            currentDate = addOneDay(currentDate);
        }
        //返回
        return currentDate;
    }

    /**
     * 定义一个方法，用于在日期上加一天
     * @param date 传入的日期
     * @return 返回加一天后的日期
     */
    Date addOneDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        return calendar.getTime();
    }


    /**
     * 查询日期 每月的 10 - 20 号 是不是节假日
     */
    @PostMapping(value = "/selectMonthHoliday", produces = "application/json; charset=utf-8")
    @ApiOperation("理疗预约-查询配置日期")
    public AjaxResult dateConfig() {
        LambdaQueryWrapper<MlConfigurationDate> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        queryWrapper
                .eq(MlConfigurationDate::getIsDeleted, "0")
                .orderByDesc(MlConfigurationDate::getCreatedTime);

        List<MlConfigurationDate> returnResult = dao.selectList(queryWrapper);

        if (returnResult == null) {
            return AjaxResult.error("请联系管理员先配置理疗服务日期!");
        }
        List<Date> dates = new ArrayList<>();
        for (MlConfigurationDate mlConfigurationDate : returnResult) {
            //转化为 String
            List<String> strings = new ArrayList<>();
            SimpleDateFormat das = new SimpleDateFormat("yyyy-MM-dd");
            // 创建一个Calendar实例
            Calendar calendar = Calendar.getInstance();
            // 设置为当前年月的10号
            calendar.set(Calendar.DAY_OF_MONTH, mlConfigurationDate.getDateNum());
            // 获取Date对象
            Date day10 = calendar.getTime();
            // 检查并顺延10号和20号
            Date shiftedDay10 = checkAndShiftHoliday(strings, day10);
            String format = das.format(shiftedDay10);
            System.err.println(format);
            dates.add(shiftedDay10);
        }
        return AjaxResult.success(dates);
    }
}
