package org.jeecg.modules.bytedance.advertise.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.bytedance.advertise.mapper.MonitorBytedanceActionTrackUrlMapper;
import org.jeecg.modules.bytedance.advertise.service.IMonitorBytedanceService;
import org.jeecg.modules.bytedance.advertise.vo.MonitorBytedanceAdVo;
import org.jeecg.modules.bytedance.common.utils.Check;
import org.jeecg.modules.bytedance.common.utils.CorpWexinUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.List;

/**
 * 规则账户清洗数据
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2020-11-16
 */
@Service
public class MonitorBytedanceServiceImpl implements IMonitorBytedanceService {
    @Resource
    private MonitorBytedanceActionTrackUrlMapper monitorBytedanceActionTrackUrlMapper;

    @Resource
    private CorpWexinUtils corpWexinUtils;

    /**
     * @description: 监测预警-点击监测链接
     * @param
     * @return: org.jeecg.common.api.vo.Result
     * @author: zianY
     * @time: 2021/12/28
     */
    public Result monitorBytedanceActionTrackUrl(){
        //查询 头条自动投放配置 中的账户 和 点击监测链接
        List<MonitorBytedanceAdVo> accountList = monitorBytedanceActionTrackUrlMapper.getBytedancStrategyAcctionId();
        accountList.forEach(accountInfo ->{
            //根据账户 查询头条计划中的 点击监测链接
            List<MonitorBytedanceAdVo> actionTrackUrlList = monitorBytedanceActionTrackUrlMapper.getBytedancPlanActionTrackUrl(accountInfo.getAccountId());
            if (!Check.isNull(actionTrackUrlList)){
                for (MonitorBytedanceAdVo actiontrackUrl : actionTrackUrlList) {
                    //账户配置中的 点击监测链接 ！= 创建计划中的 点击监测链接
                    if (!StringUtils.equals(accountInfo.getActionTrackUrl(),actiontrackUrl.getActionTrackUrl())){
                        //发送预警消息
                        sendEnterpriseWeiXinMessage(accountInfo);
                        break;
                    }
                }
            }
        });
        return Result.successMsg("成功。",null);
    }
    /**
     * 发送 点击监测链接 预警消息
     */
    public void sendEnterpriseWeiXinMessage(MonitorBytedanceAdVo accountInfo){
        String message =  "{0}，您好！您的账户：{1}，账户ID：{2}，点击监测链接配置有差异，请及时检查账户配置！";
        //发送微信通知
        JSONObject jsonObject =  monitorBytedanceActionTrackUrlMapper.getWChatIdByUserId("80e747cdea6f41dbbc8cde23046fd4e0");
        String msg = MessageFormat.format(message,accountInfo.getOperateUserName(),accountInfo.getAccountName(),String.valueOf(accountInfo.getAccountId()));
        corpWexinUtils.sendMessageByWeChatId(jsonObject,msg);
    }



}
