package cn.ysk.cashier.cons.service.impl;

import cn.ysk.cashier.cons.domain.ViewProductSkuShop;
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 cn.ysk.cashier.cons.repository.ViewProductSkuShopRepository;
import cn.ysk.cashier.cons.service.ViewProductSkuShopService;
import cn.ysk.cashier.cons.service.dto.ViewProductSkuShopDto;
import cn.ysk.cashier.cons.service.dto.ViewProductSkuShopQueryCriteria;
import cn.ysk.cashier.cons.service.mapstruct.ViewProductSkuShopMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author admin
* @date 2024-06-25
**/
@Service
@RequiredArgsConstructor
public class ViewProductSkuShopServiceImpl implements ViewProductSkuShopService {

    private final ViewProductSkuShopRepository viewProductSkuShopRepository;
    private final ViewProductSkuShopMapper viewProductSkuShopMapper;

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

    @Override
    public List<ViewProductSkuShopDto> queryAll(ViewProductSkuShopQueryCriteria criteria){
        return viewProductSkuShopMapper.toDto(viewProductSkuShopRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ViewProductSkuShopDto findById(Integer id) {
        ViewProductSkuShop viewProductSkuShop = viewProductSkuShopRepository.findById(id).orElseGet(ViewProductSkuShop::new);
        ValidationUtil.isNull(viewProductSkuShop.getId(),"ViewProductSkuShop","id",id);
        return viewProductSkuShopMapper.toDto(viewProductSkuShop);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ViewProductSkuShopDto create(ViewProductSkuShop resources) {
        return viewProductSkuShopMapper.toDto(viewProductSkuShopRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ViewProductSkuShop resources) {
        ViewProductSkuShop viewProductSkuShop = viewProductSkuShopRepository.findById(resources.getId()).orElseGet(ViewProductSkuShop::new);
        ValidationUtil.isNull( viewProductSkuShop.getId(),"ViewProductSkuShop","id",resources.getId());
        viewProductSkuShop.copy(resources);
        viewProductSkuShopRepository.save(viewProductSkuShop);
    }

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

    @Override
    public void download(List<ViewProductSkuShopDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ViewProductSkuShopDto viewProductSkuShop : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("规格", viewProductSkuShop.getSpecSnap());
            map.put("商品名称", viewProductSkuShop.getName());
            map.put("店铺名称", viewProductSkuShop.getShopName());
            map.put("自增id", viewProductSkuShop.getShopId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}