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.xhx.base.framework.config.InterceptorConfig;
import com.mortals.xhx.base.system.role.model.RoleUserQuery;
import com.mortals.xhx.base.system.role.service.RoleService;
import com.mortals.xhx.base.system.role.service.RoleUserService;
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.api.Page;
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.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.deploy.service.DeployFormService;
import com.mortals.xhx.module.form.service.FormService;
import com.mortals.xhx.module.site.service.SiteService;
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.ArrayList;
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;
    @Autowired
    private RoleUserService roleUserService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private SiteService siteService;


    @Override
    public Result<FlowTaskPdu> todoList(TaskTodoQuery query, PageInfo pageInfo, Context context) {
        Result<FlowTaskPdu> result = new Result<>();
        Long siteId = context.getUser().getSiteId();
        RoleUserQuery roleUserQuery = new RoleUserQuery();
        roleUserQuery.setUserId(context.getUser().getId());
        List<String> collect = roleUserService.find(roleUserQuery).stream().map(item -> {
            return siteService.get(siteId).getSiteName() + "[" + roleService.get(item.getRoleId()).getName()+"]";
        }).collect(Collectors.toList());
        //siteId+roid (多个)

        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());
            baseQuery.setCandidateGroups(collect);
            baseQueryReq.setPage(page);
            baseQueryReq.setQuery(baseQuery);

            ApiResp<TaskRsp> resp = apiFlowTaskFeign.todoList(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());
        }
    }


    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));
    }
}