package com.bonus.app.service.impl;

import cn.hutool.core.annotation.AliasFor;
import com.bonus.app.mapper.CraneMonitorMapper;
import com.bonus.app.service.ICraneMonitorService;
import com.bonus.common.core.constant.BusinessConstants;
import com.bonus.common.core.utils.DateTimeHelper;
import com.bonus.common.core.utils.encryption.Sm4Utils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.entity.app.AppParamsDto;
import com.bonus.common.entity.app.vo.DevInfoVo;
import com.bonus.common.entity.app.vo.DevWarnVo;
import com.bonus.common.entity.app.vo.JdConfigVo;
import com.bonus.common.entity.bracelet.vo.PersonVo;
import com.bonus.common.security.utils.ValidatorsUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @className:CraneMonitorServiceImpl
 * @author:cwchen
 * @date:2024-08-12-15:36
 * @version:1.0
 * @description:吊车监控
 */
@Service(value = "ICraneMonitorService")
@Slf4j
public class CraneMonitorServiceImpl implements ICraneMonitorService {

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

    @Resource(name = "ValidatorsUtils")
    private ValidatorsUtils validatorsUtils;

    @Override
    public AjaxResult getDzWarnList(AppParamsDto dto) {
        List<DevInfoVo> list = new ArrayList<>();
        try {
            dto.setDevType(BusinessConstants.DZYJ);
            list = mapper.getDzWarnList(dto);
            for (DevInfoVo vo : list) {
                vo.setLeaderPhone(Sm4Utils.decode(vo.getLeaderPhone()));
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getJdList(AppParamsDto dto) {
        List<DevInfoVo> list = new ArrayList<>();
        try {
            dto.setDevType(BusinessConstants.JD);
            list = mapper.getJdList(dto);
            for (DevInfoVo vo : list) {
                String electricQuantity = mapper.getElectricQuantity(vo.getDevId(), BusinessConstants.DL);
                vo.setElectricQuantity(electricQuantity);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getJdWarnList(AppParamsDto dto) {
        List<DevWarnVo> list = new ArrayList<>();
        try {
            dto.setDevType(BusinessConstants.JD);
            // 告警数据默认展示当天，不需要加日期限制可去掉
            dto.setStartTime(DateTimeHelper.getNowDay());
            dto.setEndTime(DateTimeHelper.getNowDay());
            list = mapper.getJdWarnList(dto);
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return AjaxResult.success(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult configJdDevice(JdConfigVo vo) {
        try {
            List<JdConfigVo.ConfigItems> configItems = vo.getConfigItems();
            if (CollectionUtils.isEmpty(configItems)) {
                return AjaxResult.error("配置项不能为空");
            }
            Set<JdConfigVo.ConfigItems> itemsHashSet = new HashSet<>(configItems);
            if (itemsHashSet.size() != configItems.size()) {
                return AjaxResult.error("配置项存在相同数据");
            }
            for (JdConfigVo.ConfigItems items : configItems) {
                String validResult = validatorsUtils.valid(items, JdConfigVo.ConfigItems.Query.class);
                if (StringUtils.isNotBlank(validResult)) {
                    return AjaxResult.error(validResult);
                }
            }
            mapper.delJdConfig(vo);
            mapper.addJdConfigData(vo);
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getConfigJdDevice(AppParamsDto dto) {
        List<JdConfigVo.ConfigItems> list = new ArrayList<>();
        try {
            list = mapper.getConfigJdDevice(dto);
        } catch (Exception e) {
            log.error(e.toString(),e);
        }
        return AjaxResult.success(list);
    }
}
