package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.mapper.ZccmUserAdminMapper;
import com.hc.business.model.ZccmUserAdmin;
import com.hc.business.model.jwt.GenerateAuthToken;
import com.hc.business.model.login.AccessEntity;
import com.hc.business.model.login.AuthEntity;
import com.hc.business.service.ZccmUserAdminService;
import com.hc.core.utils.enums.InfoEnum;
import com.hc.core.utils.exception.ResponseException;
import com.hc.core.utils.tool.IdGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * 管理员表(ZccmUserAdmin)表服务实现类
 *
 * @author java
 * @since 2020-11-16 13:45:07
 */

@Service("userAdminService")
public class ZccmUserAdminServiceImpl implements ZccmUserAdminService {

    private final ZccmUserAdminMapper userAdminMapper;

    private final GenerateAuthToken generateAuthToken;

    public ZccmUserAdminServiceImpl(ZccmUserAdminMapper userAdminMapper,
                                GenerateAuthToken generateAuthToken) {
        this.userAdminMapper = userAdminMapper;
        this.generateAuthToken = generateAuthToken;
    }

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

    /**
     * 新增数据
     *
     * @param userAdmin 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmUserAdmin insert(ZccmUserAdmin userAdmin) {
        userAdmin.setPermissionLevel(2);
        userAdmin.setAdminPass(IdGenerator.passwordToHash((userAdmin.getAdminPass())));
        this.userAdminMapper.insert(userAdmin);
        return userAdmin;
    }

    /**
     * 修改数据
     *
     * @param userAdmin 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmUserAdmin update(ZccmUserAdmin userAdmin) {
        String adminPass = userAdmin.getAdminPass();
        if (!Objects.isNull(adminPass)) {
            userAdmin.setAdminPass(IdGenerator.passwordToHash(adminPass));
        }
        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) {

        String adminAccount = authEntity.getUserName().trim();
        //依据用户名查询管理员
        List<ZccmUserAdmin> userAdmins = userAdminMapper.selectUserAdmin(adminAccount);
        if (CollectionUtils.isEmpty(userAdmins)) {
            throw new ResponseException(InfoEnum.LOGIN_USER_NAME_ERROR);
        }
        ZccmUserAdmin 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);
    }
}