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

import cn.ysk.cashier.pojo.TbRenewalsPayLog;
import cn.ysk.cashier.utils.ValidationUtil;
import cn.ysk.cashier.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.repository.TbRenewalsPayLogRepository;
import cn.ysk.cashier.service.TbRenewalsPayLogService;
import cn.ysk.cashier.dto.TbRenewalsPayLogDto;
import cn.ysk.cashier.dto.TbRenewalsPayLogQueryCriteria;
import cn.ysk.cashier.mapper.TbRenewalsPayLogMapper;
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 2023-11-07
**/
@Service
@RequiredArgsConstructor
public class TbRenewalsPayLogServiceImpl implements TbRenewalsPayLogService {

    private final TbRenewalsPayLogRepository tbRenewalsPayLogRepository;
    private final TbRenewalsPayLogMapper tbRenewalsPayLogMapper;

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

    @Override
    public List<TbRenewalsPayLogDto> queryAll(TbRenewalsPayLogQueryCriteria criteria){
        return tbRenewalsPayLogMapper.toDto(tbRenewalsPayLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbRenewalsPayLogDto findById(Integer id) {
        TbRenewalsPayLog tbRenewalsPayLog = tbRenewalsPayLogRepository.findById(id).orElseGet(TbRenewalsPayLog::new);
        ValidationUtil.isNull(tbRenewalsPayLog.getId(),"TbRenewalsPayLog","id",id);
        return tbRenewalsPayLogMapper.toDto(tbRenewalsPayLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbRenewalsPayLogDto create(TbRenewalsPayLog resources) {
        return tbRenewalsPayLogMapper.toDto(tbRenewalsPayLogRepository.save(resources));
    }

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

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

    @Override
    public void download(List<TbRenewalsPayLogDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbRenewalsPayLogDto tbRenewalsPayLog : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("支付方式", tbRenewalsPayLog.getPayType());
            map.put("店铺Id", tbRenewalsPayLog.getShopId());
            map.put("订单Id", tbRenewalsPayLog.getOrderId());
            map.put(" openId",  tbRenewalsPayLog.getOpenId());
            map.put("用户Id", tbRenewalsPayLog.getUserId());
            map.put("交易单号（第三方交易单号）", tbRenewalsPayLog.getTransactionId());
            map.put("金额", tbRenewalsPayLog.getAmount());
            map.put("状态 ", tbRenewalsPayLog.getStatus());
            map.put("备注", tbRenewalsPayLog.getRemark());
            map.put("用户自定义参数", tbRenewalsPayLog.getAttach());
            map.put("到期时间", tbRenewalsPayLog.getExpiredAt());
            map.put("创建时间", tbRenewalsPayLog.getCreatedAt());
            map.put(" updatedAt",  tbRenewalsPayLog.getUpdatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}