package cn.ysk.cashier.service.shop;

import cn.ysk.cashier.dto.shop.TbCouponCategoryDto;
import cn.ysk.cashier.dto.shop.TbCouponCategoryQueryCriteria;
import cn.ysk.cashier.mapper.shop.TbCouponCategoryMapper;
import cn.ysk.cashier.pojo.shop.TbCouponCategory;
import cn.ysk.cashier.repository.shop.TbCouponCategoryRepository;
import cn.ysk.cashier.utils.FileUtil;
import cn.ysk.cashier.utils.PageUtil;
import cn.ysk.cashier.utils.QueryHelp;
import cn.ysk.cashier.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Pageable;

import java.sql.Timestamp;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

@Service
@RequiredArgsConstructor
public class TbCouponCategoryService {
    private final TbCouponCategoryRepository tbCouponCategoryRepository;
    private final TbCouponCategoryMapper tbCouponCategoryMapper;

    
    public Map<String,Object> queryAll(TbCouponCategoryQueryCriteria criteria, Pageable pageable){
        Page<TbCouponCategory> page = tbCouponCategoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(tbCouponCategoryMapper::toDto));
    }

    
    public List<TbCouponCategoryDto> queryAll(TbCouponCategoryQueryCriteria criteria){
        return tbCouponCategoryMapper.toDto(tbCouponCategoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    
    @Transactional
    public TbCouponCategoryDto findById(Integer id) {
        TbCouponCategory tbCouponCategory = tbCouponCategoryRepository.findById(id).orElseGet(TbCouponCategory::new);
        ValidationUtil.isNull(tbCouponCategory.getId(),"TbCouponCategory","id",id);
        return tbCouponCategoryMapper.toDto(tbCouponCategory);
    }

    
    @Transactional(rollbackFor = Exception.class)
    public TbCouponCategoryDto create(TbCouponCategory resources) {
        resources.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return tbCouponCategoryMapper.toDto(tbCouponCategoryRepository.save(resources));
    }

    
    @Transactional(rollbackFor = Exception.class)
    public void update(TbCouponCategory resources) {
        TbCouponCategory tbCouponCategory = tbCouponCategoryRepository.findById(resources.getId()).orElseGet(TbCouponCategory::new);
        ValidationUtil.isNull( tbCouponCategory.getId(),"TbCouponCategory","id",resources.getId());
        tbCouponCategory.copy(resources);
        tbCouponCategory.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        tbCouponCategoryRepository.save(tbCouponCategory);
    }

    
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            tbCouponCategoryRepository.deleteById(id);
        }
    }

    
    public void download(List<TbCouponCategoryDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbCouponCategoryDto tbCouponCategory : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("分类名称", tbCouponCategory.getName());
            map.put(" createTime",  tbCouponCategory.getCreateTime());
            map.put(" updateTime",  tbCouponCategory.getUpdateTime());
            map.put("0：不展示；1：展示；", tbCouponCategory.getStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
