/*
*  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 me.zhengjie.modules.goods.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.goods.domain.Goods;
import me.zhengjie.modules.goods.repository.GoodsRepository;
import me.zhengjie.modules.goods.service.GoodsService;
import me.zhengjie.modules.goods.service.dto.GoodsDto;
import me.zhengjie.modules.goods.service.dto.GoodsQueryCriteria;
import me.zhengjie.modules.goods.service.mapstruct.GoodsMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;

/**
 * @website https://eladmin.vip
 * @description 服务实现
 * @author Bu
 * @date 2023-09-14
 **/
@Service
@RequiredArgsConstructor
public class GoodsServiceImpl implements GoodsService {

    private final GoodsRepository goodsRepository;
    private final GoodsMapper goodsMapper;

    @Override
    public PageResult<GoodsDto> queryAll(GoodsQueryCriteria criteria, Pageable pageable) {
        Page<Goods> page = goodsRepository.findAll(
                (root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder),
                pageable);
        return PageUtil.toPage(page.map(goodsMapper::toDto));
    }

    @Override
    public List<GoodsDto> queryAll(GoodsQueryCriteria criteria) {
        return goodsMapper.toDto(goodsRepository.findAll(
                (root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    @Transactional
    public GoodsDto findById(Integer goodsId) {
        Goods goods = goodsRepository.findById(goodsId).orElseGet(Goods::new);
        ValidationUtil.isNull(goods.getGoodsId(), "Goods", "goodsId", goodsId);
        return goodsMapper.toDto(goods);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Goods resources) {
        goodsRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Goods resources) {
        Goods goods = goodsRepository.findById(resources.getGoodsId()).orElseGet(Goods::new);
        ValidationUtil.isNull(goods.getGoodsId(), "Goods", "id", resources.getGoodsId());
        goods.copy(resources);
        goodsRepository.save(goods);
    }

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

    @Override
    public void download(List<GoodsDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (GoodsDto goods : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("货品编码", goods.getCode());
            map.put("货品名称", goods.getName());
            map.put("通用名", goods.getCommonName());
            map.put("条形码", goods.getBarcode());
            map.put("批准文号", goods.getAc());
            map.put("商品规格", goods.getSpec());
            map.put("单位", goods.getUnit());
            map.put("生产厂家", goods.getManufacturer());
            map.put("创建人", goods.getCreateBy());
            map.put("创建日期", goods.getCreateTime());
            map.put("更新人", goods.getUpdateBy());
            map.put("更新时间", goods.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}