package org.nl.acs.labeling_template.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.labeling_template.domain.LabelingTemplate;
import org.nl.acs.labeling_template.service.LabelingTemplateService;
import org.nl.acs.labeling_template.service.dto.LabelingTemplateDto;
import org.nl.acs.labeling_template.service.dto.LabelingTemplateQueryParam;
import org.nl.acs.labeling_template.service.mapper.LabelingTemplateMapper;
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 = LabelingTemplateService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class LabelingTemplateServiceImpl extends CommonServiceImpl<LabelingTemplateMapper, LabelingTemplate> implements LabelingTemplateService {

    // private final RedisUtils redisUtils;
    private final LabelingTemplateMapper labelingTemplateMapper;

    @Override
    public PageInfo<LabelingTemplateDto> queryAll(LabelingTemplateQueryParam query, Pageable pageable) {
        IPage<LabelingTemplate> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<LabelingTemplate> page = labelingTemplateMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, LabelingTemplateDto.class);
    }

    @Override
    public List<LabelingTemplateDto> queryAll(LabelingTemplateQueryParam query){
        return ConvertUtil.convertList(labelingTemplateMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), LabelingTemplateDto.class);
    }

    @Override
    public LabelingTemplate getById(String id) {
        return labelingTemplateMapper.selectById(id);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(LabelingTemplateDto resources) {
        LabelingTemplate entity = ConvertUtil.convert(resources, LabelingTemplate.class);
        return labelingTemplateMapper.insert(entity);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<String> ids){
        // delCaches(ids);
        return labelingTemplateMapper.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<LabelingTemplateDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (LabelingTemplateDto labelingTemplate : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("模板编码", labelingTemplate.getTemplateCode());
              map.put("模板名称 ", labelingTemplate.getTemplateName());
              map.put("模板地址", labelingTemplate.getTemplateAddress());
              map.put("创建者", labelingTemplate.getCreateBy());
              map.put("创建时间", labelingTemplate.getCreateTime());
              map.put("修改者", labelingTemplate.getUpdateBy());
              map.put("修改时间", labelingTemplate.getUpdateTime());
              map.put("启用时间", labelingTemplate.getIsUsedTime());
              map.put("是否启用", labelingTemplate.getIsUsed());
              map.put("是否删除", labelingTemplate.getIsDelete());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
