package com.gyee.wisdom.alarm.sharding.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gyee.wisdom.alarm.sharding.entity.AlarmUser;
import com.gyee.wisdom.alarm.sharding.mapper.AlarmUserMapper;
import com.gyee.wisdom.alarm.sharding.util.SnowflakeGenerator;
import com.gyee.wisdom.common.utils.StringUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @descrition:
 * @author:Wanghs
 * @date:2020-04-24
 */
@Service
public class AlarmUserService {

    @Autowired
    private AlarmUserMapper alarmUserMapper;

    public int registerUser(AlarmUser alarmUser) {
        if(alarmUser.getId()<=0){
            long newId= SnowflakeGenerator.generateId();
            alarmUser.setId(newId);
        }

        String password = alarmUser.getPassWord();
        //md5加密,加密盐为gdnxfd
        String md5str = DigestUtils.md5Hex(password + "gdnxfd");
        alarmUser.setPassWord(md5str);
        alarmUser.setIdentity(alarmUser.getIdentity());
        int result = alarmUserMapper.insert(alarmUser);
        return result;
    }

    public AlarmUser login(AlarmUser user) {


        if (StringUtil.isBlank(user.getUserName()) || StringUtil.isBlank(user.getPassWord()))
            return null;
        else {
            String md5str = DigestUtils.md5Hex(user.getPassWord()+"gdnxfd");
            List<AlarmUser> lst = alarmUserMapper.login(user.getUserName(),md5str);
//            List<AlarmUser> lst = alarmUserMapper.getUserByUserName(user.getUserName());
            //.login(user.getUserName(), md5str);

            if (lst.size() > 0) {
                return lst.get(0);
            } else {
                return null;
            }
        }

    }

    public AlarmUser login2(AlarmUser user) {


        if (StringUtil.isBlank(user.getUserName()) || StringUtil.isBlank(user.getPassWord()))
            return null;
        else {
            //String md5str = DigestUtils.md5Hex(user.getPassWord()+"gdnxfd")
            List<AlarmUser> lst = alarmUserMapper.getUserByUserName(user.getUserName());
            //.login(user.getUserName(), md5str);

            if (lst.size() > 0) {
                return lst.get(0);
            } else {
                return null;
            }
        }

    }

    public AlarmUser isExistUser(String userName) {
        AlarmUser user = null;
        List<AlarmUser> lst = alarmUserMapper.getUserByUserName(userName);
        if (lst.size() > 0) {
            user = lst.get(0);
        }
        return user;
    }


    public int updateUser(AlarmUser newData){
        String newpassword = newData.getNewPassWord();
        if(newpassword == null || newpassword.length() == 0){
            AlarmUser alarmUser = alarmUserMapper.selectById(newData.getId());
            newpassword = alarmUser.getPassWord();
            newData.setPassWord(newpassword);
        }
        else {
            //md5加密,加密盐为gdnxfd
            String md5str = DigestUtils.md5Hex(newpassword + "gdnxfd");
            newData.setPassWord(md5str);
        }
        int result = alarmUserMapper.updateById(newData);
        return result;
    }

    public List<AlarmUser> getAllUser(){

        return  alarmUserMapper.getAll();
    }

    public int deleteAlarmUser(String id) {

        int code = alarmUserMapper.deleteByIds(id);
        return code;
    }

}
