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.DeviceDbitem;
import org.nl.acs.device.service.DeviceDbitemService;
import org.nl.acs.device.service.dto.DeviceDbitemDto;
import org.nl.acs.device.service.dto.DeviceDbitemQueryParam;
import org.nl.acs.device.service.mapper.DeviceDbitemMapper;
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 = DeviceDbitemService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DeviceDbitemServiceImpl extends CommonServiceImpl<DeviceDbitemMapper, DeviceDbitem> implements DeviceDbitemService {

    // private final RedisUtils redisUtils;
    private final DeviceDbitemMapper deviceDbitemMapper;

    @Override
    public PageInfo<DeviceDbitemDto> queryAll(DeviceDbitemQueryParam query, Pageable pageable) {
        IPage<DeviceDbitem> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<DeviceDbitem> page = deviceDbitemMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, DeviceDbitemDto.class);
    }

    @Override
    public List<DeviceDbitemDto> queryAll(DeviceDbitemQueryParam query){
        return ConvertUtil.convertList(deviceDbitemMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), DeviceDbitemDto.class);
    }

    @Override
    public DeviceDbitem getById(String id) {
        return deviceDbitemMapper.selectById(id);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(DeviceDbitemDto resources) {
        DeviceDbitem entity = ConvertUtil.convert(resources, DeviceDbitem.class);
        return deviceDbitemMapper.insert(entity);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<String> ids){
        // delCaches(ids);
        return deviceDbitemMapper.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<DeviceDbitemDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (DeviceDbitemDto deviceDbitem : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("项编码", deviceDbitem.getItemCode());
              map.put("项名字", deviceDbitem.getItemName());
              map.put("DB块编码", deviceDbitem.getDbCode());
              map.put("DB块值", deviceDbitem.getDbValue());
              map.put("值类型", deviceDbitem.getValueType());
              map.put("采集类型", deviceDbitem.getCollectType());
              map.put("设备标识", deviceDbitem.getDeviceId());
              map.put("备注", deviceDbitem.getRemark());
              map.put("是否启用", deviceDbitem.getIsActive());
              map.put("是否删除", deviceDbitem.getIsDelete());
              map.put("创建者", deviceDbitem.getCreateBy());
              map.put("创建时间", deviceDbitem.getCreateTime());
              map.put("修改者", deviceDbitem.getUpdateBy());
              map.put("修改时间", deviceDbitem.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
