package com.skr.mdm.api.app.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.skr.mdm.api.app.service.AppStrategyInfoService;
import com.skr.mdm.bean.qo.app.AppStrategyInfoQO;
import com.skr.mdm.bean.qo.app.AppStrategyQO;
import com.skr.mdm.bean.vo.app.AppStrategyInfoVO;
import com.skr.mdm.dao.AppStrategyInstallSourceDao;
import com.skr.mdm.entity.AppStrategyInstallSourceDO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author nfq
 * @date 2023-03-27 9:30
 */
@Service
public class AppStrategyInstallSourceServiceImpl implements AppStrategyInfoService {

    @Autowired
    private AppStrategyInstallSourceDao appStrategyInstallSourceDao;

    @Override
    public AppStrategyInfoVO setAppStrategyInfoVO(AppStrategyInfoVO appStrategyInfoVO, Integer strategyId) {
        appStrategyInfoVO.setAppStrategyInstallSourceVO(appStrategyInstallSourceDao.getAppStrategyConfigInfoVO(strategyId));
        return appStrategyInfoVO;
    }

    @Override
    public void saveAppStrategy(AppStrategyQO appStrategyQO, Integer strategyId) {
        saveAppStrategyInstallSource(appStrategyQO.getAppStrategyInstallSourceQO(), strategyId);
    }

    @Override
    public void deleteAppStrategy(List<Integer> strategyIdList) {
        appStrategyInstallSourceDao.delete(Wrappers.lambdaQuery(AppStrategyInstallSourceDO.class)
                .in(AppStrategyInstallSourceDO::getStrategyId, strategyIdList));
    }

    @Override
    public boolean checkAppExist(List<Integer> appIdList, boolean isExit) {
        if (!isExit) {
            return new LambdaQueryChainWrapper<>(appStrategyInstallSourceDao)
                    .in(AppStrategyInstallSourceDO::getAppId, appIdList)
                    .count() > 0;
        }
        return true;
    }

    /**
     * 保存应用安装来源
     *
     * @param appStrategyInfoQO
     * @param strategyId
     */
    private void saveAppStrategyInstallSource(AppStrategyInfoQO appStrategyInfoQO, Integer strategyId) {
        if (appStrategyInfoQO != null) {
            // strategyType类型为其他来源
            if (CollectionUtils.isNotEmpty(appStrategyInfoQO.getAppIdList())) {
                List<AppStrategyInstallSourceDO> appStrategyInstallSourceDOList = appStrategyInfoQO.getAppIdList()
                        .stream()
                        .map(appId -> AppStrategyInstallSourceDO.builder()
                                .strategyId(strategyId)
                                .strategyType(appStrategyInfoQO.getStrategyType())
                                .appId(appId)
                                .build())
                        .collect(Collectors.toList());
                appStrategyInstallSourceDao.insertBatch(appStrategyInstallSourceDOList);
            } else {
                // strategyType类型为非其他来源
                AppStrategyInstallSourceDO appStrategyInstallSourceDO = AppStrategyInstallSourceDO.builder()
                        .strategyId(strategyId)
                        .strategyType(appStrategyInfoQO.getStrategyType())
                        .appId(0)
                        .build();
                appStrategyInstallSourceDao.insert(appStrategyInstallSourceDO);
            }
        }
    }
}
