package com.dcsoft.system.kq.controller;


import com.dcsoft.common.core.utils.poi.ExcelUtil;
import com.dcsoft.common.core.web.controller.BaseController;
import com.dcsoft.common.core.web.domain.AjaxResult;
import com.dcsoft.common.core.web.page.TableDataInfo;
import com.dcsoft.common.log.annotation.Log;
import com.dcsoft.common.log.enums.BusinessType;
import com.dcsoft.common.security.annotation.RequiresPermissions;
import com.dcsoft.system.kq.domain.KqHoliday;
import com.dcsoft.system.kq.service.IKqHolidayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 假期设置Controller
 *
 * @author dcsoft
 * @date 2023-01-11
 */
@RestController
@RequestMapping("/kqHoliday")
public class KqHolidayController extends BaseController
{
    @Autowired
    private IKqHolidayService kqHolidayService;

    /**
     * 查询假期设置列表
     */
    @GetMapping("/list")
    public TableDataInfo list(KqHoliday kqHoliday)
    {
       //startPage();
       /* if(kqHoliday.getSelectTimeStep()!=null){
            kqHoliday.setStartTimeStep(kqHoliday.getSelectTimeStep()-(long)2*31 * 24 * 60 * 60 * 1000);
            kqHoliday.setEndTimeStep(kqHoliday.getSelectTimeStep()+(long)2*31 * 24 * 60 * 60 * 1000);
        }*/
        List<KqHoliday> list = kqHolidayService.selectKqHolidayList(kqHoliday);
        return getDataTable(list);
    }

    /**
     * 查询假期设置列表
     */
    @GetMapping("/page")
    public TableDataInfo page(KqHoliday kqHoliday)
    {
        startPage();
       /* if(kqHoliday.getSelectTimeStep()!=null){
            kqHoliday.setStartTimeStep(kqHoliday.getSelectTimeStep()-(long)2*31 * 24 * 60 * 60 * 1000);
            kqHoliday.setEndTimeStep(kqHoliday.getSelectTimeStep()+(long)2*31 * 24 * 60 * 60 * 1000);
        }*/
        List<KqHoliday> list = kqHolidayService.selectKqHolidayList(kqHoliday);
        return getDataTable(list);
    }

    /**
     * 导出假期设置列表
     */
    @RequiresPermissions("kq:holiday:export")
    @Log(title = "假期设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KqHoliday kqHoliday)
    {
        List<KqHoliday> list = kqHolidayService.selectKqHolidayList(kqHoliday);
        ExcelUtil<KqHoliday> util = new ExcelUtil<KqHoliday>(KqHoliday.class);
        util.exportExcel(response, list, "假期设置数据");
    }

    /**
     * 获取假期设置详细信息
     */

    @RequiresPermissions("kq:holiday:query")
    @GetMapping(value = "/{holidayId}")
    public AjaxResult getInfo(@PathVariable("holidayId") Long holidayId)
    {
        return success(kqHolidayService.selectKqHolidayByHolidayId(holidayId));
    }

    /**
     * 新增假期设置
     */
    @RequiresPermissions("kq:holiday:add")
    @Log(title = "假期设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addInfo(@RequestBody KqHoliday kqHoliday)
    {
        if(kqHoliday.getTime()!=null){
            kqHoliday.setTimeStamp(kqHoliday.getTime().getTime());
        }
        return toAjax(kqHolidayService.insertKqHoliday(kqHoliday));
    }

    /**
     * 新增假期设置
     */
   // @PreAuthorize("@ss.hasPermi('kq:holiday:add')")
    @Log(title = "假期设置", businessType = BusinessType.INSERT)
    @GetMapping(value = "/{timeStamp}/{status}")
    public AjaxResult add(@PathVariable Long timeStamp,@PathVariable String status)
    {
        //查询是否存在该记录存在不管
        KqHoliday kqHolidays=kqHolidayService.selectKqHolidayByHolidayTimeStamp(timeStamp);
        if(kqHolidays==null){
        }else{
            kqHolidayService.deleteKqHolidayByHolidayTimeStamp(timeStamp);
        }
        KqHoliday kqHoliday=new KqHoliday();
        kqHoliday.setTimeStamp(timeStamp);
        Date date=new Date(timeStamp);
        kqHoliday.setTime(date);
        kqHoliday.setStatus(status);
        return toAjax(kqHolidayService.insertKqHoliday(kqHoliday));
    }

    /**
     * 修改假期设置
     */
    @RequiresPermissions("kq:holiday:edit")
    @Log(title = "假期设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KqHoliday kqHoliday)
    {
        if(kqHoliday.getTime()!=null){
            kqHoliday.setTimeStamp(kqHoliday.getTime().getTime());
        }
        return toAjax(kqHolidayService.updateKqHoliday(kqHoliday));
    }

    /**
     * 删除假期设置
     */
    @Log(title = "假期设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{timeStamp}")
    public AjaxResult remove(@PathVariable Long timeStamp)
    {
        return toAjax(kqHolidayService.deleteKqHolidayByHolidayTimeStamp(timeStamp));
    }

    /**
     * 删除假期设置
     */
    @RequiresPermissions("kq:holiday:remove")
    @Log(title = "假期设置", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{holidayIds}")
    public AjaxResult remove(@PathVariable Long[] holidayIds)
    {
        return toAjax(kqHolidayService.deleteKqHolidayByHolidayIds(holidayIds));
    }

}
