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

import cn.ysk.cashier.cons.domain.ViewConSku;
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.ViewConSkuRepository;
import cn.ysk.cashier.cons.service.ViewConSkuService;
import cn.ysk.cashier.cons.service.dto.ViewConSkuDto;
import cn.ysk.cashier.cons.service.dto.ViewConSkuQueryCriteria;
import cn.ysk.cashier.cons.service.mapstruct.ViewConSkuMapper;
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 ViewConSkuServiceImpl implements ViewConSkuService {

    private final ViewConSkuRepository viewConSkuRepository;
    private final ViewConSkuMapper viewConSkuMapper;

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

    @Override
    public List<ViewConSkuDto> queryAll(ViewConSkuQueryCriteria criteria){
        return viewConSkuMapper.toDto(viewConSkuRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ViewConSkuDto findById(Integer id) {
        ViewConSku viewConSku = viewConSkuRepository.findById(id).orElseGet(ViewConSku::new);
        ValidationUtil.isNull(viewConSku.getId(),"ViewConSku","id",id);
        return viewConSkuMapper.toDto(viewConSku);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ViewConSkuDto create(ViewConSku resources) {
        return viewConSkuMapper.toDto(viewConSkuRepository.save(resources));
    }

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

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

    @Override
    public void download(List<ViewConSkuDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ViewConSkuDto viewConSku : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("耗材id", viewConSku.getConInfoId());
            map.put("商品规格id", viewConSku.getProductSkuId());
            map.put("耗材名称", viewConSku.getConName());
            map.put("规格", viewConSku.getSpecSnap());
            map.put("店铺id", viewConSku.getShopId());
            map.put("状态 1 启用 0 禁用", viewConSku.getStatus());
            map.put("耗材代码", viewConSku.getConCode());
            map.put("单位值", viewConSku.getConUnit());
            map.put("库存值", viewConSku.getStockNumber());
            map.put("每份消耗", viewConSku.getSurplusStock());
            map.put("标签详情", viewConSku.getSpecInfo());
            map.put("商品名称", viewConSku.getName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}