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;

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

    @Resource
    private ITherapyAppointmentsService service;

    @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);
    }


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


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


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

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

    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);
            dates.add(shiftedDay10);
        }
        return AjaxResult.success(dates);
    }
}
