package org.jeecg.modules.approval.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.approval.model.dto.ApprovalOpinionDTO;
import org.jeecg.modules.approval.model.vo.TaskVO;
import org.jeecg.modules.approval.service.ApprovalTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * 任务控制层
 *
 * @Author: len.sun
 * @Date: 2019/6/15
 */
@Slf4j
@RestController
@RequestMapping("/approval/task")
public class ApprovalTaskController {

    @Autowired
    private ApprovalTaskService approvalTaskService;



    /**
     * 待处理任务列表
     */
    @GetMapping("/list")
    public Result list(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                       @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                       HttpServletRequest req) {

        IPage page = approvalTaskService.listMyTask( pageNo, pageSize,req);
        return Result.OK(page);
    }

    /**
     * 已处理任务列表
     */
    @GetMapping("/list/processed")
    public Result listProcessed(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                HttpServletRequest req) {

        return Result.OK(approvalTaskService.listMyProcessedTask(pageNo, pageSize,req));
    }

    /**
     * 任务明细
     *
     * @param taskId
     * @return
     */
    @GetMapping("detail/{id}")
    public Result agent(@PathVariable("id") String taskId) {
        return Result.OK(approvalTaskService.getTask(taskId));
    }

    /**
     * 获取流程图片
     *
     * @param processId
     * @param response
     */
    @RequestMapping("processImg/{processId}")
    public void viewProcessImg(@PathVariable String processId, HttpServletResponse response) {
        try {
            byte[] processImage = approvalTaskService.getProcessImage(processId);
            OutputStream outputStream = response.getOutputStream();
            InputStream in = new ByteArrayInputStream(processImage);
            IOUtils.copy(in, outputStream);
        } catch (Exception e) {
            log.error("viewProcessImg---- {}", e);
        }

    }

    /**
     * 获取当前用户发起的流程
     *
     * @return
     */
    @PostMapping("/myStarted")
    public Result listApprovalMyStarted(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                        @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                        HttpServletRequest req) {

        return Result.OK(approvalTaskService.listApprovalMyStarted(pageNo, pageSize,req));
    }


    /**
     * 根据流程id获取待我审批的流程
     *
     * @param processInstanceId
     * @return
     */
    @GetMapping("/getMyApprovalTask")
    public Result<TaskVO> getMyApprovalTask(@RequestParam String processInstanceId) {
        TaskVO myApprovalTask = approvalTaskService.getMyApprovalTask(processInstanceId);
        return Result.OK(myApprovalTask);
    }

    /**
     * 根据流程id获取审批记录
     * @param processInstanceId
     * @return
     */
    @GetMapping("/listOpinionsByPId")
    public Result listOpinionsByPId(@RequestParam String processInstanceId){
        List<ApprovalOpinionDTO> opinions = approvalTaskService.listOpinionsByPId(processInstanceId);
        return Result.OK(opinions);
    }
}
