package com.liang.service.impl;

import com.liang.common.utils.*;
import com.liang.entity.SysUserInfo;
import com.liang.dao.SysUserInfoDao;
import com.liang.service.SysUserInfoService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * (SysUserInfo)表服务实现类
 *
 * @author makejava
 * @since 2023-02-19 21:37:39
 */
@Service("sysUserInfoService")
public class SysUserInfoServiceImpl implements SysUserInfoService {
    @Resource
    private SysUserInfoDao sysUserInfoDao;



    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public SysUserInfo queryById(Integer userId) {
        return this.sysUserInfoDao.queryById(userId);
    }

    /**
     * 分页查询
     *
     * @param sysUserInfo 筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<SysUserInfo> queryByPage(SysUserInfo sysUserInfo, PageRequest pageRequest) {
        long total = this.sysUserInfoDao.count(sysUserInfo);
        return new PageImpl<>(this.sysUserInfoDao.queryAllByLimit(sysUserInfo, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param sysUserInfo 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(SysUserInfo sysUserInfo) {
        return this.sysUserInfoDao.insert(sysUserInfo);
    }

    /**
     * 修改数据
     *
     * @param sysUserInfo 实例对象
     * @return 实例对象
     */
    @Override
    public int update(SysUserInfo sysUserInfo) {
        return this.sysUserInfoDao.update(sysUserInfo);
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userId, Integer updateUser) {
        return this.sysUserInfoDao.deleteById(userId, updateUser) > 0;
    }

    /**
     * 获取用户列表
     *
     * @param map
     * @return
     */
    @Override
    public List<SysUserInfo> getSysUserInfoList(Map map) {
        return this.sysUserInfoDao.getSysUserInfoList(map);
    }

    @Override
    public List<SysUserInfo> getGw(Map map) {
        return this.sysUserInfoDao.getGw(map);
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @Override
    public SysUserInfo getSysUserInfo(Map map) {
        return this.sysUserInfoDao.getSysUserInfo(map);
    }

    /**
     * 批量删除系统用户
     *
     * @param userIdList
     * @return
     */
    @Override
    public int batchDelete(List<Integer> userIdList, Integer updateUser) {
        return this.sysUserInfoDao.batchDelete(userIdList, updateUser);
    }

    /**
     * 删除一个系统用户
     *
     * @param userId
     * @return
     */
    @Override
    public int delete(Integer userId) {
        return this.sysUserInfoDao.delete(userId);
    }

    /**
     * 校验用户账号是否唯一
     *
     * @param account 用户账号
     * @return
     */
    @Override
    public int checkUserNameUnique(String account) {
        return this.sysUserInfoDao.checkUserNameUnique(account);
    }

    /**
     * 新增用户
     *
     * @param userInfo
     * @return
     */
    @Override
    public int doAdd(SysUserInfo userInfo) {
        return this.sysUserInfoDao.doAdd(userInfo);
    }

    @Override
    public int doUpdate(SysUserInfo userInfo) {
        return this.sysUserInfoDao.doUpdate(userInfo);
    }

    @Override
    public int resetPass(SysUserInfo userInfo) {
        return this.sysUserInfoDao.resetPass(userInfo);
    }

    @Override
    public SysUserInfo getOneById(Integer userId) {
        return this.sysUserInfoDao.getOneById(userId);
    }

    /**
     * 获取用户token
     *
     * @return
     */
    public String getUserKey() {
        HttpServletRequest request = ServletUtils.getRequest();
        String token = "";
        if (request != null) {
            // 从请求中获取token，先从Header里取，取不到的话再从cookie里取（适配前后端分离的模式）
            token = request.getHeader(SystemConst.SYSTEM_USER_TOKEN);
            if (StringUtils.isBlank(token)) {
                token = CookieUtils.getCookie(request, SystemConst.SYSTEM_USER_TOKEN);
            }
            if (StringUtils.isNotBlank(token) && token.startsWith(SystemConst.TOKEN_PREFIX)) {
                token = token.replace(SystemConst.TOKEN_PREFIX, "");
                token = JwtTokenUtil.parseJWT(token);
            }
        }
        return token;
    }

    @Override
    public List<Map<String, String>> getCJR() {
        return this.sysUserInfoDao.getCJR();
    }

    /**
     * 通过account获取一条记录
     *
     * @param account
     * @return
     */
    @Override
    public SysUserInfo getUserByAccount(String account) {
        List<SysUserInfo> list = this.sysUserInfoDao.getUserByAccount(account);
        if (list.size() > 0)
            return list.get(0);
        else
            return null;
    }

    @Override
    public String getDeptNameByAccount(String dept_id) {
        return sysUserInfoDao.getDeptNameByAccount(dept_id);
    }

    @Override
    public List<String> getPostNameByUserId(Integer userId) {
        return this.sysUserInfoDao.getPostNameByUserId(userId);
    }

    @Override
    public List<SysUserInfo> getDept(Integer id) {
        return this.sysUserInfoDao.getDept(id);
    }

    @Override
    public List<SysUserInfo> getDeptName() {
        return this.sysUserInfoDao.getDeptName();
    }

    @Override
    public String getBzs(Integer id) {
        return this.sysUserInfoDao.getBzs(id);
    }


}
