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.framework.util.DateUtils;
import com.mortals.xhx.base.framework.config.InterceptorConfig;
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.IApiFlowTaskFeign;
import com.mortals.xhx.module.task.model.TaskFinishQuery;
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.TaskFinishDao;
import com.mortals.xhx.module.task.model.TaskFinishEntity;
import com.mortals.xhx.module.task.service.TaskFinishService;
import org.springframework.util.ObjectUtils;

/**
* <p>Title: 工作已办表</p>
* <p>Description: TaskFinishService service接口 </p>
* <p>Copyright: Copyright &reg;  </p>
* @version 1.0.0
*/
@Service("taskFinishService")
public class TaskFinishServiceImpl extends AbstractCRUDServiceImpl<TaskFinishDao, TaskFinishEntity, Long> implements TaskFinishService {

    @Autowired
    private IApiFlowTaskFeign apiFlowTaskFeign;

    @Autowired
    private InterceptorConfig config;



    @Override
    public Result<FlowTaskPdu> finishList(TaskFinishQuery query, PageInfo pageInfo, Context context) {

        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.finishedList(context.getUser().getId().toString(),config.getTenantId(), pageInfo);
            if (YesNoEnum.NO.getValue() == resp.getCode()) {
                log.error("获取已办列表异常：" + resp.getMsg());
                throw new AppException("获取已办列表异常：" + resp.getMsg());
            }

//            if (!ObjectUtils.isEmpty(resp.getData())) {
//                List<TaskTodoEntity> collect = resp.getData().getList().stream().map(item -> {
//                    TaskTodoEntity taskTodoEntity = new TaskTodoEntity();
//                    BeanUtils.copyProperties(item, taskTodoEntity, BeanUtil.getNullPropertyNames(item));
//                    taskTodoEntity.setProcDefVersion("v" + item.getProcDefVersion());
//
//                    return taskTodoEntity;
//                }).collect(Collectors.toList());
//                result.setList(collect);
//                result.setPageInfo(resp.getData().getPageInfo());
//            } else {
//                result.setList(Collections.emptyList());
//            }
            return resp.getData();
        } catch (Exception e) {
            throw new AppException(e.getMessage());
        }
    }
}