/*
*  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.TbMerchantThirdApply;
import cn.ysk.cashier.utils.ValidationUtil;
import cn.ysk.cashier.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.repository.shop.TbMerchantThirdApplyRepository;
import cn.ysk.cashier.service.shop.TbMerchantThirdApplyService;
import cn.ysk.cashier.dto.shop.TbMerchantThirdApplyDto;
import cn.ysk.cashier.dto.shop.TbMerchantThirdApplyQueryCriteria;
import cn.ysk.cashier.mapper.shop.TbMerchantThirdApplyMapper;
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-02-02
**/
@Service
@RequiredArgsConstructor
public class TbMerchantThirdApplyServiceImpl implements TbMerchantThirdApplyService {

    private final TbMerchantThirdApplyRepository tbMerchantThirdApplyRepository;
    private final TbMerchantThirdApplyMapper tbMerchantThirdApplyMapper;

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

    @Override
    public TbMerchantThirdApplyDto findByShopId(Integer shopId) {
        TbMerchantThirdApply byShopId = tbMerchantThirdApplyRepository.findByShopId(shopId);
        return tbMerchantThirdApplyMapper.toDto(byShopId);
    }


    @Override
    public List<TbMerchantThirdApplyDto> queryAll(TbMerchantThirdApplyQueryCriteria criteria){
        return tbMerchantThirdApplyMapper.toDto(tbMerchantThirdApplyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbMerchantThirdApplyDto findById(Integer id) {
        TbMerchantThirdApply tbMerchantThirdApply = tbMerchantThirdApplyRepository.findById(id).orElseGet(TbMerchantThirdApply::new);
        ValidationUtil.isNull(tbMerchantThirdApply.getId(),"TbMerchantThirdApply","id",id);
        return tbMerchantThirdApplyMapper.toDto(tbMerchantThirdApply);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbMerchantThirdApplyDto create(TbMerchantThirdApply resources) {
//        if(tbMerchantThirdApplyRepository.findByAppId(resources.getAppId()) != null){
//            throw new EntityExistException(TbMerchantThirdApply.class,"app_id",resources.getAppId());
//        }
        return tbMerchantThirdApplyMapper.toDto(tbMerchantThirdApplyRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TbMerchantThirdApply resources) {
        TbMerchantThirdApply tbMerchantThirdApply = tbMerchantThirdApplyRepository.findByShopId(resources.getId());
        if (tbMerchantThirdApply == null){
            resources.setCreatedAt(Instant.now().toEpochMilli());
            resources.setCreatedAt(Instant.now().toEpochMilli());
            tbMerchantThirdApplyRepository.save(resources);
        }else {
            ValidationUtil.isNull(tbMerchantThirdApply.getId(), "TbMerchantThirdApply", "id", resources.getId());
            tbMerchantThirdApply.setUpdatedAt(Instant.now().toEpochMilli());
            tbMerchantThirdApply.copy(resources);
            tbMerchantThirdApplyRepository.saveAndFlush(tbMerchantThirdApply);
        }
    }

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

    @Override
    public void download(List<TbMerchantThirdApplyDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbMerchantThirdApplyDto tbMerchantThirdApply : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" type",  tbMerchantThirdApply.getType());
            map.put("商户应用", tbMerchantThirdApply.getAppId());
            map.put("商户token", tbMerchantThirdApply.getAppToken());
            map.put("审核详情-1未提交，0已提交（不可修改） 1审核通过", tbMerchantThirdApply.getStatus());
            map.put("支付密码", tbMerchantThirdApply.getPayPassword());
            map.put(" createdAt",  tbMerchantThirdApply.getCreatedAt());
            map.put(" updatedAt",  tbMerchantThirdApply.getUpdatedAt());
            map.put("shopId", tbMerchantThirdApply.getShopId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}