package com.fs.qw.service.impl;

import com.alibaba.fastjson.JSON;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.utils.DateUtils;
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 com.fs.voice.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

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

    @Autowired
    private RedisCache redisCache;

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

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

    @Override
    public QwCompany getQwCompanyByRedis(String corpId) {
        String key =(String) redisCache.getCacheObject("qwCompany:"+corpId);
        if (!StringUtil.strIsNullOrEmpty(key)){
            return JSON.parseObject(key, QwCompany.class);
        }

        QwCompany qwCompany = qwCompanyMapper.selectQwCompanyByCorpId(corpId);
        redisCache.setCacheObject("qwCompany:"+corpId ,JSON.toJSONString(qwCompany),1, TimeUnit.HOURS);

        return qwCompany;
    }

    /**
     * 查询企微主体列表
     *
     * @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());
        int i = qwCompanyMapper.insertQwCompany(qwCompany);
        return i;
    }

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

        if (i>0){
            redisCache.setCacheObject("qwCompany:"+qwCompany.getCorpId() ,JSON.toJSONString(qwCompany),1, TimeUnit.HOURS);
        }

        return i;
    }

    /**
     * 批量删除企微主体
     *
     * @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(Long userId, Long deptId) {
        return qwCompanyMapper.selectQwCompanyListOptionsVO(userId, deptId);
    }
}
