package org.nl.acs.produceshiftorderdetail.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.produceshiftorderdetail.domain.Produceshiftorderdetail;
import org.nl.acs.produceshiftorderdetail.service.ProduceshiftorderdetailService;
import org.nl.acs.produceshiftorderdetail.service.dto.ProduceshiftorderdetailDto;
import org.nl.acs.produceshiftorderdetail.service.dto.ProduceshiftorderdetailQueryParam;
import org.nl.acs.produceshiftorderdetail.service.mapper.ProduceshiftorderdetailMapper;
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 = ProduceshiftorderdetailService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProduceshiftorderdetailServiceImpl extends CommonServiceImpl<ProduceshiftorderdetailMapper, Produceshiftorderdetail> implements ProduceshiftorderdetailService {

    // private final RedisUtils redisUtils;
    private final ProduceshiftorderdetailMapper produceshiftorderdetailMapper;

    @Override
    public PageInfo<ProduceshiftorderdetailDto> queryAll(ProduceshiftorderdetailQueryParam query, Pageable pageable) {
        IPage<Produceshiftorderdetail> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<Produceshiftorderdetail> page = produceshiftorderdetailMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, ProduceshiftorderdetailDto.class);
    }

    @Override
    public List<ProduceshiftorderdetailDto> queryAll(ProduceshiftorderdetailQueryParam query){
        return ConvertUtil.convertList(produceshiftorderdetailMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), ProduceshiftorderdetailDto.class);
    }

    @Override
    public Produceshiftorderdetail getById(String id) {
        return produceshiftorderdetailMapper.selectById(id);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(ProduceshiftorderdetailDto resources) {
        Produceshiftorderdetail entity = ConvertUtil.convert(resources, Produceshiftorderdetail.class);
        return produceshiftorderdetailMapper.insert(entity);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<String> ids){
        // delCaches(ids);
        return produceshiftorderdetailMapper.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<ProduceshiftorderdetailDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (ProduceshiftorderdetailDto produceshiftorderdetail : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("工单明细编码", produceshiftorderdetail.getOrderdetailCode());
              map.put("工单明细类型", produceshiftorderdetail.getOrderdetailType());
              map.put("优先级", produceshiftorderdetail.getPriority());
              map.put("顺序号", produceshiftorderdetail.getSequenceNumber());
              map.put("工单标识", produceshiftorderdetail.getOrderId());
              map.put("工单编码", produceshiftorderdetail.getOrderCode());
              map.put("客户信息", produceshiftorderdetail.getCustomerName());
              map.put("客户编码", produceshiftorderdetail.getCustomerCode());
              map.put("明细状态", produceshiftorderdetail.getOrderStatus());
              map.put("工单数量", produceshiftorderdetail.getOrderQty());
              map.put("刻字信息", produceshiftorderdetail.getLetteringMessage());
              map.put("当前刻字数量", produceshiftorderdetail.getPresentLetteringNumer());
              map.put("贴标完成时间", produceshiftorderdetail.getLabelingFinishTime());
              map.put("创建者", produceshiftorderdetail.getCreateBy());
              map.put("创建时间", produceshiftorderdetail.getCreateTime());
              map.put("捆扎完成时间", produceshiftorderdetail.getStarpFinishTime());
              map.put("裹膜数量", produceshiftorderdetail.getWrapingNumber());
              map.put("当前裹膜数量", produceshiftorderdetail.getPresentWrapingNumber());
              map.put("裹膜合格数量", produceshiftorderdetail.getQualifiedWrapingNumber());
              map.put("裹膜完成时间", produceshiftorderdetail.getWrapingFinishTime());
              map.put("贴标信息", produceshiftorderdetail.getLabelingMessage());
              map.put("当前贴标数量", produceshiftorderdetail.getPresentLabelingNumber());
              map.put("刻字合格数量", produceshiftorderdetail.getQualifiedLetteringNumer());
              map.put("刻字完成时间", produceshiftorderdetail.getLetteringFinishTime());
              map.put("当前套冒数量", produceshiftorderdetail.getPresentSleeveingNumer());
              map.put("套冒合格数量", produceshiftorderdetail.getQualifiedSleeveingNumber());
              map.put("套冒完成时间", produceshiftorderdetail.getSleeveingFinishTime());
              map.put("捆扎包数", produceshiftorderdetail.getStrapPackNumber());
              map.put("每捆数量", produceshiftorderdetail.getStrapNumber());
              map.put("捆扎尾料数量", produceshiftorderdetail.getStrapTailintNumber());
              map.put("捆扎直径", produceshiftorderdetail.getStarpDiameter());
              map.put("捆扎尾料直径", produceshiftorderdetail.getStarpTailintDiameter());
              map.put("当前捆扎包数", produceshiftorderdetail.getPresentStrapPackNumber());
              map.put("修改者", produceshiftorderdetail.getUpdateBy());
              map.put("修改时间", produceshiftorderdetail.getUpdateTime());
              map.put("是否删除", produceshiftorderdetail.getIsDeleted());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
