Newer
Older
pichan-haerbin / src / main / java / com / casic / service / impl / PressConfigServiceImpl.java
chaizhuang on 10 May 2023 2 KB 更换平台测试
package com.casic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.casic.dao.NbPressMapper;
import com.casic.entity.NbPress;
import com.casic.model.BusConfigParam;
import com.casic.model.ResponseData;
import com.casic.service.PressCommandSend;
import com.casic.service.PressConfigService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@AllArgsConstructor
public class PressConfigServiceImpl extends ServiceImpl<NbPressMapper,NbPress> implements PressConfigService {
    private final PressCommandSend pressCommandSend;
    @Override
    @Transactional
    public ResponseData nbPressConfig(String deviceId) {
        BusConfigParam busConfigParam=nbPressConfigCreator();
        if(StringUtils.isEmpty(deviceId)){
            List<NbPress> nbPressList = this.baseMapper.selectList(null);
            for (NbPress nbPress : nbPressList) {
                busConfigParam.setProductId(nbPress.getProductId());
                busConfigParam.setDeviceId(nbPress.getDeviceId());
                String status = pressCommandSend.sendConfig(busConfigParam);
                nbPress.setConfigStatus(status);
            }
            this.saveOrUpdateBatch(nbPressList);
            return ResponseData.success("批量下发成功");
        }else {
            QueryWrapper<NbPress> nbPressWrapper=  new QueryWrapper<NbPress>().eq("device_id",deviceId);
            List<NbPress> nbPressList = this.baseMapper.selectList(nbPressWrapper);
            if(CollectionUtils.isEmpty(nbPressList)){
                return ResponseData.error("id不存在");
            }else {
                NbPress nbPress=nbPressList.stream().findFirst().get();
                busConfigParam.setProductId(nbPress.getProductId());
                busConfigParam.setDeviceId(nbPress.getDeviceId());
                String status = pressCommandSend.sendConfig(busConfigParam);
                nbPress.setConfigStatus(status);
                this.baseMapper.updateById(nbPress);
                return ResponseData.success(status);
            }
        }
    }

    private BusConfigParam nbPressConfigCreator() {
        BusConfigParam busConfigParam = new BusConfigParam();
        Map<String, Object> dataGasMap = new HashMap<>();
        dataGasMap.put("serviceIdentifier", "parameter");
        Map<String, Object> params = new HashMap<>();
        params.put("BR","1");
        dataGasMap.put("params", params);
        busConfigParam.setContent(dataGasMap);
        busConfigParam.setLevel(1);
        busConfigParam.setOperator("casic");
        return busConfigParam;
    }

}