package com.zcloud.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcloud.common.utils.PageUtils;
import com.zcloud.modules.sys.dao.SysUserDao;
import com.zcloud.modules.sys.entity.SysUserEntity;
import com.zcloud.modules.sys.service.SysDictionariesService;
import com.zcloud.modules.sys.service.SysUserRoleService;
import com.zcloud.modules.sys.service.SysUserService;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern;


/**
 * 系统用户
 */
@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {
    @Autowired
    private SysUserRoleService sysUserRoleService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<Map<String, Object>> page = new Page<>(Integer.parseInt(params.get("curPage").toString()), Integer.parseInt(params.get("limit").toString()));
        params.put("isDelete", 0);
        IPage<SysUserEntity> ipage = baseMapper.listPage(page, params);
        return new PageUtils(ipage);
    }

    @Override
    public SysUserEntity queryById(String userId) {
        return baseMapper.queryById(userId);
    }

    @Override
    public List<String> queryAllPerms(String userId) {
        return baseMapper.queryAllPerms(userId);
    }

    @Override
    public List<String> queryAllMenuId(String userId) {
        return baseMapper.queryAllMenuId(userId);
    }

    @Override
    public SysUserEntity queryByUserName(String username) {
        return baseMapper.queryByUserName(username);
    }

    @Override
    @Transactional
    public void saveUser(SysUserEntity user) {
        user.setStatus(1);
        //sha256加密
        user.setPassword(new Sha256Hash("666666", user.getUsername()).toHex());
        this.save(user);

    }

    @Override
    @Transactional
    public void update(SysUserEntity user) {

        user.setPassword(new Sha256Hash("Aqsc@2024", user.getUsername()).toHex());
        this.updateById(user);

    }


    @Override
    public boolean updatePassword(String userId, String password, String newPassword) {
        SysUserEntity userEntity = new SysUserEntity();
        userEntity.setPassword(newPassword);
        return this.update(userEntity,
                new QueryWrapper<SysUserEntity>().eq("user_id", userId).eq("password", password));
    }

    @Override
    public List<SysUserEntity> listAll(Map<String, Object> params) {
        return baseMapper.listAll(params);
    }

    @Override
    public List<SysUserEntity> listForSelect(Map<String, Object> params){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.ne("user_id","1");
        queryWrapper.isNull("corpinfo_id");
        return baseMapper.selectList(queryWrapper);
    }

    private String get32UUID() {
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
        return uuid;
    }

    /**
     * 校验手机号
     *
     * @param phoneNumber
     * @return
     */
    public static boolean isValidPhoneNumber(String phoneNumber) {
        if ((phoneNumber != null) && (!phoneNumber.isEmpty())) {
            return Pattern.matches("^1[3-9]\\d{9}$", phoneNumber);
        }
        return false;
    }

    /**
     * 校验邮箱
     *
     * @param email
     * @return
     */
    public static boolean isValidEmail(String email) {
        if ((email != null) && (!email.isEmpty())) {
            return Pattern.matches("^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$", email);
        }
        return false;
    }

}
