package com.ygtx.worklog.controller;

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

import com.ygtx.worklog.domain.WeeklyReport;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.worklog.service.IWeeklyReportService;
import com.ygtx.common.utils.poi.ExcelUtil;
import com.ygtx.common.core.page.TableDataInfo;

/**
 * 周报Controller
 *
 * @author ruoyi
 * @date 2025-10-21
 */
@RestController
@RequestMapping("/worklog/weekly")
public class WeeklyReportController extends BaseController
{
    @Autowired
    private IWeeklyReportService weeklyReportService;

    /**
     * 查询周报列表
     */
    @PreAuthorize("@ss.hasPermi('worklog:weekly:list')")
    @GetMapping("/list")
    public TableDataInfo list(WeeklyReport weeklyReport)
    {
        startPage();
        List<WeeklyReport> list = weeklyReportService.selectWeeklyReportList(weeklyReport);
        return getDataTable(list);
    }

    /**
     * 导出周报列表
     */
    @PreAuthorize("@ss.hasPermi('worklog:weekly:export')")
    @Log(title = "周报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WeeklyReport weeklyReport)
    {
        List<WeeklyReport> list = weeklyReportService.selectWeeklyReportList(weeklyReport);
        ExcelUtil<WeeklyReport> util = new ExcelUtil<WeeklyReport>(WeeklyReport.class);
        util.exportExcel(response, list, "周报数据");
    }

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

    /**
     * 新增周报（已废弃，改为基于日报自动生成）
     */
    @Deprecated
    @PreAuthorize("@ss.hasPermi('worklog:weekly:add')")
    @Log(title = "周报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WeeklyReport weeklyReport)
    {
        return error("周报已改为基于日报自动生成，请使用生成周报接口");
    }

    /**
     * 修改周报（手动编辑）
     */
    @PreAuthorize("@ss.hasPermi('worklog:weekly:edit')")
    @Log(title = "周报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WeeklyReport weeklyReport)
    {
        return toAjax(weeklyReportService.editWeeklyReport(weeklyReport));
    }

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

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

    /**
     * 生成周报（基于本周日报自动生成）
     */
    @PreAuthorize("@ss.hasPermi('worklog:weekly:generate')")
    @Log(title = "生成周报", businessType = BusinessType.OTHER)
    @PostMapping("/generate")
    public AjaxResult generateWeekly(@RequestParam(required = false) Integer year,
                                      @RequestParam(required = false) Integer weekNum)
    {
        Long userId = getUserId();

        // 如果未指定年份和周数，使用当前周
        if (year == null || weekNum == null) {
            Calendar cal = Calendar.getInstance();
            year = cal.get(Calendar.YEAR);
            weekNum = cal.get(Calendar.WEEK_OF_YEAR);
        }

        WeeklyReport weeklyReport = weeklyReportService.generateWeeklyReport(userId, year, weekNum);
        return success(weeklyReport);
    }
}
