package cn.com.ctop.kuaishou.modules.batch.service.impl;

import cn.com.ctop.common.module.entity.UserAllocation;
import cn.com.ctop.common.module.service.IMessageTemplate;
import cn.com.ctop.common.module.service.ISendMessageService;
import cn.com.ctop.common.module.service.IUserAllocationService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaishouOverRunLog;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaishouOverRunLogMapper;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouCreativeService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouOverRunSendMessageService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouOverRunLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class KuaiShouOverRunSendMessageServiceImpl extends ServiceImpl<KuaishouOverRunLogMapper, KuaishouOverRunLog> implements IKuaiShouOverRunSendMessageService {

    @Autowired
    private IKuaiShouCreativeService creativeService;
    @Autowired
    private IMessageTemplate messageTemplate;
    @Autowired
    private IUserAllocationService userAllocationService;
    @Autowired
    private ISendMessageService sendMessageService;
    @Autowired
    private IKuaishouOverRunLogService overRunLogService;
    @Autowired
    private KuaishouOverRunLogMapper runLogMapper;


    @Override
    public void creativeOverRunSendMessage(Long accountId, String userId) {

        try {
            String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
            Integer hour = DateUtils.getHour(DateUtils.getNowDate("yyyy-MM-dd HH:mm:ss"));
            QueryWrapper<KuaishouOverRunLog> overRunLogQueryWrapper = new QueryWrapper<>();
            overRunLogQueryWrapper.eq("account_id", accountId);
            overRunLogQueryWrapper.eq("stat_date", nowDate);
            overRunLogQueryWrapper.eq("stat_hour", hour);
            overRunLogQueryWrapper.last("limit 1");
            KuaishouOverRunLog overRunLog = overRunLogService.getOne(overRunLogQueryWrapper);
            if (!Check.isNull(overRunLog)) {
                return;
            }


            String startTime = DateUtils.getStartTime(new Date());
            Integer creativeCount = creativeService.getCreateCount(accountId, startTime);

            UserAllocation userAllocation = userAllocationService.getByAccountId(accountId);
            if (!Check.isNull(userAllocation)) {
                String sendUserId;
                if (!Check.isNull(userId)) {
                    sendUserId = userId;
                } else {
                    LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
                    if (Check.isNull(sysUser)) {
                        return;
                    }
                    sendUserId = sysUser.getId();
                }
                String message = messageTemplate.getCreativeOverRunMessage(accountId, creativeCount, userAllocation.getAuthName());
                sendMessageService.sendMessage(sendUserId, message);
                KuaishouOverRunLog addOverRunLog = new KuaishouOverRunLog();
                addOverRunLog.setAccountId(accountId);
                addOverRunLog.setMailType(1);
                addOverRunLog.setStatDate(nowDate);
                addOverRunLog.setStatHour(hour);
                runLogMapper.insert(addOverRunLog);
            }
        } catch (Exception e) {
            e.printStackTrace();

        }

    }
}
