package cn.ey.biz.module.system.api.bpm;

import cn.ey.biz.common.exception.ErrorCode;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.framework.web.core.util.WebFrameworkUtils;
import cn.ey.biz.module.system.api.bpm.dto.*;
import cn.ey.biz.module.system.convert.bpm.BpmProcessInstanceConvert;
import cn.ey.biz.module.system.service.bpm.BpmProcessInstanceService;
import cn.ey.biz.module.system.service.bpm.BpmTaskService;
import cn.ey.biz.module.weaver.api.RequestOperatorInfoService;
import cn.ey.biz.module.weaver.common.RSP;
import com.alibaba.fastjson.JSON;
import jodd.util.StringUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

import static cn.ey.biz.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ey.biz.module.system.enums.ApiConstants.VERSION;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@DubboService(version = VERSION) // 提供 Dubbo RPC 接口，给 Dubbo Consumer 调用
@Validated
public class BpmApiImpl implements BpmApi {

    @Resource
    BpmProcessInstanceService bpmProcessInstanceService;
    @Resource
    BpmTaskService bpmTaskService;
    @Resource
    RequestOperatorInfoService requestOperatorInfoService;

    @Override
    public R<String> createProcessInstance(BpmProcessInstanceCreateDTO createDTO) {
        HttpServletRequest servletRequest=getRequest();
        servletRequest.setAttribute("sysCode",createDTO.getSysCode());
        if(StringUtil.isNotBlank(createDTO.getUserCode())){
            WebFrameworkUtils.setLoginUserCode(createDTO.getUserCode());
        }
        return R.success(bpmProcessInstanceService.createProcessInstance(createDTO));
    }

    @Override
    public R<BpmWorkflowRespDTO> getWorkflowRequest(BpmWorkflowDTO workflowDTO) {
        HttpServletRequest servletRequest=getRequest();
        servletRequest.setAttribute("sysCode",workflowDTO.getSysCode());
        return R.success(bpmProcessInstanceService.getWorkflowRequest(workflowDTO));
    }

    @Override
    public R<Boolean> approveTask(@Valid BpmTaskApproveDTO workflowDTO) {
        HttpServletRequest servletRequest=getRequest();
        servletRequest.setAttribute("sysCode",workflowDTO.getSysCode());
        bpmTaskService.approveTask(workflowDTO.getUserCode(),workflowDTO);
        return R.success((true));
    }

    @Override
    public R<Boolean> rejectTask(@Valid BpmTaskRejectDTO workflowDTO) {
        HttpServletRequest servletRequest=getRequest();
        servletRequest.setAttribute("sysCode",workflowDTO.getSysCode());
        bpmTaskService.rejectTask(Long.valueOf(workflowDTO.getUserCode()),workflowDTO);
        return R.success((true));
    }

    @Override
    public R<List<BpmOperatorInfoRespDTO>> getRequestOperatorInfo(@Valid BpmOperatorInfoDTO operatorInfoDTO) {
        HttpServletRequest servletRequest=getRequest();
        servletRequest.setAttribute("sysCode",operatorInfoDTO.getSysCode());
        RSP<List<RequestOperatorInfoService.OperatorInfoResponse>> response=requestOperatorInfoService.operatorInfo(operatorInfoDTO.getRequestId());
        if(response.isSuccess()){
            List<BpmOperatorInfoRespDTO> result = BpmProcessInstanceConvert.INSTANCE.convert(response.getData());
            return R.success(result);
        }else{
            throw exception(new ErrorCode(-1, JSON.toJSONString(response.getErrMsg())));
        }
    }

    @Override
    public R<Boolean> approveTaskNominator(@Valid BpmTaskApproveNominatorDTO nominatorDTO) {
        HttpServletRequest servletRequest=getRequest();
        servletRequest.setAttribute("sysCode",nominatorDTO.getSysCode());
        bpmTaskService.approveTaskNominator(nominatorDTO);
        return R.success((true));
    }

    @Override
    public R<Boolean> withdrawRequest(@Valid BpmWithdrawRequestDTO bpmWithdrawRequestDTO) {
        HttpServletRequest servletRequest=getRequest();
        servletRequest.setAttribute("sysCode",bpmWithdrawRequestDTO.getSysCode());
        bpmProcessInstanceService.withdrawRequest(bpmWithdrawRequestDTO);
        return R.success((true));
    }

    @Override
    public R<String> getStatusById(@Valid BpmGetStatusByIdDTO bpmGetStatusByIdDTO) {
        HttpServletRequest servletRequest=getRequest();
        servletRequest.setAttribute("sysCode",bpmGetStatusByIdDTO.getSysCode());
        return R.success((bpmProcessInstanceService.getStatusById(bpmGetStatusByIdDTO)));
    }

    @Override
    public R<Boolean> doForceOver(@Valid BpmDoForceOverDTO bpmDoForceOverDTO) {
        HttpServletRequest servletRequest=getRequest();
        servletRequest.setAttribute("sysCode",bpmDoForceOverDTO.getSysCode());
        bpmProcessInstanceService.doForceOver(bpmDoForceOverDTO);
        return R.success((true));
    }

    @Override
    public R<ReceiveRequestInfoByJsonRespDTO> receiveRequestInfoByJson(@Valid ReceiveRequestInfoByJsonDTO receiveRequestInfoByJsonDTO) {
        ReceiveRequestInfoByJsonRespDTO result=bpmProcessInstanceService.ReceiveRequestInfoByJson(receiveRequestInfoByJsonDTO);
        return R.success((result));
    }

    @Override
    public R<DeleteUserRequestInfoByJsonRespDTO> deleteUserRequestInfoByJson(@Valid DeleteUserRequestInfoByJsonDTO deleteDTO) {
        DeleteUserRequestInfoByJsonRespDTO result=bpmProcessInstanceService.deleteUserRequestInfoByJson(deleteDTO);
        return R.success((result));
    }

    @Override
    public R<Boolean> addAgentSave(BpmAddAgentSaveDTO saveDTO) {
        HttpServletRequest servletRequest=getRequest();
        servletRequest.setAttribute("sysCode",saveDTO.getSysCode());
        bpmProcessInstanceService.addAgentSave(saveDTO);
        return R.success((true));
    }

    @Override
    public R<Boolean> deleteRequest(@Valid BpmDeleteRequestDTO deleteDTO) {
        HttpServletRequest servletRequest=getRequest();
        servletRequest.setAttribute("sysCode",deleteDTO.getSysCode());
        bpmProcessInstanceService.deleteRequest(deleteDTO);
        return R.success((true));
    }

    @Override
    public R<Boolean> checkOperatorAndShared(@Valid CheckOperatorAndSharedRequestDTO OperatorDTO) {
        HttpServletRequest servletRequest=getRequest();
        servletRequest.setAttribute("sysCode",OperatorDTO.getSysCode());
        return R.success(bpmProcessInstanceService.checkOperatorAndSharedButton(OperatorDTO));
    }

    protected HttpServletRequest getRequest(){
        return ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
    }

}
