package com.fdkankan.platform.agent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.platform.agent.entity.AgentFramework;
import com.fdkankan.platform.agent.mapper.IAgentFrameworkMapper;
import com.fdkankan.platform.agent.request.RequestAgentFramework;
import com.fdkankan.platform.agent.service.IAgentFrameworkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.platform.user.entity.Manager;
import com.fdkankan.platform.user.service.IManagerService;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.constant.ServerCode;
import com.fdkankan.common.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 代理商架构管理 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-12-24
 */
@Service
public class AgentFrameworkServiceImpl extends ServiceImpl<IAgentFrameworkMapper, AgentFramework> implements IAgentFrameworkService {

    @Autowired
    private IManagerService managerService;

    @Override
    public String saveByParam(RequestAgentFramework param) {
        if(StringUtils.isBlank(param.getName()) || param.getParentId() == null){
            throw new BusinessException(ErrorCode.PARAM_REQUIRED);
        }
        QueryWrapper<AgentFramework> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AgentFramework::getAgentName,param.getName());
        queryWrapper.lambda().eq(AgentFramework::getTbStatus, TbStatus.VALID.code());
        List<AgentFramework> list = this.list(queryWrapper);
        if(list != null && list.size() > 0){
            throw new BusinessException(ErrorCode.AGENT_EXIST);
        }
        AgentFramework agentFramework = new AgentFramework();
        agentFramework.setAgentName(param.getName());
        agentFramework.setParentId(param.getParentId());
        if(!this.save(agentFramework)){
            throw new BusinessException(ErrorCode.ERROR_MSG);
        }
        return ServerCode.SUCCESS.message();
    }

    @Override
    public String updateByParam(RequestAgentFramework param) {
        if(StringUtils.isBlank(param.getName()) || param.getId() == null){
            throw new BusinessException(ErrorCode.PARAM_REQUIRED);
        }
        AgentFramework agentFramework = this.getById(param.getId());
        if(agentFramework == null){
            throw new BusinessException(ErrorCode.AGENT_NOT_EXIST);
        }
        if(agentFramework.getAgentName().equals(param.getName())){
            throw new BusinessException(ErrorCode.AGENT_EXIST);
        }
        agentFramework.setAgentName(param.getName());
        if(!this.saveOrUpdate(agentFramework)){
            throw new BusinessException(ErrorCode.ERROR_MSG);
        }
        return ServerCode.SUCCESS.message();
    }

    @Override
    public String deleteByParam(RequestAgentFramework param) {
        if( param.getId() == null){
            throw new BusinessException(ErrorCode.PARAM_REQUIRED);
        }
        List<Manager> managerList= managerService.getByFrameworkId(param.getId());
        if(managerList!=null && managerList.size()>0){
            throw  new BusinessException(ErrorCode.AGENT_SUB_EXIST);
        }
        if(!this.removeById(param.getId())){
            throw  new BusinessException(ErrorCode.ERROR_MSG);
        }
        return ServerCode.SUCCESS.message();
    }
}
