package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.Utils.TokenUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.thmz.Holidays;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.service.thmz.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Slf4j
@RestController
public class HolidaysController {
    @Autowired
    private HolidaysService holidaysService;
    /**
     * 保存节假日
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/saveHolidays", method = {RequestMethod.POST})
    public Map<String, Object> saveHolidays(@RequestBody Holidays holidays, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (holidays == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "保存节假日失败,参数为空");
                return resultMap;
            }
            if (holidays.getBeginDate() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "保存节假日失败,开始时间为空");
                return resultMap;
            }
            if (StringUtils.isBlank(holidays.getBeginAmpm())) {
                resultMap.put("code", -1);
                resultMap.put("message", "保存节假日失败,开始日期的时间段为空");
                return resultMap;
            }
            if (holidays.getEndDate() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "保存节假日失败,结束时间为空");
                return resultMap;
            }
            if (StringUtils.isBlank(holidays.getBeginAmpm())) {
                resultMap.put("code", -1);
                resultMap.put("message", "保存节假日失败,结束日期的时间段为空");
                return resultMap;
            }
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            resultMap.put("code", 0);
            resultMap.put("message", "保存节假日成功");
            holidays.setCreateId(tokenUser.getId());
            holidays.setCreateDate(new Date());
            holidaysService.saveHolidays(holidays);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("保存节假日失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "保存节假日失败,错误原因【"+e.getMessage()+"】");
            return resultMap;
        }
    }


    /**
     * 根据id查询节假日
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getHolidaysById", method = {RequestMethod.GET})
    public Map<String, Object> getHolidaysById(@RequestParam("id") Long id) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (id == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询节假日失败,参数为空");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询节假日成功");
            Holidays holidays = holidaysService.queryById(id);
            resultMap.put("data", holidays);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据id查询节假日失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "根据id查询节假日失败");
            return resultMap;
        }
    }


    /**
     *
     * 删除节假日
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/removeHolidaysById", method = {RequestMethod.GET})
    public Map<String, Object> removeHolidaysById(@RequestParam("id") Long id) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (id == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "删除节假日失败,参数为空");
                return resultMap;
            }
            int num = holidaysService.removeHolidays(id);
            if (num == 1) {
                resultMap.put("code", 0);
                resultMap.put("message", "删除节假日成功");
                return resultMap;
            }
            resultMap.put("code", -1);
            resultMap.put("message", "删除节假日失败");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除节假日失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "删除节假日失败");
            return resultMap;
        }
    }


    /**
     * 查询节假日排班
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getHolidays", method = {RequestMethod.POST})
    public Map<String, Object> getHolidays(@RequestBody Holidays holidays) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (holidays == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询节假日失败,参数为空");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询节假日成功");
            Integer count = holidaysService.queryCountHolidays(holidays);
            List<Holidays> holidaysList = holidaysService.queryHolidaysWithPage(holidays);
            resultMap.put("data", holidaysList);
            resultMap.put("count", count);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("节假日失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "节假日失败");
            return resultMap;
        }
    }

}
