package me.zhengjie.modules.dm.printingCount.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.device.domain.DmDevice;
import me.zhengjie.modules.dm.device.service.dto.DmDeviceDto;
import me.zhengjie.modules.dm.device.service.dto.DmDeviceQueryCriteria;
import me.zhengjie.modules.dm.printingApproval.domain.DmPrintingApproval;
import me.zhengjie.modules.dm.printingApproval.service.dto.DmPrintingApprovalDto;
import me.zhengjie.modules.dm.printingCount.domain.DmPrintingCount;
import me.zhengjie.modules.dm.printingCount.repository.DmPrintingCountRrpository;
import me.zhengjie.modules.dm.printingCount.service.DmPrintingCountService;
import me.zhengjie.modules.dm.printingCount.service.dto.DmPrintingCountDto;
import me.zhengjie.modules.dm.printingCount.service.dto.DmPrintingCountQueryCriteria;
import me.zhengjie.modules.dm.printingCount.service.mapstruct.DmPrintingCountMapper;
import me.zhengjie.utils.*;
import org.junit.Test;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class DmPrintingCountServiceImpl implements DmPrintingCountService {
    private final DmPrintingCountRrpository dmPrintingCountRrpository;
    private final DmPrintingCountMapper dmPrintingCountMapper;

    @Override
    public Map<String, Object> selectList(DmPrintingCountQueryCriteria criteria, Pageable pageable) {
        Page<DmPrintingCount> page = dmPrintingCountRrpository.selectList(criteria,pageable);
        return PageUtil.toPage(page.map(dmPrintingCountMapper::toDto));
    }

    @Override
    public void download(List<DmPrintingCountDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmPrintingCountDto dmDevice : all) {
            Map<String,Object> map = new LinkedHashMap<>();
//            if (dmDevice.getCreateTime()!=null){
//                map.put("统计时段", dmDevice.getCreateTime());
//            }else {
//                map.put("统计时段", "");
//            }
            if (dmDevice.getCountUser()!=null&&dmDevice.getCountUser().getName()!=null){
                map.put("姓名", dmDevice.getCountUser().getName());
            }else {
                map.put("姓名", "");
            }
            if (dmDevice.getCountUser()!=null&&dmDevice.getCountUser().getDept()!=null&&dmDevice.getCountUser().getDept().getName()!=null){
                map.put("组织名称", dmDevice.getCountUser().getDept().getName());
            }else{
                map.put("组织名称", "");
            }
            if (dmDevice.getTaskName()!=null){
                map.put("打印文档名称", dmDevice.getTaskName());
            }else{
                map.put("打印文档名称", "");
            }
            if (dmDevice.getFileRepeatNum()!=null){
                map.put("打印份数", dmDevice.getFileRepeatNum());
            }else {
                map.put("打印份数", "");
            }
            if (dmDevice.getFilePageNum()!=null){
                map.put("打印页数", dmDevice.getFilePageNum());
            }else {
                map.put("打印页数", "");
            }
            if (dmDevice.getCountDevice()!=null&&dmDevice.getCountDevice().getName()!=null){
                map.put("打印机名称", dmDevice.getCountDevice().getName());
            }else {
                map.put("打印机名称", "");
            }
            if (dmDevice.getCountDevice()!=null&&dmDevice.getCountDevice().getCode()!=null){
                map.put("打印机编号", dmDevice.getCountDevice().getCode());
            }else {
                map.put("打印机编号", "");
            }
            if (dmDevice.getCountDevice()!=null&&dmDevice.getCountDevice().getStyle()!=null){
                map.put("打印机类型", dmDevice.getCountDevice().getStyle());
            }else {
                map.put("打印机类型", "");
            }
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Map<String, Object> queryAll(DmPrintingCountQueryCriteria criteria, Pageable pageable) {
        Page<DmPrintingCount> page = dmPrintingCountRrpository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(dmPrintingCountMapper::toDto));
    }

    @Override
    public List<DmPrintingCountDto> queryAll(DmPrintingCountQueryCriteria criteria) {
        return dmPrintingCountMapper.toDto(dmPrintingCountRrpository.findAll(((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))));
    }

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public DmPrintingCountDto create(DmPrintingCount resources) {
//        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
//        resources.setId(snowflake.nextId());
//        resources.setTaskCode(getCode());
//        return dmPrintingCountMapper.toDto(dmPrintingCountRrpository.save(resources));
//    }
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void update(DmPrintingCountDto resources) {
//        DmPrintingCount dmPrintingApproval = dmPrintingApprovalRepository.findById(resources.getId()).orElseGet(DmPrintingCount::new);
//        ValidationUtil.isNull( dmPrintingApproval.getId(),"DmPrintingCount","id",resources.getId());
//        dmPrintingApproval.copy(resources);
//        dmPrintingCountRrpository.save(dmPrintingApproval);
//    }

}
