package com.mvc.service.alarm;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.dao.TAlarmNoticeMapper;
import com.mvc.entity.TAlarmNotice;
import com.mvc.entity.TAlarmNoticeExample;
import com.mvc.model.ResPageModel;
import com.mvc.model.alarm.TAlarmNoticeDetailModel;
import com.mvc.model.alarm.TAlarmNoticeModel;
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.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class TAlarmNoticeService {
    @Resource
    private TAlarmNoticeMapper tAlarmNoticeMapper;

    @Resource
    private TAlarmNoticeDetailService alarmNoticeDetailService;

    @Resource
    private AlarmNoticeMana alarmNoticeMana;

    public ResPageModel findAllTAlarmNotice(TAlarmNoticeModel queryModel) {
        TAlarmNoticeExample example = new TAlarmNoticeExample();
        TAlarmNoticeExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(queryModel.getRulesName())) {
            criteria.andRulesNameLike("%" + queryModel.getRulesName() + "%");
        }
        if (StringUtils.isNotEmpty(queryModel.getEmailAddress())) {
            criteria.andEmailAddressLike("%" + queryModel.getEmailAddress() + "%");
        }
        if (StringUtils.isNotEmpty(queryModel.getCreateMan())) {
            criteria.andCreateManLike("%" + queryModel.getCreateMan() + "%");
        }
        if (queryModel.getUseFlag() != null) {
            criteria.andUseFlagEqualTo(queryModel.getUseFlag());
        }
        if (StringUtils.isNotEmpty(queryModel.getUnitCode())) {
            criteria.andUnitCodeEqualTo(queryModel.getUnitCode());
        }

        example.setOrderByClause(" CREATE_TIME desc");
        Page<Object> pageObject = PageHelper.startPage(queryModel.getPageNum(), queryModel.getPageSize());
        List<TAlarmNotice> TAlarmNotices = tAlarmNoticeMapper.selectByExample(example);
        List<TAlarmNoticeModel> resList = new ArrayList<>();
        for (TAlarmNotice TAlarmNotice : TAlarmNotices) {
            TAlarmNoticeModel model = new TAlarmNoticeModel();
            BeanUtils.copyProperties(TAlarmNotice, model);
            model.setDetails(alarmNoticeDetailService.findTAlarmNoticeDetailsById(model.getId()));
            resList.add(model);
        }

        ResPageModel pageModel = new ResPageModel(resList, pageObject);
        return pageModel;
    }

    public TAlarmNoticeModel saveTAlarmNotice(TAlarmNoticeModel model) {
        model.setCreateTime(new Date());
        TAlarmNotice alarmNotice = new TAlarmNotice();
        BeanUtils.copyProperties(model, alarmNotice);
        int insert = tAlarmNoticeMapper.insert(alarmNotice);
        model.setId(alarmNotice.getId());
        if (insert > 0) {
            saveDetails(alarmNotice.getId(), model.getDetails());
        }
        //刷新报警列表
        alarmNoticeMana.getAlarmNoticeModels();
        return model;
    }

    /**
     * 保存明细列表
     *
     * @param details
     */
    private void saveDetails(int parentId, List<TAlarmNoticeDetailModel> details) {
        if (details == null || details.size() == 0) return;
        for (TAlarmNoticeDetailModel detail : details) {
            detail.setParentId(parentId);
            alarmNoticeDetailService.saveTAlarmNoticeDetail(detail);
        }
    }

    public TAlarmNoticeModel updateTAlarmNotice(TAlarmNoticeModel model) {
        TAlarmNotice alarmNotice = tAlarmNoticeMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, alarmNotice);
        int i = tAlarmNoticeMapper.updateByPrimaryKey(alarmNotice);
        if (i > 0) {
            //更新时,统一删除掉明细,再重新添加
            alarmNoticeDetailService.deleteDetailByParentId(model.getId());
            saveDetails(alarmNotice.getId(), model.getDetails());
        }
        //刷新报警列表
        alarmNoticeMana.getAlarmNoticeModels();
        return model;
    }

    public int deleteTAlarmNotice(int id) {
        int i = tAlarmNoticeMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * 根据ID获取对象
     *
     * @param id
     * @return
     */
    public TAlarmNoticeModel findTAlarmNoticeModelById(int id) {
        TAlarmNotice alarmNotice = tAlarmNoticeMapper.selectByPrimaryKey(id);
        if (alarmNotice != null) {
            TAlarmNoticeModel model = new TAlarmNoticeModel();
            BeanUtils.copyProperties(alarmNotice, model);
        }
        return null;
    }
}
