/*
 * 文 件 名:  DateAttrConrtoller
 * 版    权:  华设设计集团股份有限公司
 * 描    述:  <描述>
 * 修 改 人:  lvwenbin
 * 修改时间:  2025/5/9
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.huashe.common.data.controller;

import com.huashe.common.data.config.ApiAuth;
import com.huashe.common.data.model.CallResponse;
import com.huashe.common.data.service.DateService;
import com.huashe.common.domain.model.DateAttr;
import com.huashe.common.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 日期属性服务
 * <功能详细描述>
 *
 * @author lvwenbin
 * @version [版本号, 2025/5/9]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@RestController
@CrossOrigin(allowedHeaders = "*", allowCredentials = "false")
@RequestMapping("/date")
@Api(value = "DateAttrConrtoller", description = "日期属性接口")
public class DateAttrConrtoller {
    @Resource
    private DateService dateService;

    /**
     * 获取日期属性
     */
    @ApiAuth
    @RequestMapping(value = "/getAttr", method = RequestMethod.GET)
    @ApiOperation(value = "/getAttr", notes = "获取日期属性")
    @ApiResponses({ @ApiResponse(code = 200, message = "success"),
        @ApiResponse(code = 400, message = "{code:****,message:'fail'}")
    })
    public CallResponse<DateAttr> getAttr(@RequestParam(value = "date") String date) {
        CallResponse<DateAttr> callResponse = new CallResponse<>();

        try {
            DateAttr dateAttr = dateService.getDateAttr(date);
            callResponse.setData(dateAttr);
            callResponse.setCode(0);
            callResponse.setMessage("success");
        }
        catch (Exception e) {
            throw new BusinessException(-1, e.getMessage());
        }

        return callResponse;
    }

    /**
     * 获取日期属性
     */
    @ApiAuth
    @RequestMapping(value = "/getAttrs", method = RequestMethod.GET)
    @ApiOperation(value = "/getAttrs", notes = "获取日期属性")
    @ApiResponses({ @ApiResponse(code = 200, message = "success"),
        @ApiResponse(code = 400, message = "{code:****,message:'fail'}")
    })
    public CallResponse<List<DateAttr>> getAttrs(@RequestParam(value = "startDate") String startDate,
        @RequestParam(value = "endDate") String endDate) {
        CallResponse<List<DateAttr>> callResponse = new CallResponse<>();

        try {
            List<DateAttr> dateAttrs = dateService.getDateAttr(startDate, endDate);
            callResponse.setData(dateAttrs);
            callResponse.setCode(0);
            callResponse.setMessage("success");
        }
        catch (Exception e) {
            throw new BusinessException(-1, e.getMessage());
        }

        return callResponse;
    }

    /**
     * 节假日采集触发器
     */
    @RequestMapping(value = "/triggerColHoliday", method = RequestMethod.GET)
    @ApiOperation(value = "/triggerColHoliday", notes = "触发-节假日采集")
    @ApiResponses({ @ApiResponse(code = 200, message = "success"),
        @ApiResponse(code = 400, message = "{code:****,message:'fail'}")
    })
    public CallResponse<Void> triggerColHoliday(@RequestParam(value = "year") String year) {
        CallResponse<Void> callResponse = new CallResponse<>();
        try {
            dateService.collectHoliday(year);
            callResponse.setCode(0);
            callResponse.setMessage("success");
        }
        catch (Exception e) {
            throw new BusinessException(-1, e.getMessage());
        }

        return callResponse;
    }
}