/*
*  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.TbProductSku;
import cn.ysk.cashier.dto.product.TbProductSkuQueryCriteria;
import cn.ysk.cashier.mapper.product.TbProductSkuMapper;
import cn.ysk.cashier.utils.ValidationUtil;
import cn.ysk.cashier.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.repository.product.TbProductSkuRepository;
import cn.ysk.cashier.service.product.TbProductSkuService;
import cn.ysk.cashier.dto.product.TbProductSkuDto;
import lombok.extern.slf4j.Slf4j;
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-01-03
**/
@Slf4j
@Service
@RequiredArgsConstructor
public class TbProductSkuServiceImpl implements TbProductSkuService {

    private final TbProductSkuRepository tbProductSkuRepository;
    private final TbProductSkuMapper tbProductSkuMapper;

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

    @Override
    public List<TbProductSkuDto> queryAll(TbProductSkuQueryCriteria criteria){
        return tbProductSkuMapper.toDto(tbProductSkuRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbProductSkuDto findById(Integer id) {
        TbProductSku tbProductSku = tbProductSkuRepository.findById(id).orElseGet(TbProductSku::new);
        ValidationUtil.isNull(tbProductSku.getId(),"TbProductSku","id",id);
        return tbProductSkuMapper.toDto(tbProductSku);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbProductSkuDto create(TbProductSku resources) {
        return tbProductSkuMapper.toDto(tbProductSkuRepository.save(resources));
    }

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

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

    @Override
    public void download(List<TbProductSkuDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbProductSkuDto tbProductSku : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" shopId",  tbProductSku.getShopId());
            map.put("条形码", tbProductSku.getBarCode());
            map.put("商品Id", tbProductSku.getProductId());
            map.put("原价", tbProductSku.getOriginPrice());
            map.put("成本价", tbProductSku.getCostPrice());
            map.put("会员价", tbProductSku.getMemberPrice());
            map.put(" mealPrice",  tbProductSku.getMealPrice());
            map.put("售价", tbProductSku.getSalePrice());
            map.put("进货参考价", tbProductSku.getGuidePrice());
            map.put(" strategyPrice",  tbProductSku.getStrategyPrice());
            map.put("库存数量", tbProductSku.getStockNumber());
            map.put("标签镜像", tbProductSku.getSpecSnap());
            map.put(" coverImg",  tbProductSku.getCoverImg());
            map.put("库存警戒线", tbProductSku.getWarnLine());
            map.put(" weight",  tbProductSku.getWeight());
            map.put(" volume",  tbProductSku.getVolume());
            map.put("销量", tbProductSku.getRealSalesNumber());
            map.put("一级分销金额", tbProductSku.getFirstShared());
            map.put("二级分销金额", tbProductSku.getSecondShared());
            map.put(" createdAt",  tbProductSku.getCreatedAt());
            map.put(" updatedAt",  tbProductSku.getUpdatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}