package org.nl.acs.device.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.nl.acs.common.base.PageInfo;
import org.nl.acs.common.base.QueryHelpMybatisPlus;
import org.nl.acs.common.base.impl.CommonServiceImpl;
import org.nl.acs.utils.ConvertUtil;
import org.nl.acs.utils.PageUtil;
import org.nl.acs.device.domain.Deviceisonline;
import org.nl.acs.device.service.DeviceisonlineService;
import org.nl.acs.device.service.dto.DeviceisonlineDto;
import org.nl.acs.device.service.dto.DeviceisonlineQueryParam;
import org.nl.acs.device.service.mapper.DeviceisonlineMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author jiaolm
* @date 2023-05-09
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = DeviceisonlineService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DeviceisonlineServiceImpl extends CommonServiceImpl<DeviceisonlineMapper, Deviceisonline> implements DeviceisonlineService {

    // private final RedisUtils redisUtils;
    private final DeviceisonlineMapper deviceisonlineMapper;

    @Override
    public PageInfo<DeviceisonlineDto> queryAll(DeviceisonlineQueryParam query, Pageable pageable) {
        IPage<Deviceisonline> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<Deviceisonline> page = deviceisonlineMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, DeviceisonlineDto.class);
    }

    @Override
    public List<DeviceisonlineDto> queryAll(DeviceisonlineQueryParam query){
        return ConvertUtil.convertList(deviceisonlineMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), DeviceisonlineDto.class);
    }

    @Override
    public Deviceisonline getById(String id) {
        return deviceisonlineMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public DeviceisonlineDto findById(String id) {
        return ConvertUtil.convert(getById(id), DeviceisonlineDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(DeviceisonlineDto resources) {
        Deviceisonline entity = ConvertUtil.convert(resources, Deviceisonline.class);
        return deviceisonlineMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(DeviceisonlineDto resources){
        Deviceisonline entity = ConvertUtil.convert(resources, Deviceisonline.class);
        int ret = deviceisonlineMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<String> ids){
        // delCaches(ids);
        return deviceisonlineMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(String id){
        Set<String> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(String id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<String> ids) {
        for (String id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<DeviceisonlineDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (DeviceisonlineDto deviceisonline : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("设备编码", deviceisonline.getDeviceCode());
              map.put("订单编码", deviceisonline.getOrderCode());
              map.put("在线状态", deviceisonline.getOnlineStatus());
              map.put("创建者", deviceisonline.getCreateBy());
              map.put("创建时间", deviceisonline.getCreateTime());
              map.put("修改者", deviceisonline.getUpdateBy());
              map.put("修改时间", deviceisonline.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
