package com.securitycontrol.background.service.impl;

import com.securitycontrol.background.mapper.ITodayTaskMapper;
import com.securitycontrol.background.service.ITodayTaskService;
import com.securitycontrol.common.core.utils.aes.DateTimeHelper;
import com.securitycontrol.common.core.utils.aes.StringHelper;
import com.securitycontrol.common.core.web.domain.AjaxResult;
import com.securitycontrol.entity.background.dto.TodayTaskDto;
import com.securitycontrol.entity.background.vo.DeviceUpDownVo;
import com.securitycontrol.entity.background.vo.TodayTaskVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

/**
 * @author：cwchen
 * @date：2024-03-23-12:47
 * @version：1.0
 * @description：当日任务-业务层
 */
@Service(value = "ITodayTaskService")
@Slf4j
public class TodayTaskServiceImpl implements ITodayTaskService {

    @Resource(name = "ITodayTaskMapper")
    private ITodayTaskMapper mapper;

    @Resource(name = "testTaskExecutor")
    private ThreadPoolTaskExecutor testTaskExecutor;

    @Override
    public List<TodayTaskVo> getToDayTaskLists(TodayTaskDto dto) {
        // 多线程执行操作，存放的数据集合
        List<Future> futureList = new ArrayList<>();
        List<TodayTaskVo> newList = new ArrayList<>();
        try {
            dto = handleParams(dto);
            List<TodayTaskVo> list = mapper.getToDayTaskLists(dto);
            for (TodayTaskVo todayTaskVo : list) {
                Future<TodayTaskVo> future = testTaskExecutor.submit(new Callable<TodayTaskVo>() {
                    @Override
                    public TodayTaskVo call() throws Exception {
                        // 查询督查人
                        String checkUserName = mapper.getCheckUser(todayTaskVo.getCheckUserId());
                        todayTaskVo.setCheckUserName(checkUserName);
                        return todayTaskVo;
                    }
                });
                futureList.add(future);
            }
            for (Future<TodayTaskVo> future : futureList) {
                TodayTaskVo todayTaskVo = future.get();
                newList.add(todayTaskVo);
            }
        } catch (Exception e) {
            log.error("获取当日任务列表", e);
        }
        return newList;
    }

    /**
     * 处理参数
     * @param dto
     * @return TodayTaskDto
     * @description
     * @author cwchen
     * @date 2024/3/23 14:33
     */
    public TodayTaskDto handleParams(TodayTaskDto dto) {
        if (StringUtils.isBlank(dto.getWorkDay())) {
            dto.setStartTime(DateTimeHelper.getNowDate());
            dto.setEndTime(DateTimeHelper.getNowDate());
        } else {
            String[] dateArr = dto.getWorkDay().split(" - ");
            dto.setStartTime(dateArr[0]);
            dto.setEndTime(dateArr[1]);
        }
        if (StringUtils.isNotBlank(dto.getOrgCode())) {
            String[] buildCodeArr = dto.getOrgCode().split(",");
            List<String> buildCodeList = Arrays.asList(buildCodeArr);
            dto.setOrgCodeList(buildCodeList);
        }
        if (StringUtils.isNotBlank(dto.getRiskLevel())) {
            String[] riskLevelArr = dto.getRiskLevel().split(",");
            List<String> riskLevelList = Arrays.asList(riskLevelArr);
            dto.setRiskLevelList(riskLevelList);
        }
        return dto;
    }

    @Override
    public AjaxResult getToDayTaskDetail(TodayTaskDto dto) {
        Map<String, Object> map = new HashMap<>(16);
        Map<String, String> mapResult = new HashMap<>(6);
        List<Map<String, String>> resultList = new ArrayList<>(100);
        try {
            Future<Map<String, Map<String, String>>> future = testTaskExecutor.submit(() -> {
                log.info("站班会详情-执行线程{}", Thread.currentThread().getName());
                Map<String, Map<String, String>> dataMap = new HashMap<>(16);
                dataMap = mapper.getClassDetail(dto);
                return dataMap;
            });
            Future<List<Map<String, String>>> future2 = testTaskExecutor.submit(() -> {
                log.info("施工人员-执行线程{}", Thread.currentThread().getName());
                List<Map<String, String>> list = new ArrayList<>(100);
                list = mapper.getWorkPeopleLists(dto);
                return list;
            });
            mapResult = future.get().get(dto.getClassId());
            resultList = future2.get();
        } catch (Exception e) {
            log.error("远程巡视详情",e);
        }
        map.put("classDetails",mapResult);
        map.put("workPeopleLists",resultList);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult getBallTimeList(TodayTaskDto dto) {
        List<DeviceUpDownVo> list = new ArrayList<>(100);
        try {
            if (StringHelper.isEmpty(dto.getWorkDay())) {
                dto.setWorkDay(DateTimeHelper.getNowDay());
            }
            list = mapper.getBallTimeList(dto);
            for (DeviceUpDownVo vo : list) {
                if (StringHelper.isEmpty(vo.getDownTime())) {
                    Double hours = DateTimeHelper.getTimeHours(DateTimeHelper.getNowTime(), vo.getUpTime());
                    vo.setHours(hours);
                } else {
                    Double hours = DateTimeHelper.getTimeHours(vo.getDownTime(), vo.getUpTime());
                    vo.setHours(hours);
                }
            }
        } catch (Exception e) {
            log.error("球机上下线时长",e);
        }
        return AjaxResult.success(list);
    }
}
