/*
*  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.TbReceiptSales;
import cn.ysk.cashier.mapper.shop.TbReceiptSalesMapper;
import cn.ysk.cashier.utils.ValidationUtil;
import cn.ysk.cashier.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.repository.shop.TbReceiptSalesRepository;
import cn.ysk.cashier.service.shop.TbReceiptSalesService;
import cn.ysk.cashier.dto.shop.TbReceiptSalesDto;
import cn.ysk.cashier.dto.shop.TbReceiptSalesQueryCriteria;
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 2024-01-08
**/
@Service
@RequiredArgsConstructor
public class TbReceiptSalesServiceImpl implements TbReceiptSalesService {

    private final TbReceiptSalesRepository tbReceiptSalesRepository;
    private final TbReceiptSalesMapper tbReceiptSalesMapper;

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

    @Override
    public List<TbReceiptSalesDto> queryAll(TbReceiptSalesQueryCriteria criteria){
        return tbReceiptSalesMapper.toDto(tbReceiptSalesRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbReceiptSalesDto findById(Integer id) {
        TbReceiptSales tbReceiptSales = tbReceiptSalesRepository.findById(id).orElseGet(TbReceiptSales::new);

        return tbReceiptSalesMapper.toDto(tbReceiptSales);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbReceiptSalesDto create(TbReceiptSales resources) {
        return tbReceiptSalesMapper.toDto(tbReceiptSalesRepository.save(resources));
    }

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

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

    @Override
    public void download(List<TbReceiptSalesDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbReceiptSalesDto tbReceiptSales : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("标题", tbReceiptSales.getTitle());
            map.put("是否显示公司Logo", tbReceiptSales.getLogo());
            map.put("打印联系电话等信息", tbReceiptSales.getShowContactInfo());
            map.put("打印会员开关 0？1", tbReceiptSales.getShowMember());
            map.put("打印会员编号开关", tbReceiptSales.getShowMemberCode());
            map.put("打印会员积分", tbReceiptSales.getShowMemberScore());
            map.put("打印会员余额开关 0？1", tbReceiptSales.getShowMemberWallet());
            map.put("店铺Id", tbReceiptSales.getFooterRemark());
            map.put("打印找零", tbReceiptSales.getShowCashCharge());
            map.put("流水号", tbReceiptSales.getShowSerialNo());
            map.put("用大号字打印流水号  在showSerialNo可用前提下", tbReceiptSales.getBigSerialNo());
            map.put("头部文字", tbReceiptSales.getHeaderText());
            map.put("文字 对齐方式", tbReceiptSales.getHeaderTextAlign());
            map.put("尾部文字", tbReceiptSales.getFooterText());
            map.put("文字 对齐方式", tbReceiptSales.getFooterTextAlign());
            map.put("尾部图像", tbReceiptSales.getFooterImage());
            map.put("预打印，YES开启 NO不开启", tbReceiptSales.getPrePrint());
            map.put(" createdAt",  tbReceiptSales.getCreatedAt());
            map.put(" updatedAt",  tbReceiptSales.getUpdatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}