package com.fastbee.rechargecard.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fastbee.common.core.domain.entity.SysDept;
import com.fastbee.rechargecard.domain.NgMerchants;
import com.fastbee.rechargecard.domain.dto.NgMerchantsDto;
import com.fastbee.rechargecard.mapper.NgMerchantsMapper;
import com.fastbee.rechargecard.service.INgMerchantsService;
import com.fastbee.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * 商户信息Service业务层处理
 * 
 * @author kerwincui
 * @date 2024-12-27
 */
@Service
public class NgMerchantsServiceImpl implements INgMerchantsService
{
    @Autowired
    private NgMerchantsMapper ngMerchantsMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询商户信息
     * 
     * @param id 商户信息主键
     * @return 商户信息
     */
    @Override
    public NgMerchantsDto selectNgMerchantsById(Long id)
    {
        NgMerchantsDto ngMerchantsDto = new NgMerchantsDto();
        NgMerchants ngMerchants = ngMerchantsMapper.selectNgMerchantsById(id);
        SysDept sysDept = sysDeptMapper.selectDeptById(ngMerchants.getDeptId());
        ngMerchantsDto.setWechatPublicKeyId(ngMerchants.getWechatPublicKeyId());
        ngMerchantsDto.setPlatformCertificateSerialNumber(
                ngMerchants.getPlatformCertificateSerialNumber());
        ngMerchantsDto.setId(ngMerchants.getId());
        ngMerchantsDto.setApiV3Key(ngMerchants.getApiV3Key());
        ngMerchantsDto.setMchId(ngMerchants.getMchId());
        ngMerchantsDto.setPrivateKeyPath(ngMerchants.getPrivateKeyPath());
        ngMerchantsDto.setPublicKeyPath(ngMerchants.getPublicKeyPath());
        ngMerchantsDto.setSerialNo(ngMerchants.getSerialNo());
        ngMerchantsDto.setDeptId(ngMerchants.getDeptId());
        ngMerchantsDto.setDeptName(sysDept.getDeptName());
        return ngMerchantsDto;
    }

    /**
     * 查询商户信息列表
     * 
     * @param ngMerchants 商户信息
     * @return 商户信息
     */
    @Override
    public List<NgMerchantsDto> selectNgMerchantsList(NgMerchants ngMerchants)
    {
        // 查询商户信息列表
        List<NgMerchants> ngMerchantsList = ngMerchantsMapper.selectNgMerchantsList(ngMerchants);

        // 查询机构信息列表
        List<SysDept> sysDeptList = sysDeptMapper.selectDeptList(new SysDept());

        // 合并信息
        return ngMerchantsList.stream()
                .map(ngMerchants1 -> {
                    NgMerchantsDto ngMerchantsDto = new NgMerchantsDto();
                    ngMerchantsDto.setId(ngMerchants1.getId());
                    ngMerchantsDto.setWechatPublicKeyId(ngMerchants1.getWechatPublicKeyId());
                    ngMerchantsDto.setPlatformCertificateSerialNumber(
                            ngMerchants1.getPlatformCertificateSerialNumber());
                    ngMerchantsDto.setApiV3Key(ngMerchants1.getApiV3Key());
                    ngMerchantsDto.setMchId(ngMerchants1.getMchId());
                    ngMerchantsDto.setPrivateKeyPath(ngMerchants1.getPrivateKeyPath());
                    ngMerchantsDto.setPublicKeyPath(ngMerchants1.getPublicKeyPath());
                    ngMerchantsDto.setSerialNo(ngMerchants1.getSerialNo());
                    ngMerchantsDto.setDeptId(ngMerchants1.getDeptId());
                    ngMerchantsDto.setPlatformCertificates(ngMerchants1.getPlatformCertificates());
                    return ngMerchantsDto;
                })
                .peek(ngMerchantsDto -> {
                    sysDeptList.stream()
                            .filter(sysDept1 -> sysDept1.getDeptId().equals(ngMerchantsDto.getDeptId()))
                            .findFirst()
                            .ifPresent(sysDept1 -> ngMerchantsDto.setDeptName(sysDept1.getDeptName()));
                }).collect(Collectors.toList());
    }

    /**
     * 新增商户信息
     * 
     * @param ngMerchants 商户信息
     * @return 结果
     */
    @Override
    public int insertNgMerchants(NgMerchants ngMerchants)
    {
        SysDept sysDept =  sysDeptMapper.selectDeptById(ngMerchants.getDeptId());
        QueryWrapper<NgMerchants> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_id", ngMerchants.getDeptId());
        List<NgMerchants> ngMerchants1 = ngMerchantsMapper.selectList(queryWrapper);
        if(sysDept == null||ngMerchants1==null){
            return 0;
        }
         return ngMerchantsMapper.insertNgMerchants(ngMerchants);
    }

    /**
     * 修改商户信息
     * 
     * @param ngMerchants 商户信息
     * @return 结果
     */
    @Override
    public int updateNgMerchants(NgMerchants ngMerchants)
    {

        if (sysDeptMapper.selectDeptById(ngMerchants.getDeptId())==null){
            return 0;
        }
        return ngMerchantsMapper.updateNgMerchants(ngMerchants);
    }

    /**
     * 批量删除商户信息
     * 
     * @param ids 需要删除的商户信息主键
     * @return 结果
     */
    @Override
    public int deleteNgMerchantsByIds(Long[] ids)
    {
        return ngMerchantsMapper.deleteNgMerchantsByIds(ids);
    }

    /**
     * 删除商户信息信息
     * 
     * @param id 商户信息主键
     * @return 结果
     */
    @Override
    public int deleteNgMerchantsById(Long id)
    {
        return ngMerchantsMapper.deleteNgMerchantsById(id);
    }
}
