package com.yuchen.saas.park.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuchen.saas.manage.system.api.entity.NestApplyManage;

import com.yuchen.saas.manage.system.api.feign.ApplyManageFeignServer;
import com.yuchen.saas.park.api.entity.park.NestApplyManageParkBinding;
import com.yuchen.saas.park.api.entity.park.PmgBatParkInfo;
import com.yuchen.saas.park.api.param.ApplyManageParkBindingSaveParam;
import com.yuchen.saas.park.api.vo.park.ApplyManageParkDetailVO;
import com.yuchen.saas.park.service.mapper.NestApplyManageParkBindingMapper;
import com.yuchen.saas.park.service.service.INestApplyManageParkBindingService;
import com.yuchen.saas.park.service.service.PmgBatParkInfoService;
import org.nest.springwrap.core.log.exception.ServiceException;

import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.nest.springwrap.core.tool.utils.WebUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 园区应用关系 服务实现类
 * </p>
 *
 * @author cxj
 * @since 2023-11-13
 */
@Service
public class NestApplyManageParkBindingServiceImpl extends BaseServiceImpl<NestApplyManageParkBindingMapper, NestApplyManageParkBinding> implements INestApplyManageParkBindingService {
    @Resource
    @Lazy
    private PmgBatParkInfoService pmgBatParkInfoService;

    @Resource
    private ApplyManageFeignServer applyManageFeignServer;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveBinding(ApplyManageParkBindingSaveParam param) {
        PmgBatParkInfo  result=pmgBatParkInfoService.getById(param.getParkId());
        if(Func.isNotEmpty(result)){
            LambdaQueryWrapper<NestApplyManageParkBinding> queryWrapper=Wrappers.lambdaQuery();
            queryWrapper.eq(NestApplyManageParkBinding::getParkId,param.getParkId());
            remove(queryWrapper);
            if(Func.isNotEmpty(param.getApplyIds())){
                for (Long applyId:param.getApplyIds()) {
                    /*NestApplyManage apply = nestApplyManageService.getById(applyId);
                    if(Func.isNotEmpty(apply)){
                        NestApplyManageParkBinding dto= new NestApplyManageParkBinding();
                        dto.setApplyId(applyId);
                        dto.setParkId(param.getParkId());
                        save(dto);
                    }*/
                    NestApplyManageParkBinding dto= new NestApplyManageParkBinding();
                    dto.setApplyId(applyId);
                    dto.setParkId(param.getParkId());
                    save(dto);
                }
            }
        }else{
            throw new ServiceException("获取园区失败!");
        }
        return true;
    }

    @Override
    public ApplyManageParkDetailVO bindingDetail(Long parkId) {
        ApplyManageParkDetailVO vo=new ApplyManageParkDetailVO();
        PmgBatParkInfo  result=pmgBatParkInfoService.getById(parkId);
        if(Func.isNotEmpty(result)){
            vo.setParkId(result.getId());
            vo.setParkName(result.getParkName());
        }
        LambdaQueryWrapper<NestApplyManageParkBinding> queryWrapper=Wrappers.lambdaQuery();
        queryWrapper.eq(NestApplyManageParkBinding::getParkId,parkId);
        List<NestApplyManageParkBinding> list=list(queryWrapper);
        List<ApplyManageParkDetailVO.NestApplyManageVo> nestApplyManageVo=new ArrayList<>();
        if(Func.isNotEmpty(list)){
           List<Long> ids= list.stream().map(a->a.getApplyId()).collect(Collectors.toList());
            R<List<NestApplyManage>> r= applyManageFeignServer.listByIds(ids);
            if(r.isSuccess()&&Func.isNotEmpty(r.getData())){
                Map<Long,NestApplyManage> map= r.getData().stream().collect(Collectors.toMap(NestApplyManage::getId,
                        a->a,
                        (v1, v2) -> v2));
                for (NestApplyManageParkBinding item:list) {
                    ApplyManageParkDetailVO.NestApplyManageVo dto=new ApplyManageParkDetailVO.NestApplyManageVo();
                    if(map.containsKey(item.getApplyId())){
                        dto.setApplyId(item.getApplyId());
                        dto.setApplyName(map.get(item.getApplyId()).getName());
                    }
                    nestApplyManageVo.add(dto);
                }
            }

        }
        vo.setNestApplyManageVo(nestApplyManageVo);
        return vo;
    }

    @Override
    public List<NestApplyManageParkBinding> listByApplyId(Long applyId) {
        if(Func.isEmpty(applyId)){
            applyId= WebUtil.getApplyIdToLong();
        }
        LambdaQueryWrapper<NestApplyManageParkBinding> queryWrapper=Wrappers.lambdaQuery();
        queryWrapper.eq(NestApplyManageParkBinding::getApplyId,applyId);
        return list(queryWrapper);
    }

    @Override
    public List<NestApplyManageParkBinding> listByParkIdList(List<Long> parkIdList) {
        LambdaQueryWrapper<NestApplyManageParkBinding> queryWrapper=Wrappers.lambdaQuery();
        queryWrapper.in(NestApplyManageParkBinding::getParkId, parkIdList);
        return list(queryWrapper);
    }
}
