package com.fs.qw.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.qw.domain.QwCompany;
import com.fs.qw.mapper.QwCompanyMapper;
import com.fs.qw.service.IQwCompanyService;
import com.fs.qw.vo.QwOptionsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 企微主体Service业务层处理
 *
 * @author fs
 * @date 2024-10-09
 */
@Slf4j
@Service
public class QwCompanyServiceImpl implements IQwCompanyService
{
    @Autowired
    private QwCompanyMapper qwCompanyMapper;

    /**
     * 查询企微主体
     *
     * @param id 企微主体主键
     * @return 企微主体
     */
    @Override
    public QwCompany selectQwCompanyById(Long id)
    {
        return qwCompanyMapper.selectQwCompanyById(id);
    }

    @Override
    public QwCompany selectQwCompanyByCorpId(String corpId) {
        return qwCompanyMapper.selectQwCompanyByCorpId(corpId);
    }

    /**
     * 查询企微主体列表
     *
     * @param qwCompany 企微主体
     * @return 企微主体
     */
    @Override
    public List<QwCompany> selectQwCompanyList(QwCompany qwCompany)
    {
        return qwCompanyMapper.selectQwCompanyList(qwCompany);
    }

    /**
     * 新增企微主体
     *
     * @param qwCompany 企微主体
     * @return 结果
     */
    @Override
    public int insertQwCompany(QwCompany qwCompany)
    {
        qwCompany.setCreateTime(DateUtils.getNowDate());
        return qwCompanyMapper.insertQwCompany(qwCompany);
    }

    /**
     * 修改企微主体
     *
     * @param qwCompany 企微主体
     * @return 结果
     */
    @Override
    public int updateQwCompany(QwCompany qwCompany)
    {
        qwCompany.setUpdateTime(DateUtils.getNowDate());
        return qwCompanyMapper.updateQwCompany(qwCompany);
    }

    /**
     * 批量删除企微主体
     *
     * @param ids 需要删除的企微主体主键
     * @return 结果
     */
    @Override
    public int deleteQwCompanyByIds(Long[] ids)
    {
        return qwCompanyMapper.deleteQwCompanyByIds(ids);
    }

    /**
     * 删除企微主体信息
     *
     * @param id 企微主体主键
     * @return 结果
     */
    @Override
    public int deleteQwCompanyById(Long id)
    {
        return qwCompanyMapper.deleteQwCompanyById(id);
    }

    @Override
    public List<QwOptionsVO> selectQwCompanyListOptionsVO() {
        return qwCompanyMapper.selectQwCompanyListOptionsVO();
    }

    /**
     * 保存小程序消息发送参数
     * @param params    参数
     */
    @Override
    public void saveSendParams(JSONObject params) {
        if (Objects.isNull(params)) {
            log.warn("保存小程序消息发送参数 参数为空");
            return;
        }

        String appid = params.getString("appid");
        if (StringUtils.isBlank(appid)) {
            log.warn("保存小程序消息发送参数 appid为空");
            return;
        }

        int count = qwCompanyMapper.countNotParamByMiniAppId(appid);
        if (count > 0) {
            qwCompanyMapper.updateParamByMiniAppId(appid, params.toString());
        }
    }
}
