package com.zcxk.rmcp.web.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.page.PageMethod;
import com.zcxk.core.common.exception.BusinessException;
import com.zcxk.core.common.util.BeanCopyUtils;
import com.zcxk.core.mysql.pageing.Pagination;
import com.zcxk.core.oauth2.pojo.LoginUser;
import com.zcxk.core.oauth2.util.UserUtil;
import com.zcxk.rmcp.api.dto.alarm.AlarmRuleDto;
import com.zcxk.rmcp.api.dto.alarm.AlarmRulePageDto;
import com.zcxk.rmcp.api.dto.alarm.ConfigDataDto;
import com.zcxk.rmcp.api.dto.alarm.RuleMeasuringDto;
import com.zcxk.rmcp.core.dao.DeviceAlarmRuleMapper;
import com.zcxk.rmcp.core.entity.DeviceAlarmRule;
import com.zcxk.rmcp.core.entity.TplMeasuringDataDef;
import com.zcxk.rmcp.web.service.AlarmRuleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
/**
 *
 * @author heyumin
 * @date 2021-07-30 9:44
 */
@Service
@Slf4j
public class AlarmRuleServiceImpl implements AlarmRuleService {
    @Resource
    private DeviceAlarmRuleMapper deviceAlarmRuleMapper;
    @Override
    public Pagination<AlarmRuleDto> getList(AlarmRulePageDto alarmRulePageDto) {
        PageMethod.startPage(alarmRulePageDto.getPageNum(),alarmRulePageDto.getPageSize());
        List<AlarmRuleDto>alarmRuleDtos=deviceAlarmRuleMapper.selectList(alarmRulePageDto);
        if (CollectionUtils.isEmpty(alarmRuleDtos)){
            return new Pagination<>();
        }
        return new Pagination<>(alarmRuleDtos);
    }

    @Override
    public void add(AlarmRuleDto alarmRuleDto) {
        DeviceAlarmRule deviceAlarmRule=new DeviceAlarmRule();
        LoginUser currentUser = UserUtil.getCurrentUser();
        BeanCopyUtils.copyProperties(alarmRuleDto, deviceAlarmRule, DeviceAlarmRule.class);
        deviceAlarmRule.setStatus(1);
        deviceAlarmRule.setCreateDate(new Date());
        deviceAlarmRule.setCreateBy(currentUser.getName());
        deviceAlarmRule.setTenantId(currentUser.getTenantId());
        log.info("begin addAlarmType alarmType:" + JSON.toJSONString(deviceAlarmRule));
        generationRules(deviceAlarmRule);
        deviceAlarmRuleMapper.insert(deviceAlarmRule);
    }
    private void generationRules( DeviceAlarmRule deviceAlarmRule){
        int resultName = deviceAlarmRuleMapper.findByNameUnique(deviceAlarmRule.getId(), deviceAlarmRule.getRuleName(), deviceAlarmRule.getAlarmTypeId());
        if (resultName > 0) {
            throw BusinessException.builder(-900, "规则名称已存在");
        }
        //查询测点的规则是否添加过
        int ruleCount = deviceAlarmRuleMapper.countMeasuringCode(deviceAlarmRule.getId(), deviceAlarmRule.getAlarmTypeId(), deviceAlarmRule.getMeasuringCode());
        if (ruleCount > 0) {
            throw BusinessException.builder(-900, "该测点的规则已添加过");
        }
        //生成表达式和规格
        String expression = "";
        //规格
        String specification = "";
        if (deviceAlarmRule.getMeasuringDataType() == 1) {
            expression = "#value == \"" + deviceAlarmRule.getValue1() + "\"";
            TplMeasuringDataDef tplMeasuringDataDef = deviceAlarmRuleMapper.findTplMeasuringDataDef(deviceAlarmRule.getDeviceType(),deviceAlarmRule.getMeasuringCode(),deviceAlarmRule.getValue1());
            if (tplMeasuringDataDef != null) {
                specification = tplMeasuringDataDef.getDataName();
            }
        } else {
            String value1 = "#value >= " + deviceAlarmRule.getValue1();
            String value2 = "#value <= " + deviceAlarmRule.getValue2();
            if (!StringUtils.equals("", deviceAlarmRule.getValue1()) && !StringUtils.equals("", deviceAlarmRule.getValue2())) {
                expression = value1 + " and " + value2;
                specification = deviceAlarmRule.getValue1() + "<=" + deviceAlarmRule.getMeasuringCode() + "<=" + deviceAlarmRule.getValue2();
            } else if (!StringUtils.equals("", deviceAlarmRule.getValue1()) && StringUtils.equals("", deviceAlarmRule.getValue2())) {
                expression = value1;
                specification = deviceAlarmRule.getMeasuringCode() + ">=" + deviceAlarmRule.getValue1();
            } else if (StringUtils.equals("", deviceAlarmRule.getValue1()) && !StringUtils.equals("", deviceAlarmRule.getValue2())) {
                expression = value2;
                specification = deviceAlarmRule.getMeasuringCode() + "<=" + deviceAlarmRule.getValue2();
            }
        }
        deviceAlarmRule.setExpression(expression);
        deviceAlarmRule.setSpecification(specification);
    }
    @Override
    public void edit(AlarmRuleDto alarmRuleDto) {
        DeviceAlarmRule deviceAlarmRule=new DeviceAlarmRule();
        BeanCopyUtils.copyProperties(alarmRuleDto, deviceAlarmRule, DeviceAlarmRule.class);
        deviceAlarmRule.setUpdateBy(UserUtil.getCurrentUser().getName());
        deviceAlarmRule.setUpdateDate(new Date());
        generationRules(deviceAlarmRule);
        deviceAlarmRuleMapper.update(deviceAlarmRule);
    }

    @Override
    public void delete(List<Integer> ids) {
        deviceAlarmRuleMapper.delete(ids);
    }

    @Override
    public void deleteAll(Integer alarmTypeId) {

        deviceAlarmRuleMapper.deleteAll(alarmTypeId );
    }

    @Override
    public List<RuleMeasuringDto> getRuleMeasuringList(Integer deviceTypeId) {
        return deviceAlarmRuleMapper.getRuleMeasuringList( deviceTypeId);
    }

    @Override
    public List<ConfigDataDto> getMeasuringOptionList(Integer deviceTypeId, Integer measuringId) {

        return deviceAlarmRuleMapper.getMeasuringOptionList( deviceTypeId,measuringId);
    }
}
