package com.hc.gpbusiness.service.impl;

import com.github.pagehelper.PageHelper;
import com.hc.gpbusiness.mapper.WebUserAdminMapper;
import com.hc.gpbusiness.model.WebUserAdmin;
import com.hc.gpbusiness.model.jwt.GenerateAuthToken;
import com.hc.gpbusiness.model.login.AccessEntity;
import com.hc.gpbusiness.model.login.AuthEntity;
import com.hc.gpbusiness.service.WebUserAdminService;
import com.hc.gpcore.enums.InfoEnum;
import com.hc.gpcore.exception.ResponseException;
import com.hc.gpcore.utils.tool.IdGenerator;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * 管理员表(UserAdmin)表服务实现类
 *
 * @author java
 * @since 2020-07-29 16:23:13
 */

@Service("userAdminService")
public class WebUserAdminServiceImpl implements WebUserAdminService {

    private final WebUserAdminMapper userAdminMapper;

    private final GenerateAuthToken generateAuthToken;

    public WebUserAdminServiceImpl(WebUserAdminMapper userAdminMapper, GenerateAuthToken generateAuthToken) {
        this.userAdminMapper = userAdminMapper;
        this.generateAuthToken = generateAuthToken;
    }

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

    /**
     * 新增数据
     *
     * @param userAdmin 实例对象
     * @return 实例对象
     */
    @Override
    public WebUserAdmin insert(WebUserAdmin userAdmin) {
        this.userAdminMapper.insert(userAdmin);
        return userAdmin;
    }

    /**
     * 修改数据
     *
     * @param userAdmin 实例对象
     * @return 实例对象
     */
    @Override
    public WebUserAdmin update(WebUserAdmin userAdmin) {
        this.userAdminMapper.updateByPrimaryKeySelective(userAdmin);
        return this.queryById(userAdmin.getUserAdminId());
    }

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

    /**
     * 分页查询多条数据
     *
     * @param page 页码
     * @param size 查询条数
     * @return 对象列表
     */
    @Override
    public PageInfo queryAllByLimit(int page, int size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(this.userAdminMapper::selectAll);
    }

    @Override
    public AccessEntity userAdminLogin(AuthEntity authEntity) {
        //依据用户名查询管理员
        List<WebUserAdmin> userAdmins = userAdminMapper.selectAdminByAdminAccount(authEntity.getUserName());
        if (CollectionUtils.isEmpty(userAdmins)) {
            throw new ResponseException(InfoEnum.LOGIN_USER_NAME_ERROR);
        }
        WebUserAdmin userAdmin = userAdmins.get(0);

        //密码错误
        String password = IdGenerator.passwordToHash(authEntity.getPassword());
        if (!Objects.equals(password, userAdmin.getAdminPass())) {
            throw new ResponseException(InfoEnum.LOGIN_PASSWORD_ERROR);
        }

        //验证密码正确  生成token 存入redis并返回
        //登录成功 生成token
        return generateAuthToken.generateToken(authEntity, null, userAdmin);
    }

}