package com.agri.system.service.impl;

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

import com.agri.common.core.domain.AjaxResult;
import com.agri.common.utils.DateUtils;
import com.agri.system.domain.SysAgriLimitBak;
import com.agri.system.domain.SysDtuRemark;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.agri.system.mapper.SysDtuRemarkBakMapper;
import com.agri.system.domain.SysDtuRemarkBak;
import com.agri.system.service.ISysDtuRemarkBakService;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * dtu设备备注Service业务层处理
 * 
 * @author lld
 * @date 2026-03-17
 */
@Service
public class SysDtuRemarkBakServiceImpl extends ServiceImpl<SysDtuRemarkBakMapper, SysDtuRemarkBak> implements ISysDtuRemarkBakService 
{

    /**
     * 查询dtu设备备注
     * 
     * @param id dtu设备备注主键
     * @return dtu设备备注
     */
    @Override
    public SysDtuRemarkBak selectSysDtuRemarkBakById(String id)
    {
        return baseMapper.selectSysDtuRemarkBakById(id);
    }

    /**
     * 查询dtu设备备注列表
     * 
     * @param sysDtuRemarkBak dtu设备备注
     * @return dtu设备备注
     */
    @Override
    public List<SysDtuRemarkBak> selectSysDtuRemarkBakList(SysDtuRemarkBak sysDtuRemarkBak)
    {
        return baseMapper.selectSysDtuRemarkBakList(sysDtuRemarkBak);
    }

    /**
     * 新增dtu设备备注
     * 
     * @param sysDtuRemarkBak dtu设备备注
     * @return 结果
     */
    @Override
    public int insertSysDtuRemarkBak(SysDtuRemarkBak sysDtuRemarkBak)
    {
        sysDtuRemarkBak.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertSysDtuRemarkBak(sysDtuRemarkBak);
    }

    /**
     * 修改dtu设备备注
     * 
     * @param sysDtuRemarkBak dtu设备备注
     * @return 结果
     */
    @Override
    public int updateSysDtuRemarkBak(SysDtuRemarkBak sysDtuRemarkBak)
    {
        sysDtuRemarkBak.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateSysDtuRemarkBak(sysDtuRemarkBak);
    }

    /**
     * 批量删除dtu设备备注
     * 
     * @param ids 需要删除的dtu设备备注主键
     * @return 结果
     */
    @Override
    public int deleteSysDtuRemarkBakByIds(String[] ids)
    {
        return baseMapper.deleteSysDtuRemarkBakByIds(ids);
    }

    /**
     * 删除dtu设备备注信息
     * 
     * @param id dtu设备备注主键
     * @return 结果
     */
    @Override
    public int deleteSysDtuRemarkBakById(String id)
    {
        return baseMapper.deleteSysDtuRemarkBakById(id);
    }

    @Override
    public SysDtuRemarkBak getDtuRemarkByImei(String imei) {

        SysDtuRemarkBak dtuRemarkBak = new SysDtuRemarkBak();
        if (StringUtils.isEmpty(imei)) return dtuRemarkBak;
        List<SysDtuRemarkBak> remarkList = super.lambdaQuery()
                .select(
                        SysDtuRemarkBak::getImei,
                        SysDtuRemarkBak::getRoller,
                        SysDtuRemarkBak::getRemarkShort
                )
                .eq(SysDtuRemarkBak::getImei, imei)
                .list();
        if (CollectionUtils.isEmpty(remarkList)) return dtuRemarkBak;
        SysDtuRemarkBak sysDtuRemarkBak = remarkList.get(0);
        dtuRemarkBak.setImei(sysDtuRemarkBak.getImei());
        HashMap<String, String> map = new HashMap<>();
        for (SysDtuRemarkBak dtuRemark : remarkList) {
            map.put(dtuRemark.getRoller(), dtuRemark.getRemarkShort());
        }
        dtuRemarkBak.setRemarkMaps(map);
        return dtuRemarkBak;
    }

    @Override
    public boolean saveAgriRemark(SysDtuRemarkBak sysDtuRemarkBak) {

        Map<String, Object> map = new HashMap<>();
        map.put("imei", sysDtuRemarkBak.getImei());
        map.put("roller",sysDtuRemarkBak.getRoller());
        boolean remove = super.removeByMap(map);
        return super.save(sysDtuRemarkBak);
    }
}
