package com.xhpc.user.service.impl;

import com.xhpc.common.core.constant.StatusConstants;
import com.xhpc.common.core.constant.UserConstants;
import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.service.BaseService;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.system.api.domain.SysRole;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.api.model.LoginUser;
import com.xhpc.user.domain.SysUserRole;
import com.xhpc.user.mapper.XhpcUserMapper;
import com.xhpc.user.service.IXhpcUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户信息 服务层
 *
 * @author ruoyi
 */
@Service
public class XhpcUserServiceImpl extends BaseService implements IXhpcUserService {

    @Autowired
    private XhpcUserMapper xhpcUserMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 批量删除用户信息
     *
     * @param ids 需要删除的用户ID
     * @return 结果
     */
    @Override
    @Transactional
    public void deleteByIds(Long[] ids) {
        xhpcUserMapper.deleteByIds(ids);
    }

    /**
     * 获取平台用户分页列表
     *
     * @param userName 用户账号
     * @param nickName 用户昵称
     * @return 结果
     */
    public List<Map<String, Object>> selectPcUserList(String userName, String nickName) {
        LoginUser loginUser = tokenService.getLoginUser();
        return xhpcUserMapper.selectPcUserList(userName, nickName,loginUser.getTenantId());
    }

    /**
     * 平台用户详情
     *
     * @param userId 用户id
     * @return 结果
     */
    @Override
    public SysUser pcInfo(Long userId) {
        return xhpcUserMapper.selectUserByUserId(userId);
    }

    /**
     * 获取运营商用户分页列表
     *
     * @param userName   用户账号
     * @param nickName   用户昵称
     * @param operatorId 运营上id
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> selectOperatorUserList(String userName, String nickName, Long operatorId) {
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = xhpcUserMapper.selectUserByUserId(userId);
        startPage();
        if (StatusConstants.SYSTEM_USER_TYPE.equals(sysUser.getUserType())) {
            return xhpcUserMapper.selectOperatorUserList(userName, nickName, operatorId,sysUser.getUserType(),sysUser.getTenantId());
        } else {
            return xhpcUserMapper.selectOperatorUserList(userName, nickName, sysUser.getOperatorId(),sysUser.getUserType(),sysUser.getTenantId());
        }
    }

    /**
     * 运营商用户详情
     *
     * @param userId 用户id
     * @return 结果
     */
    @Override
    public Map<String, Object> operatorInfo(Long userId) {
        return xhpcUserMapper.operatorInfo(userId);
    }

    /**
     * 禁用/启用平台和运营商子账号
     *
     * @param userId 用户id
     * @return 结果
     */
    @Override
    @Transactional
    public void status(Long userId) {
        SysUser sysUser = xhpcUserMapper.selectUserByUserId(userId);
        sysUser.setStatus(String.valueOf(UserConstants.NORMAL == Integer.parseInt(sysUser.getStatus()) ? UserConstants.EXCEPTION : UserConstants.NORMAL));
        xhpcUserMapper.update(sysUser);
    }

    /**
     * 通过用户账号查询用户详情
     *
     * @param userName 用户张号
     * @return 结果
     */
    @Override
    public SysUser selectUserByUserName(String userName) {
        return xhpcUserMapper.selectUserByUserName(userName);
    }

    /**
     * 通过用户账号查询角色详情
     *
     * @param userName 用户帐号
     * @return 结果
     */
    @Override
    public SysRole selectUserByRoleName(String userName) {
        return xhpcUserMapper.selectUserByRoleName(userName);
    }

    /**
     * 新增角色信息
     *
     * @param role 角色信息
     * @return
     */
    @Override
    public int insertRole(SysRole role) {
        return xhpcUserMapper.insertRole(role);
    }


    /**
     * 登录账号是否存在
     *
     * @param userName 账号
     * @return
     */
    @Override
    public int checkUserNameUnique(String userName,Long userId) {
        return xhpcUserMapper.checkUserNameUnique(userName,userId);
    }


    /**
     * 新增保存用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public SysUser insertUser(SysUser user) {
        user.setCreateBy(SecurityUtils.getUsername());
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        // 新增用户信息
        xhpcUserMapper.insertUser(user);
        // 新增用户与角色管理
        insertUserRole(user);
        return user;
    }

    /**
     * 新增用户角色信息
     *
     * @param user 用户对象
     */
    public void insertUserRole(SysUser user) {
        Long[] roles = user.getRoleIds();
        if (StringUtils.isNotNull(roles)) {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<SysUserRole>();
            for (Long roleId : roles) {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(user.getUserId());
                ur.setRoleId(roleId);
                ur.setTenantId(user.getTenantId());
                list.add(ur);
            }
            if (list.size() > 0) {
                xhpcUserMapper.batchUserRole(list);
            }
        }
    }

    /**
     * 修改保存用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateUser(SysUser user) {
        return xhpcUserMapper.update(user);
    }

    @Override
    public void addXhpcDataDimension(Long userId, Long chargingStationId) {
        xhpcUserMapper.addXhpcDataDimension(userId,chargingStationId);
    }

    @Override
    public void updateXhpcDataDimension(Long userId) {
        xhpcUserMapper.updateXhpcDataDimension(userId);
    }

    @Override
    public List<Map<String, Object>> getUserDimension(Integer activity, Integer frequency, BigDecimal amount, String startTime, String endTime,String phone) {

        LoginUser loginUser = tokenService.getLoginUser();
        String tenantId = loginUser.getTenantId();

        //时间范围内的所有订单总数
       // int orderTimeNumber = xhpcUserMapper.getOrderTimeNumber(null, startTime, endTime, tenantId);
        startPage();
        List<Map<String, Object>> userDimension = xhpcUserMapper.getUserDimension(null, startTime, endTime, tenantId,activity,frequency,amount,phone);

        return userDimension;
    }

    /**
     * 修改保存流量方用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public int updateInternetUser(SysUser user) {
        return xhpcUserMapper.updateInternetUser(user);
    }


}
