package com.hotent.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotent.base.context.BaseContext;
import com.hotent.base.entity.URIEntity;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.base.model.CommonResult;
import com.hotent.portal.model.PortalBizSystem;
import com.hotent.portal.model.PortalUrlPermission;
import com.hotent.portal.model.dto.PortalUrlPermissionDto;
import com.hotent.portal.persistence.dao.PortalBizSystemDao;
import com.hotent.portal.persistence.dao.PortalUrlPermissionDao;
import com.hotent.portal.service.PortalUrlPermissionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  服务实现类
 *
 * @company 广州宏天软件股份有限公司
 * @author cola
 * @since 2021-08-26
 */
@Service
public class PortalUrlPermissionManagerImpl extends BaseManagerImpl<PortalUrlPermissionDao, PortalUrlPermission> implements PortalUrlPermissionManager {

    private Logger logger= LoggerFactory.getLogger(PortalUrlPermissionManagerImpl.class);

    @Autowired
    PortalUrlPermissionDao portalUrlPermissionDao;
    @Autowired
    PortalBizSystemDao portalBizSystemDao;

    @Autowired
    BaseContext baseContext;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult saveData(PortalUrlPermissionDto dto) {
        QueryWrapper<PortalBizSystem> wrapper = new QueryWrapper<>();
        PortalBizSystem portalBizSystem = portalBizSystemDao.selectOne(wrapper.eq("SYS_CODE_",dto.getSysId()));
        if (portalBizSystem==null){
            return new CommonResult(false,"添加失败，无此系统编码",null);
        }
        // 统计插入成功和失败的数量
        int errorCount=0;
        StringBuilder msg=new StringBuilder("路径：");
        String returnMsg="";
        for (URIEntity item : dto.getUriList()) {
            PortalUrlPermission portalUrlPermission = new PortalUrlPermission();
            portalUrlPermission.setCreateBy(baseContext.getCurrentUserId());
            portalUrlPermission.setCreateOrgId(baseContext.getCurrentOrgId());
            portalUrlPermission.setAuthUrl(item.getMethodURI().replaceAll("//","/"));
            portalUrlPermission.setFunction(item.getFunction());
            portalUrlPermission.setModule(item.getModuleName());
            portalUrlPermission.setDescription(item.getDescription());
            portalUrlPermission.setSysId(portalBizSystem.getSysCode());
            try {
                int res = portalUrlPermissionDao.insert(portalUrlPermission);
            }catch (Exception e){
                ++errorCount;
                msg.append(item.getMethodURI()).append("、");
                logger.error("路径：{}，已添加，无需再次添加",item.getMethodURI());
            }
            returnMsg="操作成功，成功插入"+(dto.getUriList().size()-errorCount)+"条，失败"+errorCount+"条。";
            if (errorCount>0){
                msg.append("已添加，无需再次添加");
                returnMsg+="其中，"+msg;

            }
        }
        return new CommonResult().message( returnMsg);
    }
}
