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

import cn.hutool.core.util.ObjectUtil;
import cn.newfeifan.mall.module.distri.controller.app.sharelink.vo.AppShareLinkPageReqVO;
import cn.newfeifan.mall.module.distri.controller.app.sharelink.vo.AppShareLinkReqVO;
import cn.newfeifan.mall.module.distri.controller.app.sharelink.vo.AppShareLinkSaveReqVO;
import cn.newfeifan.mall.module.product.dal.dataobject.spu.ProductSpuDO;
import cn.newfeifan.mall.module.product.dal.mysql.spu.ProductSpuMapper;
import cn.newfeifan.mall.module.sale.dal.dataobject.shop.ShopDO;
import cn.newfeifan.mall.module.sale.dal.mysql.shop.ShopMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import cn.newfeifan.mall.module.distri.dal.dataobject.sharelink.ShareLinkDO;
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.sharelink.ShareLinkMapper;

import static cn.newfeifan.mall.framework.common.enums.CategoryEnum.*;
import static cn.newfeifan.mall.framework.common.exception.enums.GlobalErrorCodeConstants.UNAUTHORIZED;
import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.newfeifan.mall.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.newfeifan.mall.module.distri.enums.ErrorCodeConstants.*;


/**
 * 分享链接 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class ShareLinkServiceImpl implements ShareLinkService {

    @Resource
    private ShareLinkMapper shareLinkMapper;

    @Resource
    private ShopMapper shopMapper;

    @Resource
    private ProductSpuMapper productSpuMapper;

    @Override
    public Long createShareLink(AppShareLinkSaveReqVO createReqVO) {
        // 插入
        ShareLinkDO shareLink = BeanUtils.toBean(createReqVO, ShareLinkDO.class);
        shareLinkMapper.insert(shareLink);
        // 返回
        return shareLink.getId();
    }

    @Override
    public void updateShareLink(AppShareLinkSaveReqVO updateReqVO) {
        // 校验存在
        validateShareLinkExists(updateReqVO.getId());
        // 更新
        ShareLinkDO updateObj = BeanUtils.toBean(updateReqVO, ShareLinkDO.class);
        shareLinkMapper.updateById(updateObj);
    }

    @Override
    public void deleteShareLink(Long id) {
        // 校验存在
        validateShareLinkExists(id);
        // 删除
        shareLinkMapper.deleteById(id);
    }

    private void validateShareLinkExists(Long id) {
        if (shareLinkMapper.selectById(id) == null) {
            throw exception(SHARE_LINK_NOT_EXISTS);
        }
    }

    @Override
    public String getShareLink(AppShareLinkReqVO appShareLinkReqVO) {
        //先判断是否存在
        Long loginUserId = getLoginUserId();
        if (ObjectUtil.isEmpty(loginUserId)) {
            throw exception(UNAUTHORIZED);
        }
        ShareLinkDO shareLinkDO = shareLinkMapper.selectByCategory(appShareLinkReqVO.getCategory(), loginUserId, appShareLinkReqVO.getObjectId());

        if (ObjectUtil.isNotEmpty(shareLinkDO)) {
            //存在就直接返回查询出来的id - 十六进制
            return Long.toHexString(shareLinkDO.getId());
        }

        ShareLinkDO shareLink = BeanUtils.toBean(appShareLinkReqVO, ShareLinkDO.class);
        shareLink.setCreateUserId(getLoginUserId());

        //获取对象名
        String name = getObjectName(shareLink.getCategory(), shareLink.getObjectId());
        shareLink.setName(name);

        shareLinkMapper.insert(shareLink);
        return Long.toHexString(shareLink.getId());
    }

    private String getObjectName(Integer category, Long objectId) {
        if (isMall(category)) {
            return MALL.getName();
        } else if (isShop(category)) {
            ShopDO shopDO = shopMapper.selectById(objectId);
            return shopDO.getName();
        } else if (isProduct(category)) {
            ProductSpuDO productSpuDO = productSpuMapper.selectById(objectId);
            return productSpuDO.getName();
        }
        return null;
    }

    private Long getObjectId(Integer category, Long objectId) {
        if (isMall(category)) {
            return 0L;
        } else if (isShop(category)) {
            ShopDO shopDO = shopMapper.selectById(objectId);
            return shopDO.getId();
        } else if (isProduct(category)) {
            ProductSpuDO productSpuDO = productSpuMapper.selectById(objectId);
            return productSpuDO.getId();
        }
        return null;
    }

    @Override
    public PageResult<ShareLinkDO> getShareLinkPage(AppShareLinkPageReqVO pageReqVO) {
        return shareLinkMapper.selectPage(pageReqVO);
    }

    @Override
    public Long getCreateUserIdById(Long id) {
        return shareLinkMapper.selectById(id).getCreateUserId();
    }

    @Override
    public void addRegisterUserCountById(Long id) {
        ShareLinkDO shareLinkDO = shareLinkMapper.selectById(id);

        // 使用帮助方法来增加注册用户计数，并确保操作的直观性
        incrementRegisterUserCount(shareLinkDO);

        shareLinkMapper.updateById(shareLinkDO);
    }


    /**
     * 帮助方法用于增加注册用户计数
     *
     * @param shareLinkDO 要更新的ShareLinkDO实例
     */
    private void incrementRegisterUserCount(ShareLinkDO shareLinkDO) {
        shareLinkDO.setRegisterUserCount(shareLinkDO.getRegisterUserCount() + 1);
    }

    @Override
    public Long getObjectIdByLinkId(Long linkId) {
        ShareLinkDO shareLinkDO = shareLinkMapper.selectById(linkId);
        if (shareLinkDO == null) {
            throw exception(SHARE_LINK_NOT_EXISTS);
        }

        return getObjectId(shareLinkDO.getCategory(), shareLinkDO.getObjectId());
    }
}
