/*
*  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.shopimpl;

import cn.ysk.cashier.mapper.shop.TbShopAreaMapper;
import cn.ysk.cashier.pojo.shop.TbShopArea;
import cn.ysk.cashier.utils.ValidationUtil;
import cn.ysk.cashier.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.repository.shop.TbShopAreaRepository;
import cn.ysk.cashier.service.shop.TbShopAreaService;
import cn.ysk.cashier.dto.shop.TbShopAreaDto;
import cn.ysk.cashier.dto.shop.TbShopAreaQueryCriteria;
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.time.Instant;
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-31
**/
@Service
@RequiredArgsConstructor
public class TbShopAreaServiceImpl implements TbShopAreaService {

    private final TbShopAreaRepository tbShopAreaRepository;
    private final TbShopAreaMapper tbShopAreaMapper;

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

    @Override
    public List<TbShopAreaDto> queryAll(TbShopAreaQueryCriteria criteria){
        return tbShopAreaMapper.toDto(tbShopAreaRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbShopAreaDto findById(Integer id) {
        TbShopArea tbShopArea = tbShopAreaRepository.findById(id).orElseGet(TbShopArea::new);
        ValidationUtil.isNull(tbShopArea.getId(),"TbShopArea","id",id);
        return tbShopAreaMapper.toDto(tbShopArea);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbShopAreaDto create(TbShopArea resources) {
        resources.setCreatedAt(Instant.now().toEpochMilli());
        resources.setUpdatedAt(Instant.now().toEpochMilli());
        resources.setSort(0);
        return tbShopAreaMapper.toDto(tbShopAreaRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TbShopArea resources) {
        TbShopArea tbShopArea = tbShopAreaRepository.findById(resources.getId()).orElseGet(TbShopArea::new);
        tbShopArea.setUpdatedAt(Instant.now().toEpochMilli());
        ValidationUtil.isNull( tbShopArea.getId(),"TbShopArea","id",resources.getId());
        tbShopArea.copy(resources);
        tbShopAreaRepository.save(tbShopArea);
    }

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

    @Override
    public void download(List<TbShopAreaDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbShopAreaDto tbShopArea : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("店铺Id", tbShopArea.getShopId());
            map.put("排序", tbShopArea.getSort());
            map.put("区域名称", tbShopArea.getName());
            map.put("区域价格", tbShopArea.getPrice());
            map.put("图片", tbShopArea.getView());
            map.put("建议人数 5-10", tbShopArea.getCapacityRange());
            map.put(" createdAt",  tbShopArea.getCreatedAt());
            map.put(" updatedAt",  tbShopArea.getUpdatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}