/*
*  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.pojo.shop.TbShopCashSpread;
import cn.ysk.cashier.service.shop.TbShopCashSpreadService;
import cn.ysk.cashier.dto.shop.TbShopCashSpreadDto;
import cn.ysk.cashier.mapper.shop.TbShopCashSpreadMapper;
import cn.ysk.cashier.utils.ValidationUtil;
import cn.ysk.cashier.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.repository.shop.TbShopCashSpreadRepository;
import cn.ysk.cashier.dto.shop.TbShopCashSpreadQueryCriteria;
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-05
**/
@Service
@RequiredArgsConstructor
public class TbShopCashSpreadServiceImpl implements TbShopCashSpreadService {

    private final TbShopCashSpreadRepository tbShopCashSpreadRepository;
    private final TbShopCashSpreadMapper tbShopCashSpreadMapper;

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

    @Override
    public List<TbShopCashSpreadDto> queryAll(TbShopCashSpreadQueryCriteria criteria){
        return tbShopCashSpreadMapper.toDto(tbShopCashSpreadRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbShopCashSpreadDto findById(String id) {
        TbShopCashSpread tbShopCashSpread = tbShopCashSpreadRepository.findById(id).orElseGet(TbShopCashSpread::new);
        ValidationUtil.isNull(tbShopCashSpread.getId(),"TbShopCashSpread","id",id);
        return tbShopCashSpreadMapper.toDto(tbShopCashSpread);
    }

    @Override
    public TbShopCashSpread findByShopId(Integer id) {
        TbShopCashSpread byShopId = tbShopCashSpreadRepository.findByShopId(id);
        return byShopId;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbShopCashSpreadDto create(TbShopCashSpread resources) {
        return tbShopCashSpreadMapper.toDto(tbShopCashSpreadRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer update(TbShopCashSpread resources) {
        return tbShopCashSpreadRepository.updateConfig(resources.getId(), resources.getScreenConfig().trim(), Instant.now().toEpochMilli());
    }

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

    @Override
    public void download(List<TbShopCashSpreadDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbShopCashSpreadDto tbShopCashSpread : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("登陆密码", tbShopCashSpread.getSaleReceipt());
            map.put("状态", tbShopCashSpread.getTriplicateReceipt());
            map.put("到期提醒时间", tbShopCashSpread.getScreenConfig());
            map.put(" tagConfig",  tbShopCashSpread.getTagConfig());
            map.put(" scaleConfig",  tbShopCashSpread.getScaleConfig());
            map.put(" createdAt",  tbShopCashSpread.getCreatedAt());
            map.put(" updatedAt",  tbShopCashSpread.getUpdatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}