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

import cn.com.ctop.alarm.modules.entity.RuleAccountThreshold;
import cn.com.ctop.alarm.modules.mapper.RuleAccountThresholdMapper;
import cn.com.ctop.alarm.modules.service.IRuleAccountThresholdService;
import cn.com.ctop.common.module.utils.Check;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
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
 */
@Slf4j
@Service
public class RuleAccountThresholdServiceImpl extends ServiceImpl<RuleAccountThresholdMapper, RuleAccountThreshold> implements IRuleAccountThresholdService {
    @Autowired
    private RuleAccountThresholdMapper ruleAccountThresholdMapper;

    /**
     * 填充阈值
     *
     * @param requestJson
     * @return
     */
    @Override
    public JSONObject fillThreshold(JSONObject requestJson) throws Exception {
        Long accountId = requestJson.getLong("accountId");
        if (Check.isNull(accountId)) {
            throw new Exception("请传入账户id");
        }
        JSONArray ruleList = requestJson.getJSONArray("ruleList");
        if (Check.isNull(ruleList)) {
            throw new Exception("请输入具体的规则字段");
        }
        Integer updateCount = 0;
        for (int i = 0; i < ruleList.size(); i++) {
            JSONObject groupJson = ruleList.getJSONObject(i);
            if (Check.isNull(groupJson)) {
                continue;
            }
            JSONArray ruleDetail = groupJson.getJSONArray("ruleDetail");
            if (Check.isNull(ruleDetail)) {
                continue;
            }
            Long groupId = groupJson.getLong("groupId");
            Integer isCopy = groupJson.getInteger("isCopy");
            if (isCopy == 1) {
                Map<String, Object> deleteMap = new HashMap<>();
                deleteMap.put("account_id", accountId);
                deleteMap.put("group_id", groupId);
                ruleAccountThresholdMapper.deleteByMap(deleteMap); // 删除此账户下 此组绑定的失效阈值
            }

            for (int j = 0; j < ruleDetail.size(); j++) {
                if (isCopy == 1) { // 规则组可复制
                    JSONArray ruleDetailArr = ruleDetail.getJSONArray(j);
                    if (Check.isNull(ruleDetailArr)) {
                        continue;
                    }
                    String batchNo = UUID.randomUUID().toString().replace("-", "");
                    for (int k = 0; k < ruleDetailArr.size(); k++) {
                        JSONObject ruleJson = ruleDetailArr.getJSONObject(k);
                        if (Check.isNull(ruleJson)) {
                            continue;
                        }
                        Long ruleId = ruleJson.getLong("ruleId");
                        if (Check.isNull(ruleId)) {
                            continue;
                        }
                        String threshold = ruleJson.getString("threshold");
                        RuleAccountThreshold addRuleAccountThreshold = new RuleAccountThreshold();
                        addRuleAccountThreshold.setAccountId(accountId);
                        addRuleAccountThreshold.setGroupId(groupId);
                        addRuleAccountThreshold.setThreshold(threshold);
                        addRuleAccountThreshold.setRuleId(ruleId);
                        if (isCopy == 1) {
                            addRuleAccountThreshold.setBatchNo(batchNo);
                        }
                        int insert = ruleAccountThresholdMapper.insert(addRuleAccountThreshold);
                        if (insert > 0) {
                            updateCount += 1;
                        }
                    }
                } else if (isCopy == 0) {
                    JSONObject ruleJson = ruleDetail.getJSONObject(j);
                    Long ruleId = ruleJson.getLong("ruleId");
                    if (Check.isNull(ruleId)) {
                        continue;
                    }
                    String threshold = ruleJson.getString("threshold");
                    RuleAccountThreshold updateThreshold = new RuleAccountThreshold();
                    if (Check.isNull(threshold)) {
                        updateThreshold.setThreshold("");
                    } else {
                        updateThreshold.setThreshold(threshold);
                    }
                    QueryWrapper<RuleAccountThreshold> updateQueryWrapper = new QueryWrapper();
                    updateQueryWrapper.eq("account_id", accountId);
                    updateQueryWrapper.eq("rule_id", ruleId);
                    updateQueryWrapper.eq("group_id", groupId);
                    int update = ruleAccountThresholdMapper.update(updateThreshold, updateQueryWrapper);
                    if (update > 0) {
                        updateCount += 1;
                    }
                }
            }
        }

        log.info("修改账户模板阈值完成，账户id：{}，修改规则条数：{}", accountId, updateCount);
        JSONObject returnJson = new JSONObject();
        returnJson.put("code", 0);
        returnJson.put("updateCount", updateCount);
        return returnJson;
    }

    /**
     * 获取批次号列表
     *
     * @param accountId
     * @param groupId
     * @return
     */
    @Override
    public List<String> getBatchNo(Long accountId, Long groupId) {
        return ruleAccountThresholdMapper.getBatchNo(accountId, groupId);
    }
}
