package com.qy.worksheetsystem.controller;

import com.qy.worksheetsystem.model.MessageResult;
import com.qy.worksheetsystem.model.MessageResultV2;
import com.qy.worksheetsystem.service.impl.THRATSWorkCalendarItemServiceImpl;
import com.qy.worksheetsystem.util.DateUtil;
import com.qy.worksheetsystem.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Api(value = "获取工作日历" ,tags="获取工作日历")    //将类标记为 Swagger 资源。
@RestController
@RequestMapping("/timeSheet")
public class THRATSWorkCalendarItemController {
    @Autowired
    private THRATSWorkCalendarItemServiceImpl thratsWorkCalendarItemServiceImpl;

    @ApiOperation(value = "获取工作日历")
    @GetMapping("/getWorkCalendar")
    @ApiImplicitParams({
            @ApiImplicitParam(name="token",value="token",required=true,paramType="header",dataType="String"),
            @ApiImplicitParam(name="startDate",value="开始日期",required=true,paramType="query",dataType="String"),
            @ApiImplicitParam(name="endDate",value="结束日期",required=true,paramType="query",dataType="String"),
            })
    public MessageResult selectWorkCalendar(@RequestHeader("token") String token,String startDate, String endDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String number = JWTUtil.getNumber(token);
        if(StringUtils.isEmpty(number)){
            return MessageResult.error("token异常");
        }
        if (DateUtil.isValidDate(startDate, endDate) == false) {
            return MessageResult.error("请输入正确的日期时间，日期格式例如2022-01-01");
        }

        Date start = sdf.parse(startDate);
        Date end = sdf.parse(endDate);

        if (start.getTime() > end.getTime()) {
            return MessageResult.error("开始时间不能大于结束时间");
        }

        Map<String, String> map = new HashMap<>();
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        List<Map<String, String>> selectWorkCalendar = thratsWorkCalendarItemServiceImpl.selectWorkCalendar(map);
        MessageResultV2 messageResult= MessageResultV2.success();
        messageResult.setData(selectWorkCalendar);
        return messageResult;
    }
}

