/*
*  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.TbShopCurrency;
import cn.ysk.cashier.utils.ValidationUtil;
import cn.ysk.cashier.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.repository.shop.TbShopCurrencyRepository;
import cn.ysk.cashier.service.shop.TbShopCurrencyService;
import cn.ysk.cashier.dto.shop.TbShopCurrencyDto;
import cn.ysk.cashier.dto.shop.TbShopCurrencyQueryCriteria;
import cn.ysk.cashier.mapper.shop.TbShopCurrencyMapper;
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-05
**/
@Service
@RequiredArgsConstructor
public class TbShopCurrencyServiceImpl implements TbShopCurrencyService {

    private final TbShopCurrencyRepository tbShopCurrencyRepository;
    private final TbShopCurrencyMapper tbShopCurrencyMapper;

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

    @Override
    public List<TbShopCurrencyDto> queryAll(TbShopCurrencyQueryCriteria criteria){
        return tbShopCurrencyMapper.toDto(tbShopCurrencyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbShopCurrencyDto findById(Integer id) {
        TbShopCurrency tbShopCurrency = tbShopCurrencyRepository.findById(id).orElseGet(TbShopCurrency::new);
        ValidationUtil.isNull(tbShopCurrency.getId(),"TbShopCurrency","id",id);
        return tbShopCurrencyMapper.toDto(tbShopCurrency);
    }

    @Override
    public TbShopCurrency findByShopId(String id) {
        TbShopCurrency byShopId = tbShopCurrencyRepository.findByShopId(id);
        return byShopId;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbShopCurrencyDto create(TbShopCurrency resources) {
        return tbShopCurrencyMapper.toDto(tbShopCurrencyRepository.save(resources));
    }

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

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

    @Override
    public void download(List<TbShopCurrencyDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbShopCurrencyDto tbShopCurrency : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("店铺Id", tbShopCurrency.getShopId());
            map.put("备用金", tbShopCurrency.getPrepareAmount());
            map.put("货币单位 ￥，$", tbShopCurrency.getCurrency());
            map.put("小数位，0，1，2，无，元，角，分", tbShopCurrency.getDecimalsDigits());
            map.put("无，四舍五入，五舍六入none,round4up5,round5up6,round12up34", tbShopCurrency.getDiscountRound());
            map.put("商户Id", tbShopCurrency.getMerchantId());
            map.put("订单抹零 -1无，0元 1角2分  0.5=0.5元", tbShopCurrency.getSmallChange());
            map.put("使折扣生效", tbShopCurrency.getEnableCustomDiscount());
            map.put("最大抹零金额(100)", tbShopCurrency.getMaxDiscount());
            map.put("最大折扣百分比,，优先级高于max_discount", tbShopCurrency.getMaxPercent());
            map.put("折扣显示详情", tbShopCurrency.getDiscountConfigs());
            map.put("营业时间(弃用)", tbShopCurrency.getBizDuration());
            map.put("允许网络支付", tbShopCurrency.getAllowWebPay());
            map.put("自动抹零，开启时，系统自动抹零", tbShopCurrency.getIsAutoToZero());
            map.put("商品含税", tbShopCurrency.getIsIncludeTaxPrice());
            map.put("服务费配置（小费）", tbShopCurrency.getServiceCharge());
            map.put("税号", tbShopCurrency.getTaxNumber());
            map.put(" createdAt",  tbShopCurrency.getCreatedAt());
            map.put(" updatedAt",  tbShopCurrency.getUpdatedAt());
            map.put("自动锁屏", tbShopCurrency.getAutoLockScreen());
            map.put("语音通知", tbShopCurrency.getVoiceNotification());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}