package com.mvc.service.alarm;

import com.mvc.dao.TAlarmNoticeDetailMapper;
import com.mvc.entity.TAlarmNoticeDetail;
import com.mvc.entity.TAlarmNoticeDetailExample;
import com.mvc.model.alarm.TAlarmNoticeDetailModel;
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.List;

@Service
@Transactional
public class TAlarmNoticeDetailService {
    @Resource
    private TAlarmNoticeDetailMapper tAlarmNoticeDetailMapper;

    public TAlarmNoticeDetailModel saveTAlarmNoticeDetail(TAlarmNoticeDetailModel model) {
        TAlarmNoticeDetail TAlarmNoticeDetail = new TAlarmNoticeDetail();
        BeanUtils.copyProperties(model, TAlarmNoticeDetail);
        int insert = tAlarmNoticeDetailMapper.insert(TAlarmNoticeDetail);
        model.setId(TAlarmNoticeDetail.getId());
        return model;
    }

    public TAlarmNoticeDetailModel updateTAlarmNoticeDetail(TAlarmNoticeDetailModel model) {
        TAlarmNoticeDetail TAlarmNoticeDetail = tAlarmNoticeDetailMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, TAlarmNoticeDetail);
        int i = tAlarmNoticeDetailMapper.updateByPrimaryKey(TAlarmNoticeDetail);
        return model;
    }

    public int deleteTAlarmNoticeDetail(int id) {
        int i = tAlarmNoticeDetailMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * 根据parentId获取明细列表
     *
     * @param parentId
     * @return
     */
    public List<TAlarmNoticeDetailModel> findTAlarmNoticeDetailsById(int parentId) {
        TAlarmNoticeDetailExample example = new TAlarmNoticeDetailExample();
        TAlarmNoticeDetailExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);

        List<TAlarmNoticeDetailModel> resList = new ArrayList<>();
        List<TAlarmNoticeDetail> alarmNoticeDetails = tAlarmNoticeDetailMapper.selectByExample(example);
        for (TAlarmNoticeDetail detail : alarmNoticeDetails) {
            TAlarmNoticeDetailModel model = new TAlarmNoticeDetailModel();
            BeanUtils.copyProperties(detail, model);
            resList.add(model);
        }
        return resList;
    }

    /**
     * 根据parentId删除记录
     * @param parentId
     * @return
     */
    public int deleteDetailByParentId(int parentId){
        TAlarmNoticeDetailExample example=new TAlarmNoticeDetailExample();
        TAlarmNoticeDetailExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        return tAlarmNoticeDetailMapper.deleteByExample(example);
    }
}
