package com.xhpc.user.service.impl;

import com.xhpc.common.core.constant.UserConstants;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.api.model.LoginUser;
import com.xhpc.user.domain.XhpcInternetUser;
import com.xhpc.user.mapper.XhpcInternetUserMapper;
import com.xhpc.user.mapper.XhpcOperatorMapper;
import com.xhpc.user.mapper.XhpcUserMapper;
import com.xhpc.user.service.IXhpcInternetUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 流量用户信息 服务层
 *
 * @author ruoyi
 */
@Service
public class XhpcInternetUserServiceImpl implements IXhpcInternetUserService {

    @Autowired
    private XhpcInternetUserMapper xhpcInternetUserMapper;

    @Autowired
    private XhpcUserMapper xhpcUserMapper;

    @Autowired
    private XhpcOperatorMapper xhpcOperatorMapper;

    @Autowired
    private TokenService tokenService;
    /**
     * 新增任务
     *
     * @param xhpcInternetUser 流量用户信息
     */
    @Override
    @Transactional
    public int insert(XhpcInternetUser xhpcInternetUser) {
        return xhpcInternetUserMapper.insert(xhpcInternetUser);
    }

    /**
     * 更新流量用户
     *
     * @param xhpcInternetUser 流量用户信息
     */
    @Override
    @Transactional
    public int update(XhpcInternetUser xhpcInternetUser) {
        return xhpcInternetUserMapper.update(xhpcInternetUser);
    }

    /**
     * 批量删除流量用户信息
     *
     * @param ids 需要删除的流量用户ID
     * @return 结果
     */
    @Override
    @Transactional
    public boolean deleteByIds(String[] ids) {
        for (String id : ids) {
            SysUser sysUser = xhpcInternetUserMapper.getUserByInternetUserId(Long.parseLong(id));
            if (null != sysUser) {
                // 删除用户与角色关联
                xhpcOperatorMapper.deleteUserRoleByUserId(sysUser.getUserId());
                // 删除用户与岗位表
                xhpcOperatorMapper.deleteUserPostByUserId(sysUser.getUserId());
                sysUser.setDelFlag(String.valueOf(UserConstants.DEL_FLAG));
                xhpcOperatorMapper.deleteUserById(sysUser);
                xhpcInternetUserMapper.deleteByIds(ids);
                return true;
            }
        }
        return false;
    }

    /**
     * 流量用户详情
     *
     * @param internetUserId 流量用户id
     * @return 结果
     */
    @Override
    public Map<String, Object> info(Long internetUserId) {
        return xhpcInternetUserMapper.info(internetUserId);
    }

    /**
     * 流量用户分页列表
     *
     * @param name         流量用户名称
     * @param contactName  联系人
     * @param contactPhone 联系人电话
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> selectInternetList(String name, String contactName, String contactPhone, String createTimeStart, String createTimeEnd) {
        LoginUser loginUser = tokenService.getLoginUser();
        return xhpcInternetUserMapper.selectInternetList(name, contactName, contactPhone, createTimeStart, createTimeEnd,loginUser.getTenantId());
    }

    /**
     * 禁用/启用流量用户
     *
     * @param internetUserId 流量用户id
     * @return 结果
     */
    @Override
    @Transactional
    public boolean status(Long internetUserId) {
        SysUser sysUser = xhpcInternetUserMapper.getUserByInternetUserId(internetUserId);
        if (null != sysUser) {
            sysUser.setStatus(String.valueOf(UserConstants.NORMAL == Integer.parseInt(sysUser.getStatus()) ? UserConstants.EXCEPTION : UserConstants.NORMAL));
            xhpcUserMapper.update(sysUser);
            XhpcInternetUser xhpcInternetUser = new XhpcInternetUser();
            xhpcInternetUser.setInternetUserId(internetUserId);
            xhpcInternetUser.setStatus(Integer.parseInt(sysUser.getStatus()));
            update(xhpcInternetUser);
            return true;
        }
        return false;
    }

    /**
     * 流量用户列表
     *
     * @param name 流量用户名称
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> list(String name) {
        return xhpcInternetUserMapper.list(name);
    }

    @Override
    public XhpcInternetUser getOperatorIdEvcs(String operatorIdEvcs) {

        return xhpcInternetUserMapper.getOperatorIdEvcs(operatorIdEvcs);
    }

    @Override
    public int getCountOperatorIdEvcs(Long internetUserId,String operatorIdEvcs) {

        return xhpcInternetUserMapper.getCountOperatorIdEvcs(internetUserId, operatorIdEvcs);
    }

}
