package com.ssm.manage.service.impl;

import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.SsmSysRoleDao;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.entity.SysRole;
import com.ssm.manage.query.RoleRequest;
import com.ssm.manage.response.UserResponse;
import com.ssm.manage.service.SsmSysRoleService;
import com.ssm.manage.utils.Constant;
import com.ssm.util.RedisClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.Random;

@Service
public class SsmSysRoleServiceImpl extends BaseServiceImpl<SysRole> implements SsmSysRoleService {

    @Autowired
    private SsmSysRoleDao dao;

    @Override
    public SsmSysRoleDao getDao() {
        return dao;
    }

    @Autowired
    public RedisClient<UserResponse> redisClient;

    @Override
    public PageInfo<SysRole> selectPage(RoleRequest roleRequest) {
        return dao.selectPage(roleRequest);
    }

    @Override
    public int saveSysRole(SysRole sysRole, String userNum) throws SmsException {
        //从redis获取当前操作人信息 将添加用户的创建人和创建时间进行赋值
        if (redisClient.hasKey(getKey(userNum))){
            UserResponse userResponse = redisClient.get(getKey(userNum));
            sysRole.setFounder(userResponse.getUserName());
            sysRole.setFounderDatetime(new Date());
        }
        //设置主键
        sysRole.setRoleId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        sysRole.setState(1);
        int i = dao.saveNotNull(sysRole);
        return i;
    }

    @Override
    public int updateSysRole(SysRole sysRole, String userNum) throws SmsException {
        //从redis获取当前操作人信息 将修改用户的修改人和修改时间进行赋值
        if (redisClient.hasKey(getKey(userNum))){
            UserResponse userResponse = redisClient.get(getKey(userNum));
            sysRole.setModifyBy(userResponse.getUserName());
            sysRole.setModifyDatetime(new Date());
        }
        int i = dao.updateByPrimaryKeySelective(sysRole);
        return i;
    }

    @Override
    public int updateSysRoleState(RoleRequest roleRequest, String userNum){
        Example example = new Example(SysRole.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleId",roleRequest.getRoleId());
        SysRole sysUser = new SysRole();
        sysUser.setState(roleRequest.getState());
        //从redis获取当前操作人信息 将修改用户的修改人和修改时间进行赋值
        if (redisClient.hasKey(getKey(userNum))){
            UserResponse userResponse = redisClient.get(getKey(userNum));
            sysUser.setModifyBy(userResponse.getUserName());
            sysUser.setModifyDatetime(new Date());
        }
        return dao.updateByExampleSelective(sysUser,example);
    }


    public String getKey(String userNum){
        String key = "";
        if (StringUtils.isNotBlank(userNum)){
            key = Constant.userKey + userNum;
        }
        return key;
    }

}
