package com.zd.laboratory.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.security.utils.SaveUtil;

import com.zd.laboratory.mqtt.service.impl.SubMessageSendManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.laboratory.mapper.ApkMachineconfigMapper;
import com.zd.laboratory.domain.ApkMachineconfig;
import com.zd.laboratory.service.IApkMachineconfigService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author zd
 * @date 2021-12-09
 */
@Service
public class ApkMachineconfigServiceImpl implements IApkMachineconfigService
{
    @Autowired
    private ApkMachineconfigMapper apkMachineconfigMapper;


    @Autowired
    private SubMessageSendManager messageSendService;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public ApkMachineconfig selectApkMachineconfigById(Long id)
    {
        return apkMachineconfigMapper.selectApkMachineconfigById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param apkMachineconfig 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<ApkMachineconfig> selectApkMachineconfigList(ApkMachineconfig apkMachineconfig)
    {
        return apkMachineconfigMapper.selectApkMachineconfigList(apkMachineconfig);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param apkMachineconfig 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertApkMachineconfig(ApkMachineconfig apkMachineconfig)
    {
        //设置其他公共字段
        SaveUtil.setCommonAttr(apkMachineconfig);
        ApkMachineconfig apkMachineconfigNew = selectApkMachineconfigByHardwareNum(apkMachineconfig.getHardwareNum());
        if(apkMachineconfigNew!=null){
            throw new ServiceException("不能重复添加设备编号");
        }
        return apkMachineconfigMapper.insertApkMachineconfig(apkMachineconfig);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param apkMachineconfig 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateApkMachineconfig(ApkMachineconfig apkMachineconfig)
    {
        apkMachineconfig.setUpdateTime(DateUtils.getNowDate());
        apkMachineconfig.setUpdateBy(SecurityUtils.getUsername());
        int flag = apkMachineconfigMapper.updateApkMachineconfig(apkMachineconfig);
        //向一体机发送消息
        sendOnePcInfo(apkMachineconfig.getId());
        return flag;
    }

    public void sendOnePcInfo(Long id){
        ApkMachineconfig apkMachineconfig = selectApkMachineconfigById(id);
        //向一体机发送消息，通知变更配置文件
        JSONObject object = new JSONObject();
        object.put("onepcHardwareNum",apkMachineconfig.getHardwareNum());
        object.put("baseUrl", apkMachineconfig.getBaseUrl());
        object.put("signinBaseUrl", apkMachineconfig.getSigninBaseUrl());
        object.put("mqttUrl", apkMachineconfig.getMqttUrl());
        object.put("mqttName", apkMachineconfig.getMqttName());
        object.put("mqttPwd", apkMachineconfig.getMqttPwd());
        messageSendService.sendOnepcUpConfig(object);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteApkMachineconfigByIds(Long[] ids)
    {
        return apkMachineconfigMapper.deleteApkMachineconfigByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteApkMachineconfigById(Long id)
    {
        return apkMachineconfigMapper.deleteApkMachineconfigById(id);
    }

    @Override
    public ApkMachineconfig selectApkMachineconfigByHardwareNum(String hardwareNum) {
        return apkMachineconfigMapper.selectApkMachineconfigByHardwareNum(hardwareNum);
    }
}
