package com.ruoyi.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.UserRelate;
import com.ruoyi.system.mapper.UserRelateMapper;
import com.ruoyi.system.service.IUserRelateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 子系统用户账号关联Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-02
 */
@Service
public class UserRelateServiceImpl implements IUserRelateService
{
    @Autowired
    private UserRelateMapper userRelateMapper;

    /**
     * 查询子系统用户账号关联
     * 
     * @param relateId 子系统用户账号关联ID
     * @return 子系统用户账号关联
     */
    @Override
    public UserRelate selectUserRelateById(Long relateId)
    {
        return userRelateMapper.selectUserRelateById(relateId);
    }

    /**
     * 查询子系统用户账号关联列表
     * 
     * @param userRelate 子系统用户账号关联
     * @return 子系统用户账号关联
     */
    @Override
    public List<UserRelate> selectUserRelateList(UserRelate userRelate)
    {
        return userRelateMapper.selectUserRelateList(userRelate);
    }

    /**
     * 新增子系统用户账号关联
     * 
     * @param userRelate 子系统用户账号关联
     * @return 结果
     */
    @Override
    public int insertUserRelateList(UserRelate userRelate)
    {
        int result = 0;
        String jsonStr = userRelate.getChildDatas();
        List<UserRelate> childList = JSONArray.parseArray(jsonStr, UserRelate.class);
        if(null!=childList && childList.size()>0){
            for(UserRelate relate: childList){
                relate.setUserId(userRelate.getUserId());
                relate.setLoginName(userRelate.getLoginName());
                relate.setRelateTime(DateUtils.getNowDate());
                relate.setCreateTime(DateUtils.getNowDate());
            }
            result = userRelateMapper.batchUserRelate(childList);
        }
        return result;
    }

    @Override
    public int   insertUserRelate(UserRelate userRelate) {
        return userRelateMapper.insertUserRelate(userRelate);
    }

    /**
     * 修改子系统用户账号关联
     * 
     * @param userRelate 子系统用户账号关联
     * @return 结果
     */
    @Override
    public int updateUserRelate(UserRelate userRelate)
    {
        userRelate.setUpdateTime(DateUtils.getNowDate());
        return userRelateMapper.updateUserRelate(userRelate);
    }

    /**
     * 删除子系统用户账号关联对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteUserRelateByIds(String ids)
    {
        return userRelateMapper.deleteUserRelateByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除子系统用户账号关联信息
     * 
     * @param relateId 子系统用户账号关联ID
     * @return 结果
     */
    @Override
    public int deleteUserRelateById(Long relateId)
    {
        return userRelateMapper.deleteUserRelateById(relateId);
    }
}
