/*
*  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.store.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.store.domain.Store;
import me.zhengjie.modules.store.repository.StoreRepository;
import me.zhengjie.modules.store.service.StoreService;
import me.zhengjie.modules.store.service.dto.StoreDto;
import me.zhengjie.modules.store.service.dto.StoreQueryCriteria;
import me.zhengjie.modules.store.service.mapstruct.StoreMapper;
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-13
 **/
@Service
@RequiredArgsConstructor
public class StoreServiceImpl implements StoreService {

    private final StoreRepository storeRepository;
    private final StoreMapper storeMapper;

    @Override
    public PageResult<StoreDto> queryAll(StoreQueryCriteria criteria, Pageable pageable) {
        Page<Store> page = storeRepository.findAll(
                (root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder),
                pageable);
        return PageUtil.toPage(page.map(storeMapper::toDto));
    }

    @Override
    public List<StoreDto> queryAll(StoreQueryCriteria criteria) {
        return storeMapper.toDto(storeRepository.findAll(
                (root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    @Transactional
    public StoreDto findById(Integer id) {
        Store store = storeRepository.findById(id).orElseGet(Store::new);
        ValidationUtil.isNull(store.getId(), "Store", "id", id);
        return storeMapper.toDto(store);
    }

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

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

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

    @Override
    public void download(List<StoreDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (StoreDto store : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("门店编码", store.getCode());
            map.put("门店名称", store.getName());
            map.put("创建人", store.getCreateBy());
            map.put("创建日期", store.getCreateTime());
            map.put("更新人", store.getUpdateBy());
            map.put("更新时间", store.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<StoreDto> getStores(Integer pid) {
        List<Store> stores;
        if (pid != null && !pid.equals(0)) {
            stores = storeRepository.findByPid(pid);
        } else {
            stores = storeRepository.findByPidIsNull();
        }
        return storeMapper.toDto(stores);
    }
}