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.api.Page;
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.feign.req.BaseFlowReq;
import com.mortals.xhx.feign.req.BaseQuery;
import com.mortals.xhx.feign.rsp.ApiResp;
import com.mortals.xhx.feign.rsp.TaskRsp;
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) {
        Result<FlowTaskPdu> result = new Result<>();
        FlowTaskPdu flowTaskPdu = new FlowTaskPdu();
        flowTaskPdu.setUserCode(context.getUser().getLoginName());
        try {
            BaseFlowReq<BaseQuery> baseQueryReq = new BaseFlowReq<>();
            Page page = new Page();
            page.setPer(pageInfo.getPrePageResult());
            page.setSize(pageInfo.getCurrPage());
            BaseQuery baseQuery = new BaseQuery();
            baseQuery.setPlatformSn(config.getTenantId());
            baseQuery.setUserCode(context.getUser().getLoginName());
            baseQueryReq.setPage(page);
            baseQueryReq.setQuery(baseQuery);

            ApiResp<TaskRsp> resp = apiFlowTaskFeign.finishedList(baseQueryReq);

            if (YesNoEnum.NO.getValue() == resp.getCode()) {
                log.error("获取已办列表异常：" + resp.getMsg());
                throw new AppException("获取移办列表异常：" + resp.getMsg());
            }

            pageInfo.setTotalResult(resp.getData().getPage().getTotal());
            result.setPageInfo(pageInfo);
            result.setList(resp.getData().getList());

            return result;
        } catch (Exception e) {
            throw new AppException("获取代办列表异常：" + e.getMessage());
        }
    }
}