package com.fs.qw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.qw.domain.QwWorkLink;
import com.fs.qw.domain.QwWorkShortLink;
import com.fs.qw.mapper.QwWorkLinkMapper;
import com.fs.qw.mapper.QwWorkShortLinkMapper;
import com.fs.qw.service.IQwWorkShortLinkService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 企微获客链接短链Service业务层处理
 * 
 * @author fs
 * @date 2025-03-27
 */
@Service
@AllArgsConstructor
public class QwWorkShortLinkServiceImpl extends ServiceImpl<QwWorkShortLinkMapper, QwWorkShortLink> implements IQwWorkShortLinkService {

    private final QwWorkLinkMapper qwWorkLinkMapper;

    /**
     * 查询企微获客链接短链
     * 
     * @param id 企微获客链接短链主键
     * @return 企微获客链接短链
     */
    @Override
    public QwWorkShortLink selectQwWorkShortLinkById(Long id)
    {
        return baseMapper.selectQwWorkShortLinkById(id);
    }

    /**
     * 查询企微获客链接短链列表
     * 
     * @param qwWorkShortLink 企微获客链接短链
     * @return 企微获客链接短链
     */
    @Override
    public List<QwWorkShortLink> selectQwWorkShortLinkList(QwWorkShortLink qwWorkShortLink)
    {
        return baseMapper.selectQwWorkShortLinkList(qwWorkShortLink);
    }

    /**
     * 新增企微获客链接短链
     * 
     * @param qwWorkShortLink 企微获客链接短链
     * @return 结果
     */
    @Override
    public int insertQwWorkShortLink(QwWorkShortLink qwWorkShortLink)
    {
        return baseMapper.insertQwWorkShortLink(qwWorkShortLink);
    }

    /**
     * 修改企微获客链接短链
     * 
     * @param qwWorkShortLink 企微获客链接短链
     * @return 结果
     */
    @Override
    public int updateQwWorkShortLink(QwWorkShortLink qwWorkShortLink)
    {
        return baseMapper.updateQwWorkShortLink(qwWorkShortLink);
    }

    /**
     * 批量删除企微获客链接短链
     * 
     * @param ids 需要删除的企微获客链接短链主键
     * @return 结果
     */
    @Override
    public int deleteQwWorkShortLinkByIds(Long[] ids)
    {
        return baseMapper.deleteQwWorkShortLinkByIds(ids);
    }

    /**
     * 删除企微获客链接短链信息
     * 
     * @param id 企微获客链接短链主键
     * @return 结果
     */
    @Override
    public int deleteQwWorkShortLinkById(Long id)
    {
        return baseMapper.deleteQwWorkShortLinkById(id);
    }

    @Override
    public void addLink(QwWorkLink qwWorkLink) {
        QwWorkShortLink link = new QwWorkShortLink();
        link.setNo(qwWorkLink.getNo());
        link.setUrl("https://rt2523.yjfzy.com/q/" +  qwWorkLink.getNo());
        link.setType(0);
        save(link);
    }

    @Override
    public String getUrl(String no) {
        QwWorkLink one = qwWorkLinkMapper.selectOne(new QueryWrapper<QwWorkLink>().eq("no", no));
        if(one == null){
            return null;
        }
        return one.getUrl();
    }

    @Override
    public String addWxLink(QwWorkLink qwWorkLink, String workShortNo) {
        QwWorkShortLink link = new QwWorkShortLink();
        link.setNo(workShortNo);
        link.setUrl("https://qwshort.yjfzy.com/q/" +  link.getNo());
        link.setRedirectUrl(qwWorkLink.getUrl() + "?customer_channel=" + link.getNo());
        link.setWorkLinkId(qwWorkLink.getId());
        save(link);
        return link.getUrl();
    }
}
