package com.chinaweal.aiccs.aiccs.revoke.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chinaweal.aiccs.aiccs.auditing.entity.TSOpinion;
import com.chinaweal.aiccs.aiccs.auditing.service.TSOpinionService;
import com.chinaweal.aiccs.aiccs.business.entity.TSTaskList;
import com.chinaweal.aiccs.aiccs.business.service.TSTaskListService;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.rest.ResultCode;
import com.chinaweal.youfool.framework.springboot.util.AssertUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 *  审批流程处理人控制器
 *
 * @Author Lee
 * @Since 2021/5/18 13:51
 */
@RestController("reviewer")
public class ReviewerController {

    @Autowired
    private TSTaskListService taskListService;
    @Autowired
    private TSOpinionService opinionService;

    /**
     *  获取上一步审核人
     *
     * @param tasklistid  待办id
     * @return
     */
    @GetMapping("getLastHandlerInfo")
    public RestResult<?> getLastHandlerInfo(String tasklistid) {
        AssertUtils.isNotBlank(tasklistid);
        TSTaskList tasklist = taskListService.getOne(new LambdaQueryWrapper<TSTaskList>().eq(TSTaskList::getTasklistid, tasklistid));
        if (tasklist == null) {
            return RestResult.error(ResultCode.DATA_NONE);
        }
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("senderName",tasklist.getSendername());
        resultMap.put("senderUserId",tasklist.getSenderuserid());
        return RestResult.ok(resultMap);
    }

    /**
     * 获取审批流程意见列表的经办人
     *
     * @param workFlowId   工作流id
     * @param opinionType  意见类型
     * @return
     */
    @GetMapping("getHandlerbyOpinionType")
    public RestResult<?> getHandlerbyOpinionType(String workFlowId,String opinionType) {
        AssertUtils.isNotBlank(workFlowId,opinionType);
        LambdaQueryWrapper<TSOpinion> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TSOpinion::getWorkflowid,workFlowId)
                .eq(TSOpinion::getOpiniontype,opinionType)
                .orderByDesc(TSOpinion::getLauptime);
        List<TSOpinion> list = opinionService.list(wrapper);
        if(CollectionUtils.isEmpty(list)){
            return RestResult.error(ResultCode.DATA_NONE);
        }
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("senderName",list.get(0).getHandler());
        resultMap.put("senderUserId",list.get(0).getHandlerid());
        return RestResult.ok(resultMap);
    }
}
