package org.nl.acs.produceshiftorder.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.produceshiftorder.domain.Produceshiftorder;
import org.nl.acs.produceshiftorder.service.ProduceshiftorderService;
import org.nl.acs.produceshiftorder.service.dto.ProduceshiftorderDto;
import org.nl.acs.produceshiftorder.service.dto.ProduceshiftorderQueryParam;
import org.nl.acs.produceshiftorder.service.mapper.ProduceshiftorderMapper;
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 = ProduceshiftorderService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProduceshiftorderServiceImpl extends CommonServiceImpl<ProduceshiftorderMapper, Produceshiftorder> implements ProduceshiftorderService {

    // private final RedisUtils redisUtils;
    private final ProduceshiftorderMapper produceshiftorderMapper;

    @Override
    public PageInfo<ProduceshiftorderDto> queryAll(ProduceshiftorderQueryParam query, Pageable pageable) {
        IPage<Produceshiftorder> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<Produceshiftorder> page = produceshiftorderMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, ProduceshiftorderDto.class);
    }

    @Override
    public List<ProduceshiftorderDto> queryAll(ProduceshiftorderQueryParam query){
        return ConvertUtil.convertList(produceshiftorderMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), ProduceshiftorderDto.class);
    }

    @Override
    public Produceshiftorder getById(String id) {
        return produceshiftorderMapper.selectById(id);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(ProduceshiftorderDto resources) {
        Produceshiftorder entity = ConvertUtil.convert(resources, Produceshiftorder.class);
        return produceshiftorderMapper.insert(entity);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<String> ids){
        // delCaches(ids);
        return produceshiftorderMapper.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<ProduceshiftorderDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (ProduceshiftorderDto produceshiftorder : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("工单编码", produceshiftorder.getOrderCode());
              map.put("设备编码", produceshiftorder.getDeviceCode());
              map.put("工单状态", produceshiftorder.getOrderStatus());
              map.put("下料数量", produceshiftorder.getQty());
              map.put("产品编码", produceshiftorder.getProductCode());
              map.put("物料编码", produceshiftorder.getMaterialCode());
              map.put("物料名称", produceshiftorder.getMaterialName());
              map.put("物料标识", produceshiftorder.getMaterialUuid());
              map.put("客户编码", produceshiftorder.getCustCode());
              map.put("客户名称", produceshiftorder.getCustName());
              map.put("型号", produceshiftorder.getModel());
              map.put("熔池", produceshiftorder.getMoltenPool());
              map.put("重量", produceshiftorder.getWeight());
              map.put("创建者", produceshiftorder.getCreateBy());
              map.put("修改者", produceshiftorder.getUpdateBy());
              map.put("修改时间", produceshiftorder.getUpdateTime());
              map.put("是否删除", produceshiftorder.getIsDeleted());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
