package com.ruoyi.dz.service.impl;

import java.util.Collection;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.NumberUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.enums.UserTypeEnum;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dz.mapper.DzAgentMapper;
import com.ruoyi.dz.domain.DzAgent;
import com.ruoyi.dz.service.IDzAgentService;

/**
 * 机构代理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-12
 */
@Service
public class DzAgentServiceImpl implements IDzAgentService
{
    @Autowired
    private DzAgentMapper dzAgentMapper;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询机构代理
     *
     * @param agentId 机构代理主键
     * @return 机构代理
     */
    @Override
    public DzAgent selectDzAgentByAgentId(Long agentId)
    {
        return dzAgentMapper.selectDzAgentByAgentId(agentId);
    }

    @Override
    public DzAgent selectDzAgentByUserId(Long userId) {
        return dzAgentMapper.selectDzAgentByUserId(userId);
    }

    @Override
    public List<DzAgent> selectDzAgentByAgentIds(Collection<Long> ids) {
        return dzAgentMapper.selectDzAgentByAgentIds(ids);
    }

    @Override
    public List<DzAgent> selectDzAgentByUserIds(Collection<Long> userIds) {
        return dzAgentMapper.selectDzAgentByUserIds(userIds);
    }

    /**
     * 查询机构代理列表
     *
     * @param dzAgent 机构代理
     * @return 机构代理
     */
    @Override
    public List<DzAgent> selectDzAgentList(DzAgent dzAgent)
    {
        return dzAgentMapper.selectDzAgentList(dzAgent);
    }

    /**
     * 新增机构代理
     *
     * @param dzAgent 机构代理
     * @return 结果
     */
    @Override
    public int insertDzAgent(DzAgent dzAgent)
    {
        Long userId = userService.insertRelateUser(UserTypeEnum.Agent, dzAgent.getAgentId(), NumberUtils.isPositive(dzAgent.getParentId()) ? "SubAgent" : null, dzAgent.getDeptId(), dzAgent.getUsername(), dzAgent.getName());

        dzAgent.setUserId(userId);
        int iRet = dzAgentMapper.insertDzAgent(dzAgent);

        //将用户登录的username更新为dzAgent.getAgentId()
        SysUser user = new SysUser();
        user.setUserId(userId);
        user.setUserName(String.valueOf(dzAgent.getAgentId()));
        user.setUserTypeId(dzAgent.getAgentId());
        userService.updateUserInfo(user);

        return iRet;
    }

    /**
     * 修改机构代理
     *
     * @param dzAgent 机构代理
     * @return 结果
     */
    @Override
    public int updateDzAgent(DzAgent dzAgent)
    {
        return dzAgentMapper.updateDzAgent(dzAgent);
    }

    /**
     * 批量删除机构代理
     *
     * @param agentIds 需要删除的机构代理主键
     * @return 结果
     */
    @Override
    public int deleteDzAgentByAgentIds(Long[] agentIds)
    {
        return dzAgentMapper.deleteDzAgentByAgentIds(agentIds);
    }

    /**
     * 删除机构代理信息
     *
     * @param agentId 机构代理主键
     * @return 结果
     */
    @Override
    public int deleteDzAgentByAgentId(Long agentId)
    {
        return dzAgentMapper.deleteDzAgentByAgentId(agentId);
    }
}
