package cn.iocoder.yudao.module.member.service.admincard;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.admincard.vo.AdminCardPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.admincard.vo.AdminCardSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.admincard.AdminCardDO;
import cn.iocoder.yudao.module.member.dal.mysql.admincard.AdminCardMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.ADMIN_CARD_NOT_EXISTS;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.CARD_NOT_EXIST;

/**
 * 管理员卡 Service 实现类
 *
 * @author 开发账号
 */
@Service
@Validated
public class AdminCardServiceImpl implements AdminCardService {

    @Resource
    private AdminCardMapper adminCardMapper;

    @Override
    public Long createAdminCard(AdminCardSaveReqVO createReqVO) {
        // 插入
        AdminCardDO adminCard = BeanUtils.toBean(createReqVO, AdminCardDO.class);
        adminCardMapper.insert(adminCard);
        // 返回
        return adminCard.getId();
    }

    @Override
    public void updateAdminCard(AdminCardSaveReqVO updateReqVO) {
        // 校验存在
        validateAdminCardExists(updateReqVO.getId());
        // 更新
        AdminCardDO updateObj = BeanUtils.toBean(updateReqVO, AdminCardDO.class);
        adminCardMapper.updateById(updateObj);
    }

    @Override
    public void deleteAdminCard(Long id) {
        // 校验存在
        validateAdminCardExists(id);
        // 删除
        adminCardMapper.deleteById(id);
    }

    private void validateAdminCardExists(Long id) {
        if (adminCardMapper.selectById(id) == null) {
            throw exception(ADMIN_CARD_NOT_EXISTS);
        }
    }

    @Override
    public AdminCardDO getAdminCard(Long id) {
        return adminCardMapper.selectById(id);
    }

    @Override
    public PageResult<AdminCardDO> getAdminCardPage(AdminCardPageReqVO pageReqVO) {
        return adminCardMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AdminCardDO> getAdminCardList() {
        return adminCardMapper.selectList(Wrappers.<AdminCardDO>lambdaQuery()
                .eq(AdminCardDO::getType,"1"));
    }

    @Override
    public void checkCard(String cardId) {
        LambdaQueryWrapper<AdminCardDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminCardDO::getCardNo,cardId);
        List<AdminCardDO> adminCardDOS = adminCardMapper.selectList(wrapper);
        if(CollectionUtil.isEmpty(adminCardDOS)){
            throw new ServiceException(CARD_NOT_EXIST);
        }
    }
}