package com.ruoyi.oa.controller;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.oa.domain.bo.SysOaWarehouseDetailBo;
import com.ruoyi.oa.domain.dto.ProjectActivityDTO;
import com.ruoyi.oa.domain.dto.ProjectDataDTO;
import com.ruoyi.oa.domain.vo.DashboardChartsVo;
import com.ruoyi.oa.domain.vo.DashboardMetricsVo;
import com.ruoyi.oa.domain.vo.SysOaOutWarehouseListVo;
import com.ruoyi.oa.mapper.SysOaTaskMapper;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.vo.SysOaProjectVo;
import com.ruoyi.oa.domain.bo.SysOaProjectBo;
import com.ruoyi.oa.service.ISysOaProjectService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目管理
 *
 * @author huangxing
 * @date 2024-01-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/project")
public class SysOaProjectController extends BaseController {

    private final ISysOaProjectService iSysOaProjectService;

    private final SysOaTaskMapper sysOaTaskMapper;




    /**
     * 查询项目管理列表
     */
    @GetMapping("/list")
    public TableDataInfo<SysOaProjectVo> list(SysOaProjectBo bo, PageQuery pageQuery) {
        return iSysOaProjectService.queryPageList(bo, pageQuery);
    }


    /**
     * 查询项目管理列表
     */
    @GetMapping("/ware-list")
    public TableDataInfo<SysOaProjectVo> listWareProject(SysOaProjectBo bo, PageQuery pageQuery) {
        return iSysOaProjectService.listWareProject(bo, pageQuery);
    }

    /**
     * 导出项目管理列表
     */
    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysOaProjectBo bo, HttpServletResponse response) {
        List<SysOaProjectVo> list = iSysOaProjectService.queryList(bo);
        ExcelUtil.exportExcel(list, "项目管理", SysOaProjectVo.class, response);
    }

    /**
     * 获取项目管理详细信息
     *
     * @param projectId 主键
     */
    @GetMapping("/{projectId}")
    public R<SysOaProjectVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long projectId) {
        return R.ok(iSysOaProjectService.queryById(projectId));
    }

    /**
     * 获取项目管理详细信息
     *
     */
    @GetMapping("/projectDataByMonth/{date}")
    public R<List<SysOaProjectVo>> getProjectDataByMonth(@PathVariable("date") Date date) {
        return R.ok(iSysOaProjectService.getProjectDataByMonth(date));
    }



    /**
     * 获取项目管理详细信息
     *
     */
    @GetMapping("/projectDataByMonthAndDate")
    public R<List<SysOaProjectVo>> getProjectDataByMonthAndDate() {
        return R.ok(iSysOaProjectService.getProjectDataByMonthAndDate());
    }


    /**
     * 新增项目管理
     */
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysOaProjectBo bo) {
        return toAjax(iSysOaProjectService.insertByBo(bo));
    }

    /**
     * 修改项目管理
     */
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysOaProjectBo bo) {
        return toAjax(iSysOaProjectService.updateByBo(bo));
    }

    /**
     * 删除项目管理,同时删除任务数据
     * 该方法在列表中单条删除，多条删除也可以但是没有测试
     *
     * @param projectIds 主键串
     */
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @DSTransactional
    @DeleteMapping("/{projectIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] projectIds) {
        Arrays.asList(projectIds).forEach(sysOaTaskMapper::deleteSysOaTaskByProjectId);
        return toAjax(iSysOaProjectService.deleteWithValidByIds(Arrays.asList(projectIds), true));
    }

    /**
     * 查询项目是否存在出库记录
     */
    @GetMapping("/outWare/list")
    public TableDataInfo<SysOaOutWarehouseListVo> outWareList(SysOaWarehouseDetailBo bo, PageQuery pageQuery) {
        return iSysOaProjectService.queryOutWarePageList(bo, pageQuery);
    }


    @GetMapping("/files")
    public TableDataInfo<SysOaProjectVo> files(SysOaWarehouseDetailBo bo, PageQuery pageQuery) {
        return iSysOaProjectService.queryFiles(bo, pageQuery);
    }


    /**
     * 查询进度管理列表
     */
    @GetMapping("/progress-list")
    public TableDataInfo<SysOaProjectVo> progressList(SysOaProjectBo bo, PageQuery pageQuery) {
        bo.setTradeType(1L);
        return iSysOaProjectService.listProjects(bo, pageQuery);
    }


    /**
     * 数据报表部分
     */





    /**
     * GET /api/projects/foreign/dashboard/metrics
     * @param start yyyy/MM/dd
     * @param end   yyyy/MM/dd
     */
    @GetMapping("/metrics")
    public R<DashboardMetricsVo> metrics(
            @RequestParam @DateTimeFormat(pattern = "yyyy/MM/dd") Date start,
            @RequestParam @DateTimeFormat(pattern = "yyyy/MM/dd") Date end,
            @RequestParam(value="tradeType" , required = false) Long  tradeType
    ) {
        DashboardMetricsVo vo = iSysOaProjectService.getMetrics(start, end,tradeType);
        return R.ok(vo);
    }

    /**
     * GET /projects/foreign/dashboard/charts
     * @param start yyyy/MM/dd
     * @param end   yyyy/MM/dd
     */
    @GetMapping("/charts")
    public R<DashboardChartsVo> charts(
            @RequestParam @DateTimeFormat(pattern = "yyyy/MM/dd") Date start,
            @RequestParam @DateTimeFormat(pattern = "yyyy/MM/dd") Date end,
            @RequestParam(value="tradeType" , required = false) Long  tradeType
    ) {
        DashboardChartsVo vo = iSysOaProjectService.getCharts(start, end,tradeType);
        return R.ok(vo);
    }

    /**
     * GET /projects/foreign/expiring
     * @param days 预警天数，默认 7
     */
    @GetMapping("/expiring")
    public R<List<SysOaProjectVo>> expiring(
            @RequestParam(name = "days", defaultValue = "7") Integer days,
            @RequestParam(value="tradeType" , required = false) Long  tradeType
    ) {
        List<SysOaProjectVo> list = iSysOaProjectService.getExpiringForeignProjects(days,tradeType);
        return R.ok(list);
    }

    @GetMapping("/projectData")
    public R<List<ProjectDataDTO>> projectData(
            @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date
    ) {
        String month = date.format(DateTimeFormatter.ofPattern("yyyy-MM"));
        return R.ok(iSysOaProjectService.getProjectData(month));
    }

}