package com.yunzhupaas.portal.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.yunzhupaas.base.ActionResult;
import com.yunzhupaas.base.vo.ListVO;
import com.yunzhupaas.flowable.model.operator.OperatorVo;
import com.yunzhupaas.flowable.model.task.TaskPagination;
import com.yunzhupaas.message.model.NoticeModel;
import com.yunzhupaas.message.model.message.NoticeVO;
import com.yunzhupaas.message.service.MessageService;
import com.yunzhupaas.portal.model.*;
import com.yunzhupaas.service.EmailReceiveService;
import com.yunzhupaas.util.JsonUtil;
import com.yunzhupaas.workflow.service.TaskApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 主页控制器
 *
 * @author 云筑产品开发平台组
 * @version V3.1.0
 * @copyright 深圳市乐程软件有限公司
 * @date 2023/09/27
 */
@Tag(name = "主页控制器", description = "Home")
@RestController
@RequestMapping("api/visualdev/Dashboard")
public class DashboardController {
    @Autowired
    private TaskApi taskApi;
    @Autowired
    private EmailReceiveService emailReceiveService;
    @Autowired
    private MessageService messageService;





    /**
     * 获取我的待办
     *
     * @return
     */
    @Operation(summary = "获取我的待办")
    @PostMapping("/FlowTodoCount")
    public ActionResult getFlowTodoCount(@RequestBody FlowTodo flowTodo) {
        FlowTodoCountVO vo = new FlowTodoCountVO();
        TaskPagination pagination = new TaskPagination();
        pagination.setDelegateType(true);
        pagination.setPageSize(1L);
        pagination.setCurrentPage(1L);
        pagination.setFlowCategory(String.join(",",flowTodo.getFlowToSignType()));
        pagination.setCategory("0");
        taskApi.getWaitList(pagination);
        vo.setFlowToSign(pagination.getTotal());

        pagination.setFlowCategory(String.join(",",flowTodo.getFlowTodoType()));
        pagination.setCategory("1");
        taskApi.getWaitList(pagination);
        vo.setFlowTodo(pagination.getTotal());

        pagination.setFlowCategory(String.join(",",flowTodo.getFlowDoingType()));
        pagination.setCategory("2");
        taskApi.getWaitList(pagination);
        vo.setFlowDoing(pagination.getTotal());

        pagination.setTotal(0);
        pagination.setFlowCategory(String.join(",",flowTodo.getFlowDoneType()));
        taskApi.getTrialList(pagination);
        vo.setFlowDone(pagination.getTotal());

        pagination.setFlowCategory(String.join(",",flowTodo.getFlowCirculateType()));
        taskApi.getCirculateList(pagination);
        vo.setFlowCirculate(pagination.getTotal());

        return ActionResult.success(vo);
    }

    /**
     * 获取通知公告
     *
     * @return
     */
    @Operation(summary = "获取通知公告")
    @PostMapping("/Notice")
    public ActionResult getNotice(@RequestBody NoticeModel noticeModel) {
        List<NoticeVO> list = JsonUtil.getJsonToList(messageService.getNoticeList(noticeModel.getTypeList()), NoticeVO.class);
        ListVO<NoticeVO> voList = new ListVO();
        voList.setList(list);
        return ActionResult.success(voList);
    }

    /**
     * 获取未读邮件
     *
     * @return
     */
    @Operation(summary = "获取未读邮件")
    @GetMapping("/Email")
    public ActionResult getEmail() {
        List<EmailVO> list = JsonUtil.getJsonToList(emailReceiveService.getDashboardReceiveList(), EmailVO.class);
        ListVO<EmailVO> voList = new ListVO<>();
        voList.setList(list);
        return ActionResult.success(voList);
    }

    /**
     * 获取待办事项
     *
     * @return
     */
    @Operation(summary = "获取待办事项")
    @GetMapping("/FlowTodo")
    public ActionResult getFlowTodo(@RequestParam("type") Integer type) {
        TaskPagination pagination = new TaskPagination();
        pagination.setDelegateType(true);
        pagination.setPageSize(10L);
        pagination.setCurrentPage(1L);
        String category;
        switch (type) {
            case 1:
                category = "0";
                break;
            case 2:
                category = "1";
                break;
            default:
                category = "2";
        }
        pagination.setCategory(category);
        List<OperatorVo> waitList = taskApi.getWaitList(pagination);
        List<FlowTodoVO> list = new ArrayList<>();
        for (OperatorVo operatorVo : waitList) {
            FlowTodoVO vo = JsonUtil.getJsonToBean(operatorVo, FlowTodoVO.class);
            vo.setTaskNodeId(operatorVo.getNodeCode());
            vo.setTaskOperatorId(operatorVo.getId());
            vo.setType(2);
            list.add(vo);
        }
        ListVO voList = new ListVO<>();
        voList.setList(list);
        return ActionResult.success(voList);
    }

    /**
     * 获取我的待办事项
     *
     * @return
     */
    @Operation(summary = "获取我的待办事项")
    @GetMapping("/MyFlowTodo")
    public ActionResult getMyFlowTodo() {
        TaskPagination pagination = new TaskPagination();
        pagination.setCategory("2");
        List<MyFlowTodoVO> list = JsonUtil.getJsonToList(taskApi.getWaitList(pagination), MyFlowTodoVO.class);
        ListVO<MyFlowTodoVO> voList = new ListVO<>();
        voList.setList(list);
        return ActionResult.success(voList);
    }
}

