package com.ho.flow.service.impl;


import com.ho.common.tools.exception.DataResult;
import com.ho.flow.feignclient.UserFeignClient;
import com.ho.flow.mapper.WorkOrderCirculationMapper;
import com.ho.flow.service.WorkOrderCirculationService;
import com.ho.flow.vo.WorkOrderCirculation;
import com.ho.flow.vo.resp.workorder.WorkOrderCirculationRespVO;
import com.ho.user.api.entity.SysUser;
import com.ho.user.api.vo.req.QueryUserReqVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 针对表【work_order_circulation】的数据库操作Service实现
 * Author yule
 * Date 2022/10/14 15:40
 */
@Service
@Slf4j
public class WorkOrderCirculationServiceImpl implements WorkOrderCirculationService {

    @Autowired
    WorkOrderCirculationMapper workOrderCirculationMapper;

    @Autowired
    UserFeignClient userFeignClient;

    @Override
    public void add(WorkOrderCirculation workOrderCirculation) {
        int insert = workOrderCirculationMapper.insert(workOrderCirculation);
    }

    @Override
    public int insertEventBatch(List<WorkOrderCirculation> workOrderCirculations) {
        int i = workOrderCirculationMapper.insertBatch(workOrderCirculations);
        return i;
    }

    @Override
    public List<WorkOrderCirculationRespVO> selectByOrderId(String orderId) {
        List<WorkOrderCirculationRespVO> list = workOrderCirculationMapper.selectByOrderId(orderId);
        //发起人id集合
        List<QueryUserReqVO> fromIds = new ArrayList<>();
        //执行人id集合
        List<QueryUserReqVO> toIds = new ArrayList<>();


        //遍历集合给发起人id 执行人id 增加元素
        for (WorkOrderCirculationRespVO workOrderCirculationRespVO : list) {
            if (workOrderCirculationRespVO.getFromId() != null) {
                QueryUserReqVO queryFrom = new QueryUserReqVO();
                queryFrom.setUserId(workOrderCirculationRespVO.getFromId());
                fromIds.add(queryFrom);
            }
            if (workOrderCirculationRespVO.getToId() != null) {
                QueryUserReqVO queryTo = new QueryUserReqVO();
                queryTo.setUserId(workOrderCirculationRespVO.getToId());
                toIds.add(queryTo);
            }
        }

        //查询发起人用户集合
        List<SysUser> fromUsers = new ArrayList<>();
        if (!fromIds.isEmpty()) {
            DataResult<List<SysUser>> fromResult = userFeignClient.queryUser(fromIds);
            fromUsers = fromResult.getData();
        }
        //查询执行人用户集合
        List<SysUser> toUsers = new ArrayList<>();
        if (!toIds.isEmpty()) {
            DataResult<List<SysUser>> toResult = userFeignClient.queryUser(toIds);
            toUsers = toResult.getData();
        }

        for (int i = 0; i < list.size(); i++) {
            //遍历发起人用户集合获取到发起人名字,给工单的发起人名字赋值
            if ((list.get(i).getFromId() != null)) {
                for (SysUser fromUser : fromUsers) {
                    if (list.get(i).getFromId().equals(fromUser.getId())) {
                        list.get(i).setFromName(fromUser.getUsername());
                        break;
                    }
                }
            }
            if (list.get(i).getToId() != null) {
                for (SysUser toUser : toUsers) {
                    if (list.get(i).getToId().equals(toUser.getId())) {
                        list.get(i).setToName(toUser.getUsername());
                        break;
                    }
                }
            }
        }
        return list;
    }
}
