package org.nl.acs.materialbase.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.materialbase.domain.Materialbase;
import org.nl.acs.materialbase.service.MaterialbaseService;
import org.nl.acs.materialbase.service.dto.MaterialbaseDto;
import org.nl.acs.materialbase.service.dto.MaterialbaseQueryParam;
import org.nl.acs.materialbase.service.mapper.MaterialbaseMapper;
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 = MaterialbaseService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MaterialbaseServiceImpl extends CommonServiceImpl<MaterialbaseMapper, Materialbase> implements MaterialbaseService {

    // private final RedisUtils redisUtils;
    private final MaterialbaseMapper materialbaseMapper;

    @Override
    public PageInfo<MaterialbaseDto> queryAll(MaterialbaseQueryParam query, Pageable pageable) {
        IPage<Materialbase> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<Materialbase> page = materialbaseMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, MaterialbaseDto.class);
    }

    @Override
    public List<MaterialbaseDto> queryAll(MaterialbaseQueryParam query){
        return ConvertUtil.convertList(materialbaseMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), MaterialbaseDto.class);
    }

    @Override
    public Materialbase getById(String id) {
        return materialbaseMapper.selectById(id);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(MaterialbaseDto resources) {
        Materialbase entity = ConvertUtil.convert(resources, Materialbase.class);
        return materialbaseMapper.insert(entity);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<String> ids){
        // delCaches(ids);
        return materialbaseMapper.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<MaterialbaseDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (MaterialbaseDto materialbase : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("物料编码", materialbase.getMaterialCode());
              map.put("物料名称 ", materialbase.getMaterialName());
              map.put("规格", materialbase.getMaterialSpec());
              map.put("型号", materialbase.getMaterialModel());
              map.put("基本计量单位", materialbase.getBaseUnitId());
              map.put("工程图号", materialbase.getPrintNo());
              map.put("长度单位", materialbase.getLenUnitId());
              map.put("物料长度", materialbase.getLength());
              map.put("物料宽度", materialbase.getWidth());
              map.put("物料高度", materialbase.getHeight());
              map.put("重量单位", materialbase.getWeightUnitId());
              map.put("物料毛重", materialbase.getGrossWeight());
              map.put("物料净重", materialbase.getNetWeight());
              map.put("体积单位", materialbase.getCubageUnitId());
              map.put("物料体积", materialbase.getCubage());
              map.put("外径", materialbase.getOuterDiameter());
              map.put("壁厚", materialbase.getWallThickness());
              map.put("创建者", materialbase.getCreateBy());
              map.put("创建时间", materialbase.getCreateTime());
              map.put("修改者", materialbase.getUpdateBy());
              map.put("修改时间", materialbase.getUpdateTime());
              map.put("启用时间", materialbase.getIsUsedTime());
              map.put("是否启用", materialbase.getIsUsed());
              map.put("是否删除", materialbase.getIsDelete());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
