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.DeviceRunpoint;
import org.nl.acs.device.service.DeviceRunpointService;
import org.nl.acs.device.service.dto.DeviceRunpointDto;
import org.nl.acs.device.service.dto.DeviceRunpointQueryParam;
import org.nl.acs.device.service.mapper.DeviceRunpointMapper;
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 = DeviceRunpointService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DeviceRunpointServiceImpl extends CommonServiceImpl<DeviceRunpointMapper, DeviceRunpoint> implements DeviceRunpointService {

    // private final RedisUtils redisUtils;
    private final DeviceRunpointMapper deviceRunpointMapper;

    @Override
    public PageInfo<DeviceRunpointDto> queryAll(DeviceRunpointQueryParam query, Pageable pageable) {
        IPage<DeviceRunpoint> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<DeviceRunpoint> page = deviceRunpointMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, DeviceRunpointDto.class);
    }

    @Override
    public List<DeviceRunpointDto> queryAll(DeviceRunpointQueryParam query){
        return ConvertUtil.convertList(deviceRunpointMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), DeviceRunpointDto.class);
    }

    @Override
    public DeviceRunpoint getById(String id) {
        return deviceRunpointMapper.selectById(id);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(DeviceRunpointDto resources) {
        DeviceRunpoint entity = ConvertUtil.convert(resources, DeviceRunpoint.class);
        return deviceRunpointMapper.insert(entity);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<String> ids){
        // delCaches(ids);
        return deviceRunpointMapper.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<DeviceRunpointDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (DeviceRunpointDto deviceRunpoint : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("设备编码", deviceRunpoint.getDeviceCode());
              map.put("有货状态", deviceRunpoint.getHasgoods());
              map.put("载具号", deviceRunpoint.getContainer());
              map.put("任务号", deviceRunpoint.getTaskno());
              map.put("备注", deviceRunpoint.getRemark());
              map.put("是否启用", deviceRunpoint.getIsActive());
              map.put("是否删除", deviceRunpoint.getIsDelete());
              map.put("创建者", deviceRunpoint.getCreateBy());
              map.put("创建时间", deviceRunpoint.getCreateTime());
              map.put("修改者", deviceRunpoint.getUpdateBy());
              map.put("修改时间", deviceRunpoint.getUpdateTime());
              map.put("物料类型", deviceRunpoint.getMaterialType());
              map.put("批次", deviceRunpoint.getBatch());
              map.put("锁定", deviceRunpoint.getIslock());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
