package com.mortals.xhx.module.task.service.impl;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.xhx.base.framework.config.InterceptorConfig;
import com.mortals.xhx.common.code.FlowStatusEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.pdu.api.ApiRespPdu;
import com.mortals.xhx.common.pdu.flow.FlowTaskPdu;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.feign.flowable.IApiFlowInstanceFeign;
import com.mortals.xhx.feign.flowable.IApiFlowTaskFeign;
import com.mortals.xhx.module.deploy.service.DeployFormService;
import com.mortals.xhx.module.form.service.FormService;
import com.mortals.xhx.module.task.model.TaskProcessEntity;
import com.mortals.xhx.module.task.model.TaskTodoQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.module.task.dao.TaskTodoDao;
import com.mortals.xhx.module.task.model.TaskTodoEntity;
import com.mortals.xhx.module.task.service.TaskTodoService;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>Title: 工作代办表</p>
 * <p>Description: TaskTodoService service接口 </p>
 * <p>Copyright: Copyright &reg;  </p>
 *
 * @version 1.0.0
 */
@Service("taskTodoService")
public class TaskTodoServiceImpl extends AbstractCRUDServiceImpl<TaskTodoDao, TaskTodoEntity, Long> implements TaskTodoService {

    @Autowired
    private IApiFlowTaskFeign apiFlowTaskFeign;

    @Autowired
    private IApiFlowInstanceFeign iApiFlowInstanceFeign;
    @Autowired
    private DeployFormService deployFormService;
    @Autowired
    private FormService formService;
    @Autowired
    private InterceptorConfig config;


    @Override
    public Result<FlowTaskPdu> todoList(TaskTodoQuery query, PageInfo pageInfo, Context context) {
        Result<FlowTaskPdu> result = new Result<>();
        FlowTaskPdu flowTaskPdu = new FlowTaskPdu();
        flowTaskPdu.setUserId(context.getUser().getLoginName());
        try {
            if (!ObjectUtils.isEmpty(query)) {
                BeanUtils.copyProperties(query, flowTaskPdu, BeanUtil.getNullPropertyNames(query));
            }
            ApiRespPdu<Result<FlowTaskPdu>> resp = apiFlowTaskFeign.todoList(context.getUser().getId().toString(),config.getTenantId(), pageInfo);
            if (YesNoEnum.NO.getValue() == resp.getCode()) {
                log.error("获取代办列表异常：" + resp.getMsg());
                throw new AppException("获取代办列表异常：" + resp.getMsg());
            }
            return resp.getData();
        } catch (Exception e) {
            throw new AppException("获取代办列表异常：" + e.getMessage());
        }
    }
}