package com.yonge.cooleshow.auth.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.auth.api.entity.WxConfigInfo;
import com.yonge.cooleshow.auth.api.entity.WxTemplateConfig;
import com.yonge.cooleshow.auth.api.entity.WxTemplateMessage;
import com.yonge.cooleshow.auth.dal.dao.WxConfigInfoMapper;
import com.yonge.cooleshow.auth.dal.wrapper.WxConfigInfoWrapper;
import com.yonge.cooleshow.auth.service.WxConfigInfoService;
import com.yonge.cooleshow.auth.service.WxTemplateConfigService;
import com.yonge.cooleshow.auth.service.WxTemplateMessageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 微信配置信息
 * 2023-02-01 14:21:57
 */
@Slf4j
@Service
public class WxConfigInfoServiceImpl extends ServiceImpl<WxConfigInfoMapper, WxConfigInfo> implements WxConfigInfoService {

    @Autowired
    private WxTemplateConfigService wxTemplateConfigService;
    @Autowired
    private WxTemplateMessageService wxTemplateMessageService;

	/**
     * 查询详情
     * @param id 详情ID
     * @return WxConfigInfo
     */
	@Override
    public WxConfigInfo detail(Long id) {

        return baseMapper.selectById(id);
    }

    /**
     * 分页查询
     * @param page IPage<WxConfigInfo>
     * @param query WxConfigInfoWrapper.WxConfigInfoQuery
     * @return IPage<WxConfigInfo>
     */
    @Override
    public IPage<WxConfigInfo> selectPage(IPage<WxConfigInfo> page, WxConfigInfoWrapper.WxConfigInfoQuery query) {

        return page.setRecords(baseMapper.selectPage(page, query));
    }

    /**
     * 添加
     * @param wxConfigInfo WxConfigInfoWrapper.WxConfigInfo
     * @return Boolean
     */
    @Override
    public Boolean add(WxConfigInfoWrapper.WxConfigInfo wxConfigInfo) {

        return this.save(JSON.parseObject(wxConfigInfo.jsonString(), WxConfigInfo.class));
    }

    /**
     * 更新
     * @param wxConfigInfo WxConfigInfoWrapper.WxConfigInfo
     * @return Boolean
     */
    @Override
    public Boolean update(WxConfigInfoWrapper.WxConfigInfo wxConfigInfo){

        return this.updateById(JSON.parseObject(wxConfigInfo.jsonString(), WxConfigInfo.class));
    }

    /**
     * 公众号模板信息内容
     *
     * @param appId 公众号Id
     * @param cmd   消息指令
     * @return WxTemplateMessage
     */
    @Override
    public WxTemplateMessage findWxTemplateMessageByCommand(String appId, String cmd) {

        List<WxTemplateMessage> records = wxTemplateMessageService.lambdaQuery()
            .eq(WxTemplateMessage::getAppid, appId)
            .eq(WxTemplateMessage::getCommand, cmd)
            .eq(WxTemplateMessage::getStatus, true)
            .list();

        if (CollectionUtils.isEmpty(records)) {
            return null;
        }

        return records.get(0);
    }

    /**
     * 公众号模板消息配置
     *
     * @param appId 公众号Id
     * @param cmd   指令
     * @return WxTemplateConfig
     */
    @Override
    public WxTemplateConfig findWxTemplateConfigWhen(String appId, String cmd) {

        List<WxTemplateConfig> records = wxTemplateConfigService.lambdaQuery()
            .eq(WxTemplateConfig::getAppid, appId)
            .eq(WxTemplateConfig::getCommand, cmd)
            .eq(WxTemplateConfig::getStatus, true)
            .list();

        if (CollectionUtils.isEmpty(records)) {
            return null;
        }

        return records.get(0);
    }

    /**
     * 公众号配置
     *
     * @param appId 公众号Id
     * @return WxConfigInfo
     */
    @Override
    public WxConfigInfo findWxConfigInfoByAppId(String appId) {

        List<WxConfigInfo> records = lambdaQuery()
            .eq(WxConfigInfo::getAppid, appId)
            .eq(WxConfigInfo::getStatus, true)
            .list();

        if (CollectionUtils.isEmpty(records)) {
            return null;
        }

        return records.get(0);
    }
}
