package com.bonus.app.service.impl;

import com.bonus.app.mapper.CraneMonitorMapper;
import com.bonus.app.service.ICraneMonitorService;
import com.bonus.common.core.constant.BusinessConstants;
import com.bonus.common.core.constant.HttpStatus;
import com.bonus.common.core.constant.SecurityConstants;
import com.bonus.common.core.domain.R;
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.security.utils.ValidatorsUtils;
import com.bonus.system.api.RemoteTcpService;
import com.bonus.system.api.domain.SysTcpMessage;
import com.github.pagehelper.PageInfo;
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.*;

/**
 * @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;

    @Resource
    private RemoteTcpService remoteTcpService;

    @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 PageInfo<DevWarnVo> getJdWarnList(AppParamsDto dto) {
        List<DevWarnVo> list = new ArrayList<>();
        try {
            dto.setDevType(BusinessConstants.JD);
            list = mapper.getJdWarnList(dto);
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        PageInfo<DevWarnVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult configJdDevice(JdConfigVo vo) {
        try {
            List<JdConfigVo.ConfigItems> configItems = vo.getConfigItems();
            if (CollectionUtils.isEmpty(configItems)) {
                return AjaxResult.error("配置项不能为空");
            }
            if(configItems.size() > 10){
                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);
                }
            }
            SysTcpMessage sysTcpMessage = new SysTcpMessage();
            sysTcpMessage.setDevCode(vo.getDevCode());
            sysTcpMessage.setDistance(Float.parseFloat(configItems.get(0).getDistance()));
            R result = remoteTcpService.setMessage(sysTcpMessage, SecurityConstants.INNER);
            log.info("调用结果：code:{},msg:{}", result.getCode(),result.getMsg());
            if (result != null && result.getCode() == HttpStatus.SUCCESS && Objects.equals(result.getMsg(), BusinessConstants.SET_MSG)) {
                mapper.delJdConfig(vo);
                mapper.addJdConfigData(vo);
                return AjaxResult.success(result.getMsg());
            } else {
                return AjaxResult.error(result.getMsg());
            }
        } 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);
    }
}
