package com.wb.permission.domain;

import com.wb.permission.pojo.WbUser;
import com.wb.tool.Encrypter;
import com.wb.util.SysUtil;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* Description:用户
* Author: wangll
* Version: 1.0
* Create Date: 2020-03-16 21:32:32
*/
@Service
public class WbUserBO {
    @Autowired
    SqlSessionTemplate sqlSessionTemplate;

    /**
     * Description: 查询用户信息
     * @param wbUser
     * @return
     * Note: Nothing much.
     */
    public List<WbUser> listWbUser(WbUser wbUser){
        if (StringUtils.isEmpty(wbUser.getUserId())){
            wbUser.setUserId(null);
        }
        return sqlSessionTemplate.selectList("resources/permission.WbUserMapper.selectWbUser",wbUser);
    }

    /**
    * Description: 新增和修改
    * @param wbUser
    * @return
    * Note: Nothing much.
    */
    public void save(WbUser wbUser){
        if (StringUtils.isEmpty(wbUser.getUserId())) {
            wbUser.setUserId(SysUtil.getId());
            if (!StringUtils.isEmpty(wbUser.getPassword())){
                try {
                    wbUser.setPassword(Encrypter.getMD5(wbUser.getPassword()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            wbUser.setCreateDate(wbUser.getCreatedOn());
            sqlSessionTemplate.insert("resources/permission.WbUserMapper.insertSelective",wbUser);
        }else{
            sqlSessionTemplate.update("resources/permission.WbUserMapper.updateByPrimaryKeySelective",wbUser);
        }
    }

    /**
    * Description: 新增
    * @param wbUser
    * @return
    * Note: Nothing much.
    */
    public void insert(WbUser wbUser){
       sqlSessionTemplate.insert("resources/permission.WbUserMapper.insertSelective",wbUser);
    }

    /**
    * Description: 修改
    * @param wbUser
    * @return
    * Note: Nothing much.
    */
    public void update(WbUser wbUser){
       sqlSessionTemplate.update("resources/permission.WbUserMapper.updateByPrimaryKeySelective",wbUser);
    }

    /**
    * Description: 删除
    * @param userId
    * @return
    * Note: Nothing much.
    */
    public void delete(String userId){
       sqlSessionTemplate.delete("resources/permission.WbUserMapper.deleteByPrimaryKey",userId);
    }

}