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

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.entity.WarningOperationLog;
import cn.com.ctop.common.module.mapper.ProjectMapper;
import cn.com.ctop.common.module.mapper.UserAllocationMapper;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.service.IMessageTemplate;
import cn.com.ctop.common.module.service.ISendMessageService;
import cn.com.ctop.common.module.service.IWarningOperationLogService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouUpdateService;
import cn.com.ctop.kuaishou.modules.batch.service.IWarningOperationService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class WarningOperationServiceImpl implements IWarningOperationService {
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private UserAllocationMapper userAllocationMapper;
    @Autowired
    private IMessageTemplate messageTemplate;
    @Autowired
    private IKuaiShouUpdateService updateService;
    @Autowired
    private ISendMessageService sendMessageService;
    @Autowired
    private ICtopOauthTokenService oauthTokenService;
    @Autowired
    private IWarningOperationLogService warningOperationLogService;

    @Override
    public JSONObject getBidTypeAndMaxBid(Long accountId) {
        Long projectId = userAllocationMapper.getProjectIdByAccountId(accountId);
        if (Check.isNull(projectId)) {
            log.error("此账户未绑定项目");
            return null;
        }
        JSONObject warningJson = projectMapper.getWarningInfoByProjectId(projectId);
        return warningJson;
    }


    /**
     * 快手出价方式预警 && 操作
     *
     * @param projectId
     * @param projectName
     * @param advertiserId
     * @param unitId
     * @param unit_name
     */

    @Override
    public void kuaiShouWarningOperation(Long projectId, String projectName, Long advertiserId, Long unitId, String unit_name) {

        String userId = userAllocationMapper.queryUserIdByAccountId(advertiserId);
        try {
            String text = messageTemplate.getKuaiShouBidTypeMessage(projectName, advertiserId, unitId, unit_name);

            sendMessageService.sendMessage(userId, text);
        } catch (Exception e) {
            e.printStackTrace();
        }
        CtopOauthToken token = oauthTokenService.getTokenByAccountId(advertiserId);
        updateService.updateUnitStatus(token.getAccessToken(), advertiserId, unitId, 2, "admin");
        WarningOperationLog warningOperationLog = new WarningOperationLog();
        warningOperationLog.setAccountId(advertiserId);
        warningOperationLog.setSubject("出价方式预警");
        warningOperationLog.setUnitId(unitId);
        warningOperationLog.setUserId(userId);
        warningOperationLog.setWarningType(1);
        warningOperationLogService.save(warningOperationLog);


    }


    /**
     * 出价 过高预警 && 操作
     *
     * @param projectId
     * @param projectName
     * @param advertiserId
     * @param unitId
     * @param unit_name
     */

    @Override
    public void kuaiShouBidWarningOperation(Long projectId, String projectName, Long advertiserId, Long unitId, String unit_name) {
        String userId = userAllocationMapper.queryUserIdByAccountId(advertiserId);
        try {
            String text = messageTemplate.getKuaiShouBidMessage(projectName, advertiserId, unitId, unit_name);
            sendMessageService.sendMessage(userId, text);
        } catch (Exception e) {
            e.printStackTrace();
        }

        CtopOauthToken token = oauthTokenService.getTokenByAccountId(advertiserId);
        updateService.updateUnitStatus(token.getAccessToken(), advertiserId, unitId, 2, "admin");

        WarningOperationLog warningOperationLog = new WarningOperationLog();
        warningOperationLog.setAccountId(advertiserId);
        warningOperationLog.setSubject("出价过高预警");
        warningOperationLog.setUnitId(unitId);
        warningOperationLog.setUserId(userId);
        warningOperationLog.setWarningType(2);
        warningOperationLogService.save(warningOperationLog);

    }
}
