package com.example.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Adminstore;
import com.example.exception.CustomException;
import com.example.mapper.AdminstoreMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 管理员业务处理
 **/
@Service
public class AdminStoreService {

    @Resource
    private AdminstoreMapper adminstoreMapper;

    /**
     * 注册成为店家
     * @param account
     */
    public void register(Account account) {
        Adminstore adminstore = adminstoreMapper.selectByUsername(account.getUsername());
        if(!ObjectUtils.isEmpty(adminstore)) {
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR) ;
        }
        Adminstore adminStore = new Adminstore();
        BeanUtils.copyProperties(account, adminStore);
        add(adminStore);
    }

    /**
     * 新增
     */
    public void add(Adminstore adminstore) {
        adminstoreMapper.insert(adminstore);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        adminstoreMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            adminstoreMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Adminstore adminstore) {
        adminstoreMapper.updateById(adminstore);
    }

    /**
     * 根据ID查询
     */
    public Adminstore selectById(Integer id) {
        return adminstoreMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Adminstore> selectAll(Adminstore adminstore) {
        return adminstoreMapper.selectAll(adminstore);
    }

    /**
     * 分页查询
     */
    public PageInfo<Adminstore> selectPage(Adminstore adminstore, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Adminstore> list = adminstoreMapper.selectAll(adminstore);
        return PageInfo.of(list);
    }


    /**
     * 店铺管理员 登陆
     * @param account
     * @return
     */
    public Account login(Account account) {
        Account dbAdminkeeper = adminstoreMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(dbAdminkeeper)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        if (!account.getPassword().equals(dbAdminkeeper.getPassword())) {
            throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
        }
        // 生成token
        String tokenData = dbAdminkeeper.getId() + "-" + RoleEnum.STOREKEEPER.name();
        String token = TokenUtils.createToken(tokenData, dbAdminkeeper.getPassword());
        dbAdminkeeper.setToken(token);
        return dbAdminkeeper;
    }
}