package com.ygtx.worklog.controller;

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

import com.ygtx.worklog.domain.DailyReport;
import com.ygtx.worklog.service.IDailyReportService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.format.annotation.DateTimeFormat;
import com.ygtx.common.annotation.Log;
import com.ygtx.common.core.controller.BaseController;
import com.ygtx.common.core.domain.AjaxResult;
import com.ygtx.common.enums.BusinessType;
import com.ygtx.common.utils.poi.ExcelUtil;
import com.ygtx.common.core.page.TableDataInfo;

import static com.ygtx.common.utils.PageUtils.startPage;

/**
 * 日报Controller
 *
 * @author ruoyi
 * @date 2025-10-21
 */
@RestController
@RequestMapping("/worklog/daily")
public class DailyReportController extends BaseController
{
    @Autowired
    private IDailyReportService dailyReportService;

    /**
     * 查询日报列表
     */
    @PreAuthorize("@ss.hasPermi('worklog:daily:list')")
    @GetMapping("/list")
    public TableDataInfo list(DailyReport dailyReport)
    {
        startPage();
        List<DailyReport> list = dailyReportService.selectDailyReportList(dailyReport);
        return getDataTable(list);
    }

    /**
     * 导出日报列表
     */
    @PreAuthorize("@ss.hasPermi('worklog:daily:export')")
    @Log(title = "日报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DailyReport dailyReport)
    {
        List<DailyReport> list = dailyReportService.selectDailyReportList(dailyReport);
        ExcelUtil<DailyReport> util = new ExcelUtil<DailyReport>(DailyReport.class);
        util.exportExcel(response, list, "日报数据");
    }

    /**
     * 获取日报详细信息
     */
    @PreAuthorize("@ss.hasPermi('worklog:daily:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dailyReportService.selectDailyReportById(id));
    }

    /**
     * 新增日报
     */
    @PreAuthorize("@ss.hasPermi('worklog:daily:add')")
    @Log(title = "日报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DailyReport dailyReport)
    {
        return toAjax(dailyReportService.insertDailyReport(dailyReport));
    }

    /**
     * 修改日报
     */
    @PreAuthorize("@ss.hasPermi('worklog:daily:edit')")
    @Log(title = "日报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DailyReport dailyReport)
    {
        return toAjax(dailyReportService.updateDailyReport(dailyReport));
    }

    /**
     * 删除日报
     */
    @PreAuthorize("@ss.hasPermi('worklog:daily:remove')")
    @Log(title = "日报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dailyReportService.deleteDailyReportByIds(ids));
    }

    /**
     * 发布日报
     */
    @PreAuthorize("@ss.hasPermi('worklog:daily:publish')")
    @Log(title = "发布日报", businessType = BusinessType.UPDATE)
    @PutMapping("/publish/{id}")
    public AjaxResult publish(@PathVariable Long id)
    {
        return toAjax(dailyReportService.publishDailyReport(id));
    }

    /**
     * 创建新日报
     */
    @PreAuthorize("@ss.hasPermi('worklog:daily:add')")
    @GetMapping("/create")
    public AjaxResult create()
    {
        Long userId = getUserId();
        DailyReport dailyReport = dailyReportService.createDailyReport(userId, new Date());
        return success(dailyReport);
    }

    /**
     * 补交日报
     */
    @PreAuthorize("@ss.hasPermi('worklog:daily:makeup')")
    @Log(title = "补交日报", businessType = BusinessType.INSERT)
    @PostMapping("/makeup")
    public AjaxResult makeup(@RequestBody DailyReport dailyReport)
    {
        return toAjax(dailyReportService.makeupDailyReport(dailyReport));
    }

    /**
     * 查询授权日报列表
     */
    @PreAuthorize("@ss.hasPermi('worklog:authreport:list')")
    @GetMapping("/auth/list")
    public TableDataInfo authList(DailyReport dailyReport)
    {
        startPage();
        List<DailyReport> list = dailyReportService.selectAuthDailyReportList(dailyReport);
        return getDataTable(list);
    }
}
