package cn.com.ctop.alarm.modules.service.impl;

import cn.com.ctop.alarm.modules.entity.*;
import cn.com.ctop.alarm.modules.entity.vo.TemplateAppliedVo;
import cn.com.ctop.alarm.modules.mapper.RuleAccountTemplateMapper;
import cn.com.ctop.alarm.modules.service.*;
import cn.com.ctop.common.module.utils.Check;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 规则模板
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2020-11-15
 */
@Service
public class RuleAccountTemplateServiceImpl extends ServiceImpl<RuleAccountTemplateMapper, RuleAccountTemplate> implements IRuleAccountTemplateService {
    @Autowired
    private RuleAccountTemplateMapper ruleAccountTemplateMapper;
    @Autowired
    private IRuleTemplateService ruleTemplateService;
    @Autowired
    private IRuleGroupService ruleGroupService;
    @Autowired
    private IRuleBaseService ruleBaseService;
    @Autowired
    private IRuleAccountThresholdService ruleAccountThresholdService;

    /**
     * 推送模板到账户下
     *
     * @param accountId
     * @param templateId
     * @return
     */
    @Override
    public JSONObject pushTemplateToAccount(Long accountId, Long templateId) throws Exception {

        RuleTemplate ruleTemplate = ruleTemplateService.getById(templateId);
        if (Check.isNull(ruleTemplate)) {
            throw new Exception("推送失败，未获取到模板信息");

        }
        JSONArray groupIds = JSONArray.parseArray(ruleTemplate.getGroupIds());
        if (Check.isNull(groupIds)) {
            throw new Exception("推送失败，模板下战暂无可用规则");
        }
        Map<String, Object> deleteMap = new HashMap<>();
        deleteMap.put("account_id", accountId);
        ruleAccountTemplateMapper.deleteByMap(deleteMap);
        ruleAccountThresholdService.removeByMap(deleteMap);
        RuleAccountTemplate template = new RuleAccountTemplate();
        template.setAccountId(accountId);
        template.setTemplateId(templateId);
        int insert = ruleAccountTemplateMapper.insert(template);
        if (insert == 0) {
            throw new Exception("推送失败，请联系相关技术人员");
        }
        JSONObject returnJson = new JSONObject();
        Integer groupCount = 0;
        for (int i = 0; i < groupIds.size(); i++) {
            Long groupId = groupIds.getLong(i);
            RuleGroup ruleGroup = ruleGroupService.getById(groupId);
            if (Check.isNull(ruleGroup)) {
                continue;
            }
            groupCount += 1;
            Integer isCopy = ruleGroup.getIsCopy();
            String batchId = null;
            if (isCopy == 1) {
                batchId = UUID.randomUUID().toString().replace("-", "");

            }
            JSONArray ruleIds = JSONArray.parseArray(ruleGroup.getRuleIds());
            if (Check.isNull(ruleIds)) {
                continue;
            }
            for (int j = 0; j < ruleIds.size(); j++) {
                Long ruleId = ruleIds.getLong(j);
                RuleBase ruleBase = ruleBaseService.getById(ruleId);
                if (Check.isNull(ruleBase)) {
                    continue;
                }
                RuleAccountThreshold threshold = new RuleAccountThreshold();
                threshold.setAccountId(accountId);
                threshold.setGroupId(groupId);
                threshold.setRuleId(ruleBase.getId());
                if (ruleBase.getVariableType() == 0) {
                    threshold.setThreshold(ruleBase.getThreshold());
                }
                if (isCopy == 1) {
                    threshold.setBatchNo(batchId);
                }
                if (ruleBase.getVariableType() == 0) {
                    threshold.setThreshold(ruleBase.getThreshold());
                }
                ruleAccountThresholdService.save(threshold);
            }

        }
        returnJson.put("groupCount", groupCount);
        return returnJson;
    }


    /**
     * 查询 账户下 应用的模板
     *
     * @param requestMap
     * @return
     */
    @Override
    public List<TemplateAppliedVo> getTemplateApplied(Map<String, Object> requestMap) {
        return ruleAccountTemplateMapper.getTemplateApplied(requestMap);
    }
}
