package com.mvc.service.alarm;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.constant.GasAlarmType;
import com.mvc.dao.TAlarmRecordMapper;
import com.mvc.entity.TAlarmRecord;
import com.mvc.entity.TAlarmRecordExample;
import com.mvc.model.ResPageModel;
import com.mvc.model.StationAccModel;
import com.mvc.model.alarm.TAlarmRecordModel;
import com.mvc.service.taizhang.StationAccService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class TAlarmRecordService {
    @Resource
    private TAlarmRecordMapper tAlarmRecordMapper;

    @Resource
    private TAlarmNoticeService alarmNoticeService;

    @Resource
    private StationAccService stationAccService;

    public ResPageModel findAllTAlarmRecord(TAlarmRecordModel queryModel) {
        TAlarmRecordExample example = new TAlarmRecordExample();
        TAlarmRecordExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(queryModel.getStationCode())) {
            criteria.andStationCodeLike("%" + queryModel.getStationCode() + "%");
        }

        if (queryModel.getAlarmStatus() != null) {
            criteria.andAlarmStatusEqualTo(queryModel.getAlarmStatus());
        }

        if (queryModel.getAlarmType() != null) {
            criteria.andAlarmTypeEqualTo(queryModel.getAlarmType());
        }

        if(StringUtils.isNotEmpty(queryModel.getUnitCode())){
            criteria.andUnitCodeEqualTo(queryModel.getUnitCode());
        }

        example.setOrderByClause("ALARM_TIME desc");
        Page<Object> pageObject = PageHelper.startPage(queryModel.getPageNum(), queryModel.getPageSize());
        List<TAlarmRecord> TAlarmRecords = tAlarmRecordMapper.selectByExample(example);
        List<TAlarmRecordModel> resList = new ArrayList<>();
        for (TAlarmRecord TAlarmRecord : TAlarmRecords) {
            TAlarmRecordModel model = new TAlarmRecordModel();
            BeanUtils.copyProperties(TAlarmRecord, model);

            if (model.getAlarmNoticeId() != null) {
                model.setAlarmNoticeModel(alarmNoticeService.findTAlarmNoticeModelById(model.getAlarmNoticeId()));
            }

            setStationName(model);
            setAlarmInfo(model);
            resList.add(model);
        }
        ResPageModel pageModel = new ResPageModel(resList, pageObject);
        return pageModel;
    }

    /**
     * 设置站名
     *
     * @param recordModel
     */
    private void setStationName(TAlarmRecordModel recordModel) {
        StationAccModel accModel = stationAccService.findStationAccByStationCode(recordModel.getStationCode());
        if (accModel != null) {
            recordModel.setStationName(accModel.getStationName());
        }
    }

    /**
     * 赋值前台的提示信息
     * @param recordModel
     */
    private void setAlarmInfo(TAlarmRecordModel recordModel) {
        StringBuilder sb = new StringBuilder();
        if(recordModel.getAlarmType()==null){
            return;
        }
        GasAlarmType alarmType = GasAlarmType.findGasAlarmTypeByflag(recordModel.getAlarmType());
        switch (alarmType){
            case STANDARD_BELOW:
            case STANDARD_ABOVE:
                sb.append("流量：");
                sb.append(recordModel.getActualUsage().setScale(2, RoundingMode.HALF_UP));
                sb.append(" m³/h; ");
                sb.append(alarmType.getDesc());
                if (recordModel.getPlanUsage() == null) {
                    sb.append("-");
                } else {
                    sb.append(recordModel.getPlanUsage().setScale(2, RoundingMode.HALF_UP));
                }

                sb.append(" m³/h");
                break;
            case DATA_MISSING:
            case IMAGE_MISSING:
                sb.append(alarmType.getDesc());
                break;
            default:
                //单独处理用量
                sb.append("实际用量：");
                sb.append(recordModel.getActualUsage().setScale(2, RoundingMode.HALF_UP));
                sb.append(" m³; ");
                if(alarmType==GasAlarmType.SUBSCRIBE_BELOW||alarmType==GasAlarmType.SUBSCRIBE_BELOW){//订阅
                    sb.append("限定用量：");
                }else if(alarmType==GasAlarmType.DYNAMIC_BELOW||alarmType==GasAlarmType.DYNAMIC_ABOVE){//动态
                    sb.append("推算用量：");
                }

                if (recordModel.getPlanUsage() == null) {
                    sb.append("-");
                } else {
                    sb.append(recordModel.getPlanUsage().setScale(2, RoundingMode.HALF_UP));
                }
                sb.append(" m³; ");
                sb.append(alarmType.getDesc());
                break;
        }

        recordModel.setAlarmInfo(sb.toString());
    }

    public TAlarmRecordModel saveTAlarmRecord(TAlarmRecordModel model) {
        TAlarmRecord TAlarmRecord = new TAlarmRecord();
        BeanUtils.copyProperties(model, TAlarmRecord);
        int insert = tAlarmRecordMapper.insert(TAlarmRecord);
        model.setId(TAlarmRecord.getId());
        return model;
    }

    public TAlarmRecordModel updateTAlarmRecord(TAlarmRecordModel model) {
        TAlarmRecord TAlarmRecord = tAlarmRecordMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, TAlarmRecord);
        int i = tAlarmRecordMapper.updateByPrimaryKey(TAlarmRecord);
        return model;
    }

    public int deleteTAlarmRecord(int id) {
        int i = tAlarmRecordMapper.deleteByPrimaryKey(id);
        return i;
    }
}
