package com.lightinit.hsdataplatform.impl.admin;

import com.lightinit.hsdataplatform.dictionary.DicNotificationState;
import com.lightinit.hsdataplatform.entity.Notification;
import com.lightinit.hsdataplatform.entity.NotificationExample;
import com.lightinit.hsdataplatform.mapper.NotificationMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.ResultStateCode;
import com.lightinit.hsdataplatform.model.admin.NotificationListModel;
import com.lightinit.hsdataplatform.model.admin.NotificationSearchModel;
import com.lightinit.hsdataplatform.service.admin.INotificationService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: sunxiang
 * @Date: 2018/7/8 16:48
 * @Description:
 **/
@Service
public class NotificationServiceImpl implements INotificationService {
    @Autowired
    private NotificationMapper notificationMapper;
    @Override
    public List<NotificationListModel> getList(NotificationSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        List<NotificationListModel> list = notificationMapper.getNotiList(inputModel);
        return list;
    }

    @Override
    public long queryCount(NotificationSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return notificationMapper.getNotiCount(inputModel);
    }
    @Transactional
    @Override
    public ResultState add(NotificationSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState resultState = new ResultState();
        try {
            Notification notification = new Notification();
            BeanUtils.copyProperties(inputModel,notification);
            notification.setCreatedBy(inputModel.getUserId());
            if(DicNotificationState.sended.equals(inputModel.getNotiState())){
                notification.setPublishAt(new Date());
                notification.setPublishBy(inputModel.getUserId());
            }
            notification.setCreatedAt(new Date());
            notificationMapper.insert(notification);
            resultState.setMsg("添加成功");
            return resultState;
        } catch (BeansException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        resultState.setStateCode(ResultStateCode.INVALID_DATA);
        resultState.setMsg("添加失败");
        return resultState;
    }

    @Transactional
    @Override
    public ResultState update(NotificationSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState resultState = new ResultState();
        try {
            Notification notification = new Notification();
            BeanUtils.copyProperties(inputModel,notification);
            if(DicNotificationState.sended.equals(inputModel.getNotiState())){
                notification.setPublishAt(new Date());
                notification.setPublishBy(inputModel.getUserId());
            }
            notificationMapper.updateByPrimaryKeySelective(notification);
            resultState.setMsg("修改成功");
            return resultState;
        } catch (BeansException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        resultState.setStateCode(ResultStateCode.INVALID_DATA);
        resultState.setMsg("修改失败");
        return resultState;
    }

    @Override
    public ResultState delete(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState resultState = new ResultState();
        try {
            notificationMapper.deleteByPrimaryKey(id);
            resultState.setMsg("删除成功");
            return resultState;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        resultState.setStateCode(ResultStateCode.INVALID_DATA);
        resultState.setMsg("删除失败");
        return resultState;
    }
}
