package com.njcn.prepare.harmonic.service.mysql.newalgorithm.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.common.pojo.param.StatisticsBizBaseParam;
import com.njcn.common.utils.PubUtils;
import com.njcn.device.biz.commApi.CommTerminalGeneralClient;
import com.njcn.device.biz.pojo.dto.DeptGetBase;
import com.njcn.device.biz.pojo.dto.StatisticsMonitor;
import com.njcn.device.biz.pojo.param.DeptGetLineParam;
import com.njcn.prepare.harmonic.mapper.mysql.newalgorithm.PmsStatisticsSpecialMonitorMapper;
import com.njcn.prepare.harmonic.pojo.mysql.PmsStatisticsSpecialMonitor;
import com.njcn.prepare.harmonic.pojo.param.LineParam;
import com.njcn.prepare.harmonic.service.mysql.newalgorithm.IPmsStatisticsSpecialMonitorService;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.po.Dept;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wr
 * @since 2023-10-13
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class PmsStatisticsSpecialMonitorServiceImpl extends MppServiceImpl<PmsStatisticsSpecialMonitorMapper, PmsStatisticsSpecialMonitor> implements IPmsStatisticsSpecialMonitorService {

    private final CommTerminalGeneralClient commTerminalGeneralClient;
    private final DeptFeignClient deptFeignClient;
    @Override
    @Async("asyncExecutor")
//    @Transactional(rollbackFor = {Exception.class})
    public void pmsStatisticsSpecialMonitorHandler(LineParam jobParam) {
        List<PmsStatisticsSpecialMonitor> info=new ArrayList<>();
        Dept dept = deptFeignClient.getRootDept().getData();
        StatisticsBizBaseParam param=new StatisticsBizBaseParam();
        param.setId(dept.getId());
        param.setStartTime(jobParam.getBeginTime());
        param.setEndTime(jobParam.getEndTime());
        List<StatisticsMonitor> data = commTerminalGeneralClient.getStatisticsMonitor(param).getData();
        if(CollUtil.isNotEmpty(data)){
            DeptGetLineParam deptGetLineParam=new DeptGetLineParam();
            deptGetLineParam.setDeptId(dept.getId());
            List<DeptGetBase> deptGetBases = commTerminalGeneralClient.getDeptChildrenByParent(deptGetLineParam).getData();
            Map<String, String> deptName = deptGetBases.stream().collect(Collectors.toMap(DeptGetBase::getUnitId, DeptGetBase::getUnitName));
            for (StatisticsMonitor datum : data) {
                if(deptName.containsKey(datum.getOrgId())){
                    PmsStatisticsSpecialMonitor monitor = BeanUtil.copyProperties(datum, PmsStatisticsSpecialMonitor.class);
                    monitor.setOrgName(deptName.get(datum.getOrgId()));
                    monitor.setDataDate(PubUtils.localDateTimeFormat(jobParam.getDataDate() + " " + "00:00:00"));
                    monitor.setStatus(1);
                    info.add(monitor);
                }
            }
        }
        if (CollUtil.isNotEmpty(info)) {
            log.info("info______________-"+JSON.toJSONString(info));
            this.saveOrUpdateBatchByMultiId(info, 500);
        }
    }
}
