package com.friday.custody.service.basicService.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.friday.custody.dao.NotifierInfoDao;
import com.friday.custody.entity.NotifierInfoEntity;
import com.friday.custody.entity.SubscribeFundInfoEntity;
import com.friday.custody.entity.SubscribeFundNotifierInfoEntity;
import com.friday.custody.service.basicService.NotifierInfoService;
import com.friday.custody.service.basicService.SubscribeFundInfoService;
import com.friday.custody.service.basicService.SubscribeFundNotifierInfoService;
import com.friday.custody.utils.PageUtils;
import com.friday.custody.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("notifierInfoService")
public class NotifierInfoServiceImpl extends ServiceImpl<NotifierInfoDao, NotifierInfoEntity> implements NotifierInfoService {

    @Autowired
    private SubscribeFundNotifierInfoService mappingService;
    @Autowired
    private SubscribeFundInfoService subscribeFundInfoService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<NotifierInfoEntity> page = this.page(
                new Query<NotifierInfoEntity>().getPage(params),
                new QueryWrapper<NotifierInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<NotifierInfoEntity> queryByFund(String fundCode, String admin, String accountType) {
        return baseMapper.queryByFund(fundCode, admin, accountType);
    }

    /**
     * 查询订阅人订阅过的所有基金
     * @param subscribeId
     * @return
     */
    @Override
    public List<SubscribeFundInfoEntity> getFundInfoListBySubscribeId(Integer subscribeId) {
        List<SubscribeFundInfoEntity> result = new ArrayList<>();
        LambdaQueryWrapper<SubscribeFundNotifierInfoEntity> mappingQueryWrapper = new LambdaQueryWrapper<>();
        mappingQueryWrapper.eq(SubscribeFundNotifierInfoEntity::getNotifierId, subscribeId);
        List<SubscribeFundNotifierInfoEntity> mappingList = mappingService.list(mappingQueryWrapper);
        for (SubscribeFundNotifierInfoEntity mapping : mappingList) {
            Integer fundId = mapping.getFundId();
            SubscribeFundInfoEntity fundInfo = subscribeFundInfoService.getById(fundId);
            result.add(fundInfo);
        }

        return result;
    }
}