package com.yingpaipay.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yingpaipay.business.domain.Document;
import com.yingpaipay.business.domain.bo.ProjectListOnDashboardWorkbenchBo;
import com.yingpaipay.business.domain.vo.ProjectInDashboardWorkbenchVo;
import com.yingpaipay.business.domain.vo.TaskTodoCountVo;
import com.yingpaipay.business.enumeration.DocumentStatusEnum;
import com.yingpaipay.business.service.IDocumentQcTaskDetailService;
import com.yingpaipay.business.service.IDocumentService;
import com.yingpaipay.business.service.IProjectService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/home/dashboard")
@RequiredArgsConstructor
public class DashboardController extends BaseController {

    private final IProjectService projectService;
    private final IDocumentService documentService;
    private final IDocumentQcTaskDetailService qcTaskDetailService;

    @SaCheckPermission("home:dashboard:index")
    @GetMapping("/list")
    public TableDataInfo<ProjectInDashboardWorkbenchVo> listOnDashboardWorkbench(ProjectListOnDashboardWorkbenchBo bo, PageQuery pageQuery) {
        return projectService.queryPageListOnDashboardWorkbench(bo, pageQuery);
    }

    @SaCheckPermission("home:dashboard:index")
    @GetMapping("/countTodo")
    public R<TaskTodoCountVo> countTodo() {
        TaskTodoCountVo vo = new TaskTodoCountVo();
        List<Document> documents = documentService.listOnWorkbench();
        if (documents.isEmpty()) {
            R.ok(vo);
        }
        vo.setToSubmit(documents.stream()
            .filter(e -> e.getStatus().equals(DocumentStatusEnum.UN_UPLOAD.getValue()) || e.getStatus().equals(DocumentStatusEnum.AUDIT_REJECT.getValue()))
            .filter(e -> e.getPlanSubmitter().equals(LoginHelper.getUserId()))
            .count());
        // FIXME 必须得是自己能够审核的文件才能算进去
        vo.setToAudit(documents.stream()
            .filter(e -> e.getStatus().equals(DocumentStatusEnum.UN_AUDIT.getValue()))
            .filter(e -> e.getCreateBy().equals(LoginHelper.getUserId()))
            .count());
        vo.setToFiling(documents.stream()
            .filter(e -> e.getStatus().equals(DocumentStatusEnum.UN_ARCHIEVED.getValue()))
            .filter(e -> e.getCreateBy().equals(LoginHelper.getUserId()))
            .count());
        vo.setToQc((long) qcTaskDetailService.listOnWorkbench().size());
        return R.ok(vo);
    }

}
