package com.ruoyi.hardware.service.impl;

import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hardware.mapper.DoumuHardwareMapper;
import com.ruoyi.hardware.domain.DoumuHardware;
import com.ruoyi.hardware.service.IDoumuHardwareService;
import utils.QRCodeUtils;

/**
 * 设备Service业务层处理
 *
 * @author boman
 * @date 2024-01-23
 */
@Service
public class DoumuHardwareServiceImpl implements IDoumuHardwareService {
    @Autowired
    private DoumuHardwareMapper doumuHardwareMapper;

    /**
     * 查询设备
     *
     * @param hardwareId 设备主键
     * @return 设备
     */
    @Override
    public DoumuHardware selectDoumuHardwareByHardwareId(Long hardwareId) {
        return doumuHardwareMapper.selectDoumuHardwareByHardwareId(hardwareId);
    }

    /**
     * 查询设备列表
     *
     * @param doumuHardware 设备
     * @return 设备
     */
    @Override
    public List<DoumuHardware> selectDoumuHardwareList(DoumuHardware doumuHardware) {
        return doumuHardwareMapper.selectDoumuHardwareList(doumuHardware);
    }

    /**
     * 新增设备
     *
     * @param doumuHardware 设备
     * @return 结果
     */
    @Override
    public int insertDoumuHardware(DoumuHardware doumuHardware) {
        doumuHardware.setCreateTime(DateUtils.getNowDate());
        //根据设备编号生成二维码
        String hardwareNum = doumuHardware.getHardwareNum();
        if (StringUtils.isEmpty(hardwareNum)) {
            return 0;
        }
        try {
            String imagePath = QRCodeUtils.encode(hardwareNum, null, RuoYiConfig.getUploadQrPath(), true);
            doumuHardware.setHardwareUrl(imagePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return doumuHardwareMapper.insertDoumuHardware(doumuHardware);
    }

    /**
     * 修改设备
     *
     * @param doumuHardware 设备
     * @return 结果
     */
    @Override
    public int updateDoumuHardware(DoumuHardware doumuHardware) {
        DoumuHardware doumuHardwareOld = doumuHardwareMapper.selectDoumuHardwareByHardwareId(doumuHardware.getHardwareId());
        doumuHardware.setUpdateTime(DateUtils.getNowDate());
        //判断设备编号是否被修改
        if (!doumuHardwareOld.getHardwareNum().equals(doumuHardware.getHardwareNum())) {
            String imagePath = null;
            try {
                imagePath = QRCodeUtils.encode(doumuHardware.getHardwareNum(), null, RuoYiConfig.getUploadQrPath(), true);
                doumuHardware.setHardwareUrl(imagePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return doumuHardwareMapper.updateDoumuHardware(doumuHardware);
    }

    /**
     * 批量删除设备
     *
     * @param hardwareIds 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteDoumuHardwareByHardwareIds(Long[] hardwareIds) {
        return doumuHardwareMapper.deleteDoumuHardwareByHardwareIds(hardwareIds);
    }

    /**
     * 删除设备信息
     *
     * @param hardwareId 设备主键
     * @return 结果
     */
    @Override
    public int deleteDoumuHardwareByHardwareId(Long hardwareId) {
        return doumuHardwareMapper.deleteDoumuHardwareByHardwareId(hardwareId);
    }

    /**
     * 一键开启/关闭
     *
     * @return 结果
     */
    @Override
    public AjaxResult setType(String type) {
        int rows = doumuHardwareMapper.setType(type);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 根据设备id来开启关闭
     *
     * @param doumuHardware
     * @return
     */
    @Override
    public AjaxResult setTypeOpen(DoumuHardware doumuHardware) {
        int rows =  doumuHardwareMapper.setTypeOpen(doumuHardware);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }
}
