package com.ssm.manage.service.impl;

import com.ssm.core.utils.AESUtils;
import com.ssm.core.utils.RedisUtils;
import com.ssm.manage.dao.RlMenuPowerDao;
import com.ssm.manage.dao.RlStaffDao;
import com.ssm.manage.dto.RlStaffDto;
import com.ssm.manage.entity.RlStaff;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.RlStaffVo;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.RlStaffService;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ssm.manage.dao.BaseDao;
import com.ssm.core.impl.BaseDaoImpl;

import java.io.Serializable;
import java.util.List;
import java.util.UUID;

import tk.mybatis.mapper.entity.Example;

/**
 * 门店员工档案(RlStaff)表服务实现类
 *
 * @author makejava
 * @since 2023-12-13 15:34:08
 */
@Service("rlStaffService")
public class RlStaffServiceImpl extends BaseServiceImpl<RlStaff> implements RlStaffService {
    @Autowired
    private RlStaffDao rlStaffDao;
    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private RlMenuPowerDao rlMenuPowerDao;

    @Override
    public BaseDao<RlStaff> getDao() {
        return rlStaffDao;
    }

    @Override
    public PageInfo<RlStaff> selectPage(RlStaffDto dto) {
        Example example = getExample();
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<RlStaff> list = this.rlStaffDao.selectByExample(example);
        PageInfo<RlStaff> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public RlStaffVo phoneLogin(RlStaffDto rlStaffDto) throws SmsException {
        //查询用户是否存在
        Example example = getExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(RlStaffDto.Fields.mobilePhone,rlStaffDto.getMobilePhone());
        List<RlStaff> rlStaffs = rlStaffDao.selectByExample(example);
        if(rlStaffs==null || rlStaffs.size()==0){
            throw new SmsException(ResponseCode.MSG,"密码或手机号错误");
        }
        RlStaff rlStaff = rlStaffs.get(0);
        //比较密码
        if (!AESUtils.encrypt(rlStaffDto.getPassword()).equals(rlStaff.getPassword())){
            throw new SmsException(ResponseCode.MSG,"密码或手机号错误");
        }
        //写入redis

        RlStaffVo rlStaffVo = new RlStaffVo();
        String token = UUID.randomUUID().toString();
        rlStaffVo.setToken(token);
        redisUtils.setStaff(rlStaffVo);
        return rlStaffVo;
    }

    private Example getExample() {
        Example example = new Example(RlStaff.class);
        return example;
    }
}
