package com.xhpc.user.service.impl;

import com.xhpc.common.core.constant.UserConstants;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.api.model.LoginUser;
import com.xhpc.user.domain.XhpcOperator;
import com.xhpc.user.mapper.XhpcOperatorMapper;
import com.xhpc.user.mapper.XhpcUserMapper;
import com.xhpc.user.service.IXhpcOperatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 运营商信息 服务层
 *
 * @author ruoyi
 */
@Service
public class XhpcOperatorServiceImpl implements IXhpcOperatorService {

    @Autowired
    private XhpcOperatorMapper xhpcOperatorMapper;

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

    /**
     * 新增任务
     *
     * @param xhpcOperator 运营商信息
     */
    @Override
    @Transactional
    public int insert(XhpcOperator xhpcOperator) {
        return xhpcOperatorMapper.insert(xhpcOperator);
    }

    /**
     * 更新运营商
     *
     * @param xhpcOperator 运营商信息
     */
    @Override
    @Transactional
    public int update(XhpcOperator xhpcOperator) {
        return xhpcOperatorMapper.update(xhpcOperator);
    }

    /**
     * 批量删除运营商信息
     *
     * @param ids 需要删除的运营商ID
     * @return 结果
     */
    @Override
    @Transactional
    public void deleteByIds(String ids) {
        for (String id : ids.split(",")) {
            SysUser sysUser = xhpcOperatorMapper.getUserByOperatorId(Long.parseLong(id));
            if (null != sysUser) {
                removeUser(sysUser.getUserId());
            }
        }
        xhpcOperatorMapper.deleteByIds(ids.split(","));
    }

    /**
     * 校验账号是否唯一
     *
     * @param xhpcOperator 运营商信息
     * @return
     */
    @Override
    public String checkAccountUnique(XhpcOperator xhpcOperator) {
        Long operatorId = StringUtils.isNull(xhpcOperator.getOperatorId()) ? -1L : xhpcOperator.getOperatorId();
        XhpcOperator info = xhpcOperatorMapper.checkAccountUnique(xhpcOperator.getPhone());
        if (StringUtils.isNotNull(info) && info.getOperatorId().longValue() != operatorId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 查询运营商详情
     *
     * @param operatorId 运营商id
     * @return 结果
     */
    @Override
    public Map<String, Object> info(Long operatorId) {
        Map<String, Object> info = xhpcOperatorMapper.info(operatorId);
        return xhpcOperatorMapper.info(operatorId);
    }

    /**
     * 获取运营商分页列表
     *
     * @param name         运营商名称
     * @param contactName  联系人
     * @param contactPhone 联系人电话
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> selectOperatorList(String tenantId,Long operatorId,String name, String contactName, String contactPhone, String createTimeStart, String createTimeEnd) {
        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        //获取登陆用户
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            if (UserTypeUtil.SYS_SUPER_TYPE_ONE.equals(loginUser.getSuperType())) {
                return xhpcOperatorMapper.selectOperatorList(operatorId,name, contactName, contactPhone, createTimeStart, createTimeEnd,loginUser.getTenantId(),0,sysUserId);
            }
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                Long logOperatorId = sysUser.getOperatorId();
                return xhpcOperatorMapper.selectOperatorList(operatorId,name, contactName, contactPhone, createTimeStart, createTimeEnd,tenantId,1,logOperatorId);
                //运营商看自己的场站
            }else{
                //查询赋值的场站
                return xhpcOperatorMapper.selectOperatorList(operatorId,name, contactName, contactPhone, createTimeStart, createTimeEnd,tenantId,2,sysUserId);
            }
        }else{
            return xhpcOperatorMapper.selectOperatorList(operatorId,name, contactName, contactPhone, createTimeStart, createTimeEnd,tenantId,0,sysUserId);
        }



    }

    /**
     * 删除用户
     *
     * @param userId 用户id
     * @return 结果
     */
    @Override
    @Transactional
    public int removeUser(Long userId) {
        // 删除用户与角色关联
        xhpcOperatorMapper.deleteUserRoleByUserId(userId);
        // 删除用户与岗位表
        xhpcOperatorMapper.deleteUserPostByUserId(userId);
        Map<String, Object> map = xhpcUserMapper.pcInfo(userId);
        if (StringUtils.isNotNull(map)) {
            String userName = StringUtils.valueOf(map.get("userName"));
            // 删除用户与岗位表
            xhpcOperatorMapper.deleteRoleByName(userName);
        }
        SysUser sysUser = new SysUser();
        sysUser.setUserId(userId);
        sysUser.setDelFlag(String.valueOf(UserConstants.DEL_FLAG));
        return xhpcOperatorMapper.deleteUserById(sysUser);
    }

    /**
     * 禁用/启用运营商
     *
     * @param operatorId 运营商id
     * @return 结果
     */
    @Override
    @Transactional
    public void status(Long operatorId) {
        SysUser sysUser = xhpcOperatorMapper.getUserByOperatorId(operatorId);
        sysUser.setStatus(String.valueOf(UserConstants.NORMAL == Integer.parseInt(sysUser.getStatus()) ? UserConstants.EXCEPTION : UserConstants.NORMAL));
        xhpcUserMapper.update(sysUser);
        List<SysUser> sysUserList = xhpcUserMapper.selectUserByCreateBy(sysUser.getUserName());
        if (null != sysUserList && sysUserList.size() > 0) {
            for (SysUser user : sysUserList) {
                user.setStatus(String.valueOf(UserConstants.NORMAL == Integer.parseInt(sysUser.getStatus()) ? UserConstants.EXCEPTION : UserConstants.NORMAL));
                xhpcUserMapper.update(user);
            }
        }
        XhpcOperator xhpcOperator = new XhpcOperator();
        xhpcOperator.setOperatorId(operatorId);
        xhpcOperator.setStatus(UserConstants.NORMAL == Integer.parseInt(sysUser.getStatus()) ? UserConstants.EXCEPTION : UserConstants.NORMAL);
        update(xhpcOperator);
    }


    @Override
    public List<XhpcOperator> getOperatorListByTenantId(String tenantId){
        return xhpcOperatorMapper.selectListByTenantId(tenantId);
    }

}
