package com.fs.qw.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.DateUtils;
import com.fs.qw.domain.QwContactWay;
import com.fs.qw.domain.QwDrainageLink;
import com.fs.qw.mapper.QwContactWayMapper;
import com.fs.qw.mapper.QwDrainageLinkMapper;
import com.fs.qw.service.IQwDrainageLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 引流链接Service业务层处理
 *
 * @author fs
 * @date 2024-07-11
 */
@Service
public class QwDrainageLinkServiceImpl implements IQwDrainageLinkService
{
    @Autowired
    private QwDrainageLinkMapper qwDrainageLinkMapper;
    @Autowired
    private QwContactWayMapper qwContactWayMapper;
    /**
     * 查询引流链接
     *
     * @param id 引流链接主键
     * @return 引流链接
     */
    @Override
    public QwDrainageLink selectQwDrainageLinkById(Long id)
    {
        return qwDrainageLinkMapper.selectQwDrainageLinkById(id);
    }

    /**
     * 查询引流链接列表
     *
     * @param qwDrainageLink 引流链接
     * @return 引流链接
     */
    @Override
    public List<QwDrainageLink> selectQwDrainageLinkList(QwDrainageLink qwDrainageLink)
    {
        return qwDrainageLinkMapper.selectQwDrainageLinkList(qwDrainageLink);
    }

    /**
     * 新增引流链接
     *
     * @param qwDrainageLink 引流链接
     * @return 结果
     */
    @Override
    public int insertQwDrainageLink(QwDrainageLink qwDrainageLink)
    {
        if(qwDrainageLink.getType()==1){
            QwContactWay qwContactWay = qwContactWayMapper.selectQwContactWayById(qwDrainageLink.getWayId());
            if (qwContactWay!=null){
                qwDrainageLink.setQrCode(qwContactWay.getQrCode());
            }else {
                throw new CustomException("活码错误");
            }
        }

        qwDrainageLink.setCreateTime(DateUtils.getNowDate());
        return qwDrainageLinkMapper.insertQwDrainageLink(qwDrainageLink);
    }

    /**
     * 修改引流链接
     *
     * @param qwDrainageLink 引流链接
     * @return 结果
     */
    @Override
    public int updateQwDrainageLink(QwDrainageLink qwDrainageLink)
    {
        if(qwDrainageLink.getType()==1){
            QwContactWay qwContactWay = qwContactWayMapper.selectQwContactWayById(qwDrainageLink.getWayId());
            if (qwContactWay!=null){
                qwDrainageLink.setQrCode(qwContactWay.getQrCode());
            }else {
                throw new CustomException("活码错误");
            }
        }
        return qwDrainageLinkMapper.updateQwDrainageLink(qwDrainageLink);
    }

    /**
     * 批量删除引流链接
     *
     * @param ids 需要删除的引流链接主键
     * @return 结果
     */
    @Override
    public int deleteQwDrainageLinkByIds(Long[] ids)
    {
        return qwDrainageLinkMapper.deleteQwDrainageLinkByIds(ids);
    }

    /**
     * 删除引流链接信息
     *
     * @param id 引流链接主键
     * @return 结果
     */
    @Override
    public int deleteQwDrainageLinkById(Long id)
    {
        return qwDrainageLinkMapper.deleteQwDrainageLinkById(id);
    }

    @Override
    public List<JSONObject> selectQwDrainageLinkStatisticsCounts(Map<String, Object> map) {
        return qwDrainageLinkMapper.selectQwDrainageLinkStatisticsCounts(map);
    }
}
