/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package cn.ysk.cashier.service.impl.productimpl;

import cn.ysk.cashier.pojo.product.TbProductSkuResult;
import cn.ysk.cashier.utils.ValidationUtil;
import cn.ysk.cashier.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.repository.product.TbProductSkuResultRepository;
import cn.ysk.cashier.service.product.TbProductSkuResultService;
import cn.ysk.cashier.dto.product.TbProductSkuResultDto;
import cn.ysk.cashier.dto.product.TbProductSkuResultQueryCriteria;
import cn.ysk.cashier.repository.product.TbProductSkuResultMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import cn.ysk.cashier.utils.PageUtil;
import cn.ysk.cashier.utils.QueryHelp;
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 lyf
* @date 2024-02-08
**/
@Service
@RequiredArgsConstructor
public class TbProductSkuResultServiceImpl implements TbProductSkuResultService {

    private final TbProductSkuResultRepository tbProductSkuResultRepository;
    private final TbProductSkuResultMapper tbProductSkuResultMapper;

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

    @Override
    public List<TbProductSkuResultDto> queryAll(TbProductSkuResultQueryCriteria criteria){
        return tbProductSkuResultMapper.toDto(tbProductSkuResultRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbProductSkuResultDto findById(Integer id) {
        TbProductSkuResult tbProductSkuResult = tbProductSkuResultRepository.findById(id).orElseGet(TbProductSkuResult::new);
        ValidationUtil.isNull(tbProductSkuResult.getId(),"TbProductSkuResult","id",id);
        return tbProductSkuResultMapper.toDto(tbProductSkuResult);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbProductSkuResultDto create(TbProductSkuResult resources) {
        return tbProductSkuResultMapper.toDto(tbProductSkuResultRepository.save(resources));
    }

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

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

    @Override
    public void download(List<TbProductSkuResultDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbProductSkuResultDto tbProductSkuResult : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("商户Id", tbProductSkuResult.getMerchantId());
            map.put("规格id", tbProductSkuResult.getSpecId());
            map.put("标签列表，包括名称，显示", tbProductSkuResult.getTagSnap());
            map.put(" createdAt",  tbProductSkuResult.getCreatedAt());
            map.put(" updatedAt",  tbProductSkuResult.getUpdatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}