package org.nl.acs.log.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.log.domain.Log;
import org.nl.acs.log.service.LogService;
import org.nl.acs.log.service.dto.LogDto;
import org.nl.acs.log.service.dto.LogQueryParam;
import org.nl.acs.log.service.mapper.LogMapper;
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 = LogService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class LogServiceImpl extends CommonServiceImpl<LogMapper, Log> implements LogService {

    // private final RedisUtils redisUtils;
    private final LogMapper logMapper;

    @Override
    public PageInfo<LogDto> queryAll(LogQueryParam query, Pageable pageable) {
        IPage<Log> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<Log> page = logMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, LogDto.class);
    }

    @Override
    public List<LogDto> queryAll(LogQueryParam query){
        return ConvertUtil.convertList(logMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), LogDto.class);
    }

    @Override
    public Log getById(String id) {
        return logMapper.selectById(id);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(LogDto resources) {
        Log entity = ConvertUtil.convert(resources, Log.class);
        return logMapper.insert(entity);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<String> ids){
        // delCaches(ids);
        return logMapper.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<LogDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (LogDto log : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("任务编码", log.getTaskCode());
              map.put("任务标识", log.getTaskId());
                map.put(" vehicleCode",  log.getVehicleCode());
              map.put("备注", log.getRemark());
              map.put("日志类型", log.getLogType());
              map.put("方法", log.getMethod());
              map.put("请求参数", log.getRequestparam());
              map.put("返回参数", log.getResponseparam());
              map.put("请求地址", log.getRequesturl());
              map.put("状态码", log.getStatusCode());
              map.put("是否删除", log.getIsDelete());
              map.put("创建者", log.getCreateBy());
              map.put("创建时间", log.getCreateTime());
              map.put("修改者", log.getUpdateBy());
              map.put("修改时间", log.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
