package com.lightinit.hsdataplatform.impl.admin;

import com.lightinit.hsdataplatform.entity.AtlasRelationship;
import com.lightinit.hsdataplatform.mapper.AtlasRelationshipMapper;
import com.lightinit.hsdataplatform.mapper.ResourceMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.ResultStateCode;
import com.lightinit.hsdataplatform.model.admin.*;
import com.lightinit.hsdataplatform.service.admin.IRelationshipManageService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RelationshipManageServiceImpl implements IRelationshipManageService {
   @Autowired
    private ResourceMapper resourceMapper;
   @Autowired
   private AtlasRelationshipMapper atlasRelationshipMapper;
    @Override
    public List<RelationshipManageListModel> queryRelationshipManageList(RelationshipManageSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return resourceMapper.queryRelationshipManageList(inputModel);
    }

    @Override
    public long queryRelationshipManageCount(RelationshipManageSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return resourceMapper.queryRelationshipManageCount(inputModel);
    }
    /**
     *调用list
     * @param
     * @return
     */
    @Override
    public List<RelationshipManageListModel> callerList(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return atlasRelationshipMapper.callerList(id);
    }
    /**
     *被调用list
     * @param inputModel
     * @return
     */
    @Override
    public List<RelationshipManageListModel> calleeList(RelationshipManageSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return atlasRelationshipMapper.calleeList(inputModel);
    }
    /**
     *被调用条数
     * @param inputModel
     * @return
     */
    @Override
    public long calleeCount(RelationshipManageSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return atlasRelationshipMapper.calleeCount(inputModel);
    }

    @Override
    public ResultState doEdit(AtlasRelationshipModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState=new ResultState<Long>();
       int sqlresult =0;
        String[] str =inputModel.getCalle().split(",");
        for (int i = 0; i < str.length; i++) {
            String s = str[i];
            inputModel.setCallee(StringUtils.isBlank(s)? 0 : Long.valueOf(s));
            AtlasRelationship model = new AtlasRelationship();
            BeanUtils.copyProperties(inputModel,model);
            sqlresult = atlasRelationshipMapper.insert(model);
        }
        if(!(sqlresult>0)) {
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据不存在");
        }
        resultState.setData(inputModel.getCaller());
        return resultState;
    }

    @Override
    public RelationshipManageViewModel detal(long id, String resourceType) {
        return resourceMapper.detal(id,resourceType);
    }

    @Override
    public List<ResourceAPIRegisterAddAPIInterfaceInputModel> queryInterfaceIn(Long id) {
        return resourceMapper.queryInterfaceIn(id);
    }

    @Override
    public List<ResourceAPIRegisterAddAPIInterfaceOutputModel> queryInterfaceOut(Long id) {
        return resourceMapper.queryInterfaceOut(id);
    }

    @Override
    public List<RelationshipManageListModel> caller(Long id) {
        return atlasRelationshipMapper.caller(id);
    }

    @Override
    public List<RelationshipManageListModel> callee(Long id) {
        return atlasRelationshipMapper.callee(id);
    }

}
