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

import com.alibaba.fastjson.JSON;
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.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.base.framework.config.InterceptorConfig;
import com.mortals.xhx.busiz.req.BaseFlowReq;
import com.mortals.xhx.busiz.req.BaseQuery;
import com.mortals.xhx.busiz.rsp.ApiResp;
import com.mortals.xhx.busiz.rsp.TaskRsp;
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.feign.flowable.IApiFlowInstanceFeign;
import com.mortals.xhx.feign.flowable.IApiFlowTaskFeign;
import com.mortals.xhx.flowable.task.CommonTaskReq;
import com.mortals.xhx.module.deploy.service.DeployFormService;
import com.mortals.xhx.module.task.dao.TaskTodoDao;
import com.mortals.xhx.module.task.model.TaskTodoEntity;
import com.mortals.xhx.module.task.model.TaskTodoQuery;
import com.mortals.xhx.module.task.service.TaskTodoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * <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 InterceptorConfig config;


    @Override
    public Result<FlowTaskPdu> todoList(TaskTodoQuery query, PageInfo pageInfo, Context context) {
        Result<FlowTaskPdu> result = new Result<>();

        try {
            CommonTaskReq commonTaskReq = new CommonTaskReq();
            commonTaskReq.setPageInfo(pageInfo);
            ApiRespPdu<Result<FlowTaskPdu>> resp = apiFlowTaskFeign.todoList(commonTaskReq);
            if (YesNoEnum.NO.getValue() == resp.getCode()) {
                log.error("获取代办列表异常：" + resp.getMsg());
                throw new AppException("获取代办列表异常：" + resp.getMsg());
            }

            pageInfo.setTotalResult(resp.getData().getPageInfo().getTotalResult());
            result.setPageInfo(pageInfo);
            result.setList(resp.getData().getList());

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


    public static void main(String[] args) {
        BaseFlowReq<BaseQuery> baseQueryReq = new BaseFlowReq<>();
        Page page = new Page();
        page.setPer(10);
        page.setSize(1);
        BaseQuery baseQuery = new BaseQuery();
        baseQuery.setPlatformSn("goverment");
        baseQuery.setUserCode("2323");
        baseQueryReq.setPage(page);
        baseQueryReq.setQuery(baseQuery);

        System.out.println(JSON.toJSONString(baseQueryReq));
    }
}