package com.zd.laboratory.service.impl;


import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zd.common.core.constant.SecurityConstants;
import com.zd.common.core.constant.UserConstants;
import com.zd.common.core.domain.R;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.security.service.TokenService;
import com.zd.laboratory.domain.LabMessageDynamic;
import com.zd.laboratory.domain.LabSecurityMaterialData;
import com.zd.laboratory.domain.LabSecurityMaterialrelation;
import com.zd.laboratory.domain.vo.LabSecurityApplyVO;
import com.zd.laboratory.domain.vo.LabSecurityMaterialDataVO;
import com.zd.laboratory.domain.vo.LiteMessageNoticeVO;
import com.zd.laboratory.mapper.*;
import com.zd.laboratory.service.ILabSecurityMaterialrelationService;
import com.zd.system.api.RemoteUserService;
import com.zd.system.api.domain.SysUser;
import com.zd.system.api.laboratory.domain.vo.LabMessageUserListVO;
import com.zd.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.system.api.laboratory.domain.LabMessageUser;
import com.zd.laboratory.service.ILabMessageUserService;

/**
 * 用户消息Service业务层处理
 *
 * @Author: zhoupan
 * @Date: 2021/09/02/17:55
 * @Description:
 */
@Service
public class LabMessageUserServiceImpl implements ILabMessageUserService {
    @Autowired
    private LabMessageUserMapper labMessageUserMapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private LabMessageDynamicMapper labMessageDynamicMapper;
    @Autowired
    private LabSecurityApplyMapper labSecurityApplyMapper;
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private LabSecurityMaterialDataMapper labSecurityMaterialDataMapper;
    @Autowired
    private LabSecurityMaterialrelationMapper labSecurityMaterialrelationMapper;

    /**
     * 查询用户消息
     *
     * @param id 用户消息主键
     * @return 用户消息
     */
    @Override
    public LabMessageUser selectLabMessageUserById(Long id) {
        return labMessageUserMapper.selectLabMessageUserById(id);
    }

    /**
     * 查询用户消息列表
     *
     * @param labMessageUser 用户消息
     * @return 用户消息
     */
    @Override
    public List<LabMessageUser> selectLabMessageUserList(LabMessageUser labMessageUser) {
        return labMessageUserMapper.selectLabMessageUserList(labMessageUser);
    }

    /**
     * 查询用户消息列表VO
     *
     * @param labMessageUser 用户消息
     * @return 用户消息集合
     */
    public List<LabMessageUserListVO> selectListVO(LabMessageUser labMessageUser) {

        return labMessageUserMapper.selectListVO(labMessageUser);
    }

    /**
     * 新增用户消息
     *
     * @param labMessageUser 用户消息
     * @return 结果
     */
    @Override
    public int insertLabMessageUser(LabMessageUser labMessageUser) {
        labMessageUser.setCreateTime(DateUtils.getNowDate());
        return labMessageUserMapper.insertLabMessageUser(labMessageUser);
    }

    /**
     * 修改用户消息
     *
     * @param labMessageUser 用户消息
     * @return 结果
     */
    @Override
    public int updateLabMessageUser(LabMessageUser labMessageUser) {
        labMessageUser.setUpdateTime(DateUtils.getNowDate());
        return labMessageUserMapper.updateLabMessageUser(labMessageUser);
    }

    /**
     * 批量删除用户消息
     *
     * @param ids 需要删除的用户消息主键
     * @return 结果
     */
    @Override
    public int deleteLabMessageUserByIds(Long[] ids) {
        return labMessageUserMapper.deleteLabMessageUserByIds(ids);
    }

    /**
     * 删除用户消息信息
     *
     * @param id 用户消息主键
     * @return 结果
     */
    @Override
    public int deleteLabMessageUserById(Long id) {
        return labMessageUserMapper.deleteLabMessageUserById(id);
    }

    /**
     * 批量新增用户消息
     *
     * @param labMessageUser 用户消息
     * @return
     */
    @Override
    public int insertLabMessageUserBatch(List<LabMessageUser> labMessageUser) {

        return labMessageUserMapper.insertLabMessageUserBatch(labMessageUser);
    }

    /**
     * 小程序获取 外层消息列表 （通讯录短信类似， 合并发送人）
     * @return
     */
    @Override
    public List<LiteMessageNoticeVO>  queryLiteMessageNoticeVO()
    {
        LoginUser loginUser = tokenService.getLoginUser();
        if(loginUser ==null ) return null;
       return labMessageUserMapper.queryLiteMessageNoticeVO(loginUser.getUserid(),loginUser.getSysUser().getDeptId());
    }


