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.AppStrategyQO;
import com.skr.mdm.bean.qo.app.AppStrategyUsePermissionQO;
import com.skr.mdm.bean.vo.app.AppStrategyInfoVO;
import com.skr.mdm.bean.vo.app.AppStrategyUsePermissionVO;
import com.skr.mdm.dao.AppStrategyUsePermissionAppDao;
import com.skr.mdm.dao.AppStrategyUsePermissionDao;
import com.skr.mdm.entity.AppStrategyUsePermissionAppDO;
import com.skr.mdm.entity.AppStrategyUsePermissionDO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
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 AppStrategyUsePermissionServiceImpl implements AppStrategyInfoService {

    @Autowired
    private AppStrategyUsePermissionDao appStrategyUsePermissionDao;
    @Autowired
    private AppStrategyUsePermissionAppDao appStrategyUsePermissionAppDao;

    @Override
    public AppStrategyInfoVO setAppStrategyInfoVO(AppStrategyInfoVO appStrategyInfoVO, Integer strategyId) {
        AppStrategyUsePermissionVO appStrategyUsePermissionVO = appStrategyUsePermissionDao.getAppStrategyUsePermissionVO(strategyId);
        if (appStrategyUsePermissionVO != null) {
            appStrategyUsePermissionVO.setAppConfigVOList(appStrategyUsePermissionAppDao.listAppConfigVO(strategyId));
        }
        appStrategyInfoVO.setAppStrategyUsePermissionVO(appStrategyUsePermissionVO);
        return appStrategyInfoVO;
    }

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

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

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

    /**
     * 保存应用权限控制
     *
     * @param appStrategyUsePermissionQO
     * @param strategyId
     */
    private void saveAppStrategyUsePermission(AppStrategyUsePermissionQO appStrategyUsePermissionQO, Integer strategyId) {
        if (appStrategyUsePermissionQO != null) {
            AppStrategyUsePermissionDO appStrategyUsePermissionDO = AppStrategyUsePermissionDO.builder()
                    .strategyId(strategyId)
                    .build();
            BeanUtils.copyProperties(appStrategyUsePermissionQO, appStrategyUsePermissionDO);
            appStrategyUsePermissionDao.insert(appStrategyUsePermissionDO);
            if (CollectionUtils.isNotEmpty(appStrategyUsePermissionQO.getAppIdList())) {
                List<AppStrategyUsePermissionAppDO> strategyUsePermissionAppDOList =  appStrategyUsePermissionQO.getAppIdList().stream()
                        .map(appId -> AppStrategyUsePermissionAppDO.builder()
                                .strategyId(strategyId)
                                .appId(appId)
                                .build())
                        .collect(Collectors.toList());
                appStrategyUsePermissionAppDao.insertBatch(strategyUsePermissionAppDOList);
            }
        }
    }
}