package cn.newfeifan.mall.module.distri.service.sharelinkregister;

import cn.newfeifan.mall.module.distri.controller.app.sharelinkregister.vo.AppShareLinkRegisterPageReqVO;
import cn.newfeifan.mall.module.distri.controller.app.sharelinkregister.vo.AppShareLinkRegisterSaveReqVO;
import cn.newfeifan.mall.module.distri.service.sharelink.ShareLinkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import cn.newfeifan.mall.module.distri.dal.dataobject.sharelinkregister.ShareLinkRegisterDO;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;

import cn.newfeifan.mall.module.distri.dal.mysql.sharelinkregister.ShareLinkRegisterMapper;

import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.newfeifan.mall.module.distri.enums.ErrorCodeConstants.*;

/**
 * 通过分享链接，新注册用户 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class ShareLinkRegisterServiceImpl implements ShareLinkRegisterService {

    @Resource
    private ShareLinkRegisterMapper shareLinkRegisterMapper;
    @Resource
    private ShareLinkService shareLinkService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createShareLinkRegister(AppShareLinkRegisterSaveReqVO createReqVO) {
        //插入之前先判断有没有再插入
        ShareLinkRegisterDO shareLinkRegisterDO = shareLinkRegisterMapper.selectOne(new LambdaQueryWrapper<ShareLinkRegisterDO>()
                .eq(ShareLinkRegisterDO::getDistriShareLinkId, createReqVO.getDistriShareLinkId())
                .eq(ShareLinkRegisterDO::getRegisterUserId, createReqVO.getRegisterUserId()));
        if (shareLinkRegisterDO != null) {
            return shareLinkRegisterDO.getId();
        }

        // 插入
        ShareLinkRegisterDO shareLinkRegister = BeanUtils.toBean(createReqVO, ShareLinkRegisterDO.class);
        shareLinkRegisterMapper.insert(shareLinkRegister);

        //增加当前链接的累计
        if (createReqVO.getDistriShareLinkId() != null) {
            shareLinkService.addRegisterUserCountById(createReqVO.getDistriShareLinkId());
        }
        // 返回
        return shareLinkRegister.getId();
    }

    @Override
    public void updateShareLinkRegister(AppShareLinkRegisterSaveReqVO updateReqVO) {
        // 校验存在
        validateShareLinkRegisterExists(updateReqVO.getId());
        // 更新
        ShareLinkRegisterDO updateObj = BeanUtils.toBean(updateReqVO, ShareLinkRegisterDO.class);
        shareLinkRegisterMapper.updateById(updateObj);
    }

    @Override
    public void deleteShareLinkRegister(Long id) {
        // 校验存在
        validateShareLinkRegisterExists(id);
        // 删除
        shareLinkRegisterMapper.deleteById(id);
    }

    private void validateShareLinkRegisterExists(Long id) {
        if (shareLinkRegisterMapper.selectById(id) == null) {
            throw exception(SHARE_LINK_REGISTER_NOT_EXISTS);
        }
    }

    @Override
    public ShareLinkRegisterDO getShareLinkRegister(Long id) {
        return shareLinkRegisterMapper.selectById(id);
    }

    @Override
    public PageResult<ShareLinkRegisterDO> getShareLinkRegisterPage(AppShareLinkRegisterPageReqVO pageReqVO) {
        return shareLinkRegisterMapper.selectPage(pageReqVO);
    }

}