    /**
     * 查询安全准入消息信息
     *
     * @param messageId 消息id
     * @return
     */
    @Override
    public Map<String, Object> myRead(Long messageId) {
        Map<String, Object> map =new HashMap <String, Object>();
        LabMessageDynamic labMessageDynamic = labMessageDynamicMapper.selectLabMessageDynamicById(messageId);

        //处理已读信息
        LabMessageUser labMessageUser = new LabMessageUser();
        labMessageUser.setMessageId(messageId);
        labMessageUser.setJoinUserId(tokenService.getLoginUser().getUserid());
        List<LabMessageUser> labMessageUserList = labMessageUserMapper.selectLabMessageListByUser(labMessageUser);
        for(LabMessageUser labMessageUser1:labMessageUserList){
            LabMessageUser labMessageUserNew = new LabMessageUser();
            labMessageUserNew.setId(labMessageUser1.getId());
            labMessageUserNew.setIsRead(true);
            if(!labMessageUser1.getIsRead()){
                labMessageUserMapper.updateLabMessageUser(labMessageUserNew);
            }
        }
//        //查询安全准入相关信息
//        LabSecurityApplyVO labSecurityApplyVO = labSecurityApplyMapper.selectLabSecurityApplyById(labMessageDynamic.getDynamicId());
//        R <SysUser> rDate =remoteUserService.getUserInfoByUserId(labSecurityApplyVO.getUserId(), SecurityConstants.INNER);
//        SysUser sysUser = rDate.getData();
//        labSecurityApplyVO.setSysUser(sysUser);
//
//        List<LabSecurityMaterialDataVO> listTemp=Collections.emptyList();//模板
//        //学生登录
//        if("22".equals(sysUser.getUserType())){
//            //模板查询
////            listModel =labSecurityMaterialDataMapper.selectLabSecurityMaterialDataStudent(labSecurityApplyVO.getSubjectId());
//            //模板查询
//            listTemp =labSecurityMaterialDataMapper.selectLabSecurityMaterialDataStudent(labSecurityApplyVO.getSubjectId());
//            for (int i=0;i<listTemp.size();i++){
//                //如果是关联模板 查询出具体关联的内容
//                if(1==listTemp.get(i).getMaterialType()){
//                    //上传材料查询
//                    LabSecurityMaterialrelation lmr = new LabSecurityMaterialrelation();
//                    lmr.setApplyId(labSecurityApplyVO.getId());
//                    lmr.setMaterialId(listTemp.get(i).getId());
//                    List<LabSecurityMaterialrelation>  list =labSecurityMaterialrelationMapper.selectLabSecurityMaterialrelationList(lmr);
//                    listTemp.get(i).setListMr(list);
//                }
//            }
//            map.put("listModel",listTemp);//模板
//        }else if("11".equals(sysUser.getUserType())){
//            //教职工 模板查询
////            listModel= labSecurityMaterialDataMapper.selectLabSecurityMaterialDataTeacher(labSecurityApplyVO.getSubjectId());
//            //教职工 模板查询
//            listTemp= labSecurityMaterialDataMapper.selectLabSecurityMaterialDataTeacher(labSecurityApplyVO.getSubjectId());
//            for (int i=0;i<listTemp.size();i++){
//                //如果是关联模板 查询出具体关联的内容
//                if(1==listTemp.get(i).getMaterialType()){
//                    //上传材料查询
//                    LabSecurityMaterialrelation lmr = new LabSecurityMaterialrelation();
//                    lmr.setApplyId(labSecurityApplyVO.getId());
//                    lmr.setMaterialId(listTemp.get(i).getId());
//                    List<LabSecurityMaterialrelation>  list =labSecurityMaterialrelationMapper.selectLabSecurityMaterialrelationList(lmr);
//                    listTemp.get(i).setListMr(list);
//                }
//            }
//            map.put("listModel",listTemp);//模板
//        }
//        map.put("labSecurityApply",labSecurityApplyVO);//安全准入申请信息
        map.put("applyId",labMessageDynamic.getDynamicId());//安全准入申请id
        if(labMessageDynamic.getMessClass()==1){
            //查询安全准入相关信息
            LabSecurityApplyVO labSecurityApplyVO = labSecurityApplyMapper.selectLabSecurityApplyById(labMessageDynamic.getDynamicId());
            R <SysUser> rDate =remoteUserService.getUserInfoByUserId(labSecurityApplyVO.getUserId(), SecurityConstants.INNER);
            SysUser sysUser = rDate.getData();
            labSecurityApplyVO.setSysUser(sysUser);

            List<LabSecurityMaterialDataVO> listTemp=Collections.emptyList();//模板
            //学生登录
            if(UserConstants.USER_TYPE_STUDENT.equals(sysUser.getUserType())){
                //模板查询
//            listModel =labSecurityMaterialDataMapper.selectLabSecurityMaterialDataStudent(labSecurityApplyVO.getSubjectId());
                //模板查询
                listTemp =labSecurityMaterialDataMapper.selectLabSecurityMaterialDataStudent(labSecurityApplyVO.getSubjectId());
                for (int i=0;i<listTemp.size();i++){
                    //如果是关联模板 查询出具体关联的内容
                    if(1==listTemp.get(i).getMaterialType()){
                        //上传材料查询
                        LabSecurityMaterialrelation lmr = new LabSecurityMaterialrelation();
                        lmr.setApplyId(labSecurityApplyVO.getId());
                        lmr.setMaterialId(listTemp.get(i).getId());
                        List<LabSecurityMaterialrelation>  list =labSecurityMaterialrelationMapper.selectLabSecurityMaterialrelationList(lmr);
                        listTemp.get(i).setListMr(list);
                    }
                }
                map.put("listModel",listTemp);//模板
            }else if("11".equals(sysUser.getUserType())){
                //教职工 模板查询
//            listModel= labSecurityMaterialDataMapper.selectLabSecurityMaterialDataTeacher(labSecurityApplyVO.getSubjectId());
                //教职工 模板查询
                listTemp= labSecurityMaterialDataMapper.selectLabSecurityMaterialDataTeacher(labSecurityApplyVO.getSubjectId());
                for (int i=0;i<listTemp.size();i++){
                    //如果是关联模板 查询出具体关联的内容
                    if(1==listTemp.get(i).getMaterialType()){
                        //上传材料查询
                        LabSecurityMaterialrelation lmr = new LabSecurityMaterialrelation();
                        lmr.setApplyId(labSecurityApplyVO.getId());
                        lmr.setMaterialId(listTemp.get(i).getId());
                        List<LabSecurityMaterialrelation>  list =labSecurityMaterialrelationMapper.selectLabSecurityMaterialrelationList(lmr);
                        listTemp.get(i).setListMr(list);
                    }
                }
                map.put("listModel",listTemp);//模板
            }
            map.put("labSecurityApply",labSecurityApplyVO);//安全准入申请信息
        }else if(labMessageDynamic.getMessClass()==2){
            map.put("dynamicId",labMessageDynamic.getDynamicId());//随手拍主键id
        }
        return map;
    }


    /**
     * 查询我的消息统计数量
     *
     * @return
     */
    @Override
    public Map<String, Object> myGroupTotalCount() {
        Map<String, Object> map =new HashMap <String, Object>();
        LabMessageDynamic labMessageDynamic = new LabMessageDynamic();
        labMessageDynamic.setUserId(tokenService.getLoginUser().getUserid());
        List<LabMessageDynamic> labMessageDynamicList = labMessageDynamicMapper.selectLabMessageDynamicListByNotRead(labMessageDynamic);
        map.put("totalCount",labMessageDynamicList.size());
        return map;
    }

    /**
     * 登录用户消息读取
     * @param labMessageUser
     */
    @Override
    public void readByLoginUser(LabMessageUser labMessageUser) {
        if(labMessageUser.getId() != null){
            labMessageUser = labMessageUserMapper.selectLabMessageUserById(labMessageUser.getId());
            if(labMessageUser != null && !labMessageUser.getIsRead()){
                LabMessageDynamic labMessageDynamic = labMessageDynamicMapper.selectLabMessageDynamicById(labMessageUser.getMessageId());
                if(labMessageDynamic != null){
                    LabMessageUser labMessageUserNew = new LabMessageUser();
                    labMessageUserNew.setId(labMessageUser.getId());
                    labMessageUserNew.setIsRead(true);
                    labMessageUserMapper.updateLabMessageUser(labMessageUserNew);
                }
            }
        }
    }

}
