package com.bonus.system.basic.service.impl;

import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.system.att.entity.AttGroupBean;
import com.bonus.system.basic.dao.SysNoticeMapper;
import com.bonus.system.basic.domain.SysNotice;
import com.bonus.system.basic.domain.SysUserRole;
import com.bonus.system.basic.service.SysNoticeService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 我的消息
 * 
 * @author hay
 */
@Service
@Log4j2
public class SysNoticeServiceImpl implements SysNoticeService
{

    @Resource
    private SysNoticeMapper noticeMapper;


    @Override
    public List<SysNotice> selectNoticeList(SysNotice bean) {
        Long userId = SecurityUtils.getUserId();
        bean.setUserId(userId);
        return noticeMapper.selectNoticeList(bean);
    }

    @Override
    public AjaxResult insertNotice(SysNotice bean) {
        try {
            //先增加消息
            Long userId = SecurityUtils.getUserId();
            bean.setUserId(userId);
            //根据类型名称，查询类型id
//            Long noticeId = noticeMapper.getDictData(bean.getType());
//            bean.setType(String.valueOf(noticeId));
            int result = noticeMapper.insertNotice(bean);
            if (result > 0){
                //再添加消息对应的接收人员
                Long[] userIds = bean.getUserIds();
                List<SysNotice> list = new ArrayList<SysNotice>();
                for (Long userId1 : userIds){
                    SysNotice notice = new SysNotice();
                    notice.setUserId(userId1);
                    notice.setNoticeId(bean.getNoticeId());
                    notice.setLeaveApplyId(bean.getLeaveApplyId());
                    list.add(notice);
                }
                result = noticeMapper.insertUserNotice(list);
            }
            return result > 0 ? AjaxResult.success() : AjaxResult.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.toString(),e);
            return AjaxResult.error();
        }
    }

    @Override
    public AjaxResult updateStatus(SysNotice bean) {
        try {
            int re = noticeMapper.updateStatus(bean);
            return re > 0 ? AjaxResult.success() : AjaxResult.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.toString(),e);
            return AjaxResult.error();
        }
    }

    @Override
    public List<Long> getUserIdByModelName(String modelName) {
        return noticeMapper.getUserIdByModelName(modelName);
    }

    @Override
    public List<Long> getUserIdByOrgId(Long orgId) {
        return noticeMapper.getUserIdByOrgId(orgId);
    }

    @Override
    public List<Long> getAttUserIdByOrgId(List<Long> orgList) {
        return noticeMapper.getAttUserIdByOrgId(orgList);
    }

    @Override
    public AjaxResult delNotice(SysNotice bean) {
        try {
            int re = noticeMapper.delNotice(bean);
            return re > 0 ? AjaxResult.success() : AjaxResult.error();
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.toString(),e);
            return AjaxResult.error();
        }
    }

    @Override
    public List<AttGroupBean> getAttSettingHistoryDate(String currentDay) {
        return noticeMapper.getAttSettingHistoryDate(currentDay);
    }

    @Override
    public void insertAttSettingHistoryData(List<AttGroupBean> attList) {
        noticeMapper.insertAttSettingHistoryData(attList);
    }

    @Override
    public List<String> getAttSettingDate(String startDate) {
        return noticeMapper.getAttSettingDate(startDate);
    }
}
