package com.ho.flow.threads;

import cn.hutool.core.date.DateUtil;
import com.ho.business.entity.Station;
import com.ho.common.tools.entity.OrderForm;
import com.ho.common.tools.entity.ProcessOrder;
import com.ho.common.tools.exception.DataResult;
import com.ho.flow.entity.process.MovementDTO;
import com.ho.flow.entity.process.PreDoUser;
import com.ho.flow.feignclient.BusinessFeignClient;
import com.ho.flow.service.process.service.ProcessFlowNewService;
import com.ho.flow.vo.Event;
import com.ho.flow.vo.req.workorder.DoTaskReq;
import com.ho.flow.vo.req.workorder.TaskUserReq;
import com.ho.user.api.entity.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class EventToProcessThread implements Runnable {
    private ProcessFlowNewService processFlowNewService;

    private BusinessFeignClient businessFeignClient;

    private MovementDTO movementDTO;

    public EventToProcessThread(ProcessFlowNewService processFlowNewService, MovementDTO movementDTO,BusinessFeignClient businessFeignClient) {
        this.processFlowNewService = processFlowNewService;
        this.movementDTO = movementDTO;
        this.businessFeignClient = businessFeignClient;
    }


    public static final Integer offset = 6;

    @Override
    @Transactional
    public void run() {
        log.error("EventToProcessThread.run");
        List<Event> events = movementDTO.getEvents();
        for (Event event : events) {
            // 流程模板id和告警等级暂时用写死来测试,之后改成读取数据库关联关系信息来创建
            ProcessOrder processOrder = new ProcessOrder();
            OrderForm orderForm = new OrderForm();
            orderForm.setStationId(event.getStationId());
            orderForm.setDeviceId(event.getTargetDevice());
            orderForm.setEventId(event.getId());
            orderForm.setTitle(event.getDescription());
            Date afterAddingHours = DateUtil.offsetHour(new Date(), offset);
            orderForm.setPlannedTime(afterAddingHours);
            orderForm.setUserId(movementDTO.getUserId() == null ? "1" : movementDTO.getUserId());
            orderForm.setPriority(movementDTO.getPriority() == null ? 1 : movementDTO.getPriority());
            orderForm.setOrderType(movementDTO.getOrderType() == null ? 3 : movementDTO.getOrderType());
            //todo 现只有一套流程定义模版
            processOrder.setOrderForm(orderForm);
            processOrder.setKey("Process_approve");
            processOrder.setUserId(movementDTO.getUserId() == null ? "1" : movementDTO.getUserId());
            /** todo:新建时不返回实例id **/
            processFlowNewService.initProcessInstance(processOrder);

            // 立刻执行
            DoTaskReq doTaskReq = new DoTaskReq();
            doTaskReq.setType("1");
            doTaskReq.setOrderForm(orderForm);
            /** todo: 候选人组 */
            /** start */
            //根据用户id查询对应表单数据
            List<SysUser> users = processFlowNewService.getFristUsers(orderForm.getProcessInstanceKey());
            DataResult<Station> stationDataResult = businessFeignClient.selectByStationId(event.getStationId());
            //过滤其他公司的人
            Integer deptId = stationDataResult.getData().getDeptId();
            if (!deptId.equals(155)) {
                users = users.stream().filter(s -> {
                    if (s.getDeptId().equals(deptId)) {
                        return true;
                    } else {
                        return false;
                    }
                }).collect(Collectors.toList());
            }
            List<String> candidateUserList = users.stream().map(SysUser::getId).collect(Collectors.toList());
            /** end */
            //设置发送者的id
            doTaskReq.setSenderId(movementDTO.getUserId() == null ? "1" : movementDTO.getUserId());
            //设置租户id
            doTaskReq.setTenantId(String.valueOf(event.getGroupId()));
            //完成任务的责任人必须和自己相同,否则需要给出提示
            doTaskReq.setAssigneeUser(movementDTO.getUserId() == null ? "1" : movementDTO.getUserId());
            doTaskReq.setCandidateUserList(candidateUserList);
            doTaskReq.setSuggestion("");
//            processFlowNewService.getFristApproval(doTaskReq);
        }
    }
}
