package com.sunxung.factoring.web.project;

import com.sunxung.factoring.component.exception.BusinessException;
import com.sunxung.factoring.component.util.CodeUtil;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.project.vo.SearchBusinessProcessingTask;
import com.sunxung.factoring.service.project.BusinessProcessingLogService;
import com.sunxung.factoring.service.project.BusinessProcessingTaskService;
import com.sunxung.factoring.service.sys.flowable.FlowableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Description:流程相关业务任务列表Controller（如立项流程、尽调流程、评审会流程等待我处理、我已处理和全部列表）
 * @Author:wanxiangZhou
 * @E-mail:zhouyu@sunxung.cn
 * @Date:2018年7月30日 下午7:18:28
 */
@Controller
@RequestMapping("/businessProcessingTask")
public class BusinessProcessingTaskController {

    @Autowired
    private BusinessProcessingTaskService businessProcesingTaskService;
    @Autowired
    private FlowableService flowableService;
    @Autowired
    private BusinessProcessingLogService processingLogService;

    /**
     * 查询待我处理的流程业务信息
     */
    @PostMapping(value = "/doMyPendingList")
    @ResponseBody
    public Object doMyPendingList(@RequestBody SearchBusinessProcessingTask search) {

        return businessProcesingTaskService.findMyPendingList(search);
    }

    /**
     * 我已处理的流程业务信息
     */
    @PostMapping(value = "/doMyCompletedList")
    @ResponseBody
    public Object doMyCompletedList(SearchBusinessProcessingTask search) {
        return businessProcesingTaskService.findMyCompletedList(search);
    }

    /**
     * 全部流程业务信息
     */
    @GetMapping(value = "/doList")
    @ResponseBody
    public Object doList(SearchBusinessProcessingTask search) {
        return businessProcesingTaskService.findAll(search);
    }

    /**
     * 时间轴
     */
    @GetMapping(value = "/doTimelineList")
    @ResponseBody
    public Object doTimelineList(@RequestParam Long businessKey, @RequestParam String businessType) {
        return processingLogService.findByBusiness(businessKey, businessType);
    }

    /**
     * 处理业务、办理任务
     */
    @RequestMapping("/dispatchMyTask")
    public String dispatchMyTask(String taskId, Long businessKey) {
        // 统一入口：设计好业务规则，根据不同的业务或者一个业务不同的状态，读取不同的表单
        return "redirect:" + flowableService.getTaskFormKeyByTaskId(taskId, businessKey);
    }

    /**
     * 加载流程业务内各个节点处理的历史信息
     */
    @GetMapping(value = "/doBusinessHistoryView")
    @ResponseBody
    public Object doBusinessHistoryView(@RequestParam Long businessKey, @RequestParam String businessType) {
        return businessProcesingTaskService.getHistoricBusinessById(businessKey, businessType);
    }

    /**
     * 验证任务是否已经完成或者被分配了
     *
     * @param taskId 任务ID
     * @return
     */
    @GetMapping(value = "/doDuplicateProcessing")
    @ResponseBody
    public Object doDuplicateProcessing(String taskId) {
        return businessProcesingTaskService.duplicateProcessingTask(taskId);
    }

    /**
     * 验证非业务流程下操作者的权限，比如账后运营专员、项目经理查看等等，因为其他地方可能会分配导致无权限查看
     */
    @GetMapping(value = "/doDuplicateProcessingBusiness")
    @ResponseBody
    public Object doDuplicateProcessingBusiness(Long businessId, String businessType, String msgType) {
        return businessProcesingTaskService.duplicateProcessingBusiness(businessId, businessType, msgType);
    }

    /**
     * 客户信息列表接口
     */
    @GetMapping(value = "/doDifferenceList")
    @ResponseBody
    public Object doFinancialCustomerList(SearchBusinessProcessingTask search) {
        if ("pending".equals(search.getDifference())) {
            //待我处理
            return new ResponseJson(businessProcesingTaskService.findMyPendingList(search));
        } else if ("completed".equals(search.getDifference())) {
            return new ResponseJson(businessProcesingTaskService.findMyCompletedList(search));
        } else if ("all".equals(search.getDifference())) {
            return new ResponseJson(businessProcesingTaskService.findAll(search));
        } else {
            return new BusinessException(CodeUtil.FAIL, "参数非法！");
        }
    }
}
