package com.zd.laboratory.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.zd.common.core.constant.SecurityConstants;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.utils.StringUtils;
import com.zd.common.datascope.annotation.DataScope;
import com.zd.laboratory.domain.LabMessageDynamic;
import com.zd.laboratory.domain.LabSubject;
import com.zd.laboratory.domain.dto.LabPhotoNoteADD;
import com.zd.laboratory.domain.vo.LabPhotoNoteListVO;
import com.zd.laboratory.domain.vo.LabPhotoNoteVO;
import com.zd.laboratory.domain.vo.LabSecurityApplyVO;
import com.zd.laboratory.mapper.LabSubjectMapper;
import com.zd.laboratory.service.ILabMessageDynamicService;
import com.zd.laboratory.service.ILabMessageUserService;
import com.zd.laboratory.service.ILabSecurityApplyService;
import com.zd.system.api.RemoteDeptService;
import com.zd.system.api.RemoteUserService;
import com.zd.system.api.domain.SysDept;
import com.zd.system.api.domain.SysUser;
import com.zd.common.core.utils.SpringUtils;
import com.zd.common.security.service.TokenService;

import com.zd.system.api.laboratory.domain.LabMessageUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.laboratory.mapper.LabPhotoNoteMapper;
import com.zd.laboratory.domain.LabPhotoNote;
import com.zd.laboratory.service.ILabPhotoNoteService;

/**
 * 随手拍记录Service业务层处理
 *
 * @author 周攀
 * @date 2021-09-10
 */
@Service
public class LabPhotoNoteServiceImpl implements ILabPhotoNoteService {
    @Autowired
    private LabPhotoNoteMapper labPhotoNoteMapper;
    @Autowired
    private LabSubjectMapper labSubjectMapper;
    @Autowired
    private RemoteDeptService remoteDeptService;
    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private ILabMessageDynamicService labMessageDynamicService;

    @Autowired
    private ILabMessageUserService labMessageUserService;

    @Autowired
    private ILabSecurityApplyService labSecurityApplyService;

    /**
     * 查询随手拍记录
     *
     * @param id 随手拍记录主键
     * @return 随手拍记录
     */
    @Override
    public LabPhotoNote selectLabPhotoNoteById(Long id) {
        return labPhotoNoteMapper.selectLabPhotoNoteById(id);
    }

    /**
     * 查询随手拍记录列表
     *
     * @param labPhotoNote 随手拍记录
     * @return 随手拍记录
     */
    @Override
    public List<LabPhotoNote> selectLabPhotoNoteList(LabPhotoNote labPhotoNote) {
        return labPhotoNoteMapper.selectLabPhotoNoteList(labPhotoNote);
    }

    /**
     * 查询随手拍记录列表-VO
     *
     * @param labPhotoNote 随手拍记录
     * @return 随手拍记录
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u",permi = PerPrefix.LABORATORY_PHOTONOTE)
    public List<LabPhotoNoteListVO> selectLabPhotoNoteVOList(LabPhotoNote labPhotoNote) {
        return labPhotoNoteMapper.selectLabPhotoNoteVOList(labPhotoNote);
    }


    /**
     * 新增随手拍记录
     *
     * @param labPhotoNote 随手拍记录
     * @return 结果
     */
    @Override
    public int insertLabPhotoNote(LabPhotoNote labPhotoNote) {
        labPhotoNote.setHandleStatus(0);
        labPhotoNote.setCreateTime(DateUtils.getNowDate());
        labPhotoNote.setCreateBy(SecurityUtils.getUsername());
        labPhotoNote.setUserId(SecurityUtils.getUserId());
        SysUser sysUser = SpringUtils.getBean(TokenService.class).getLoginUser().getSysUser();
        labPhotoNote.setDeptId(sysUser.getDeptId());
        labPhotoNote.setDeptName(sysUser.getDeptName());
        return labPhotoNoteMapper.insertLabPhotoNote(labPhotoNote);
    }

    /**
     * 保存随手拍记录 并返回ID
     *
     * @param labPhotoNote 随手拍记录
     * @return 结果
     */
    @Override
    public Long saveLabPhotoNote(LabPhotoNoteADD labPhotoNote) {
        LabPhotoNote add = new LabPhotoNote();
        BeanUtils.copyProperties(labPhotoNote, add);
        List<String> imgs = labPhotoNote.getImgUrls();
        String imgStr = "";
        for(String img:imgs){
            imgStr += ","+img;
        }
        if(!"".equals(imgStr)){
            imgStr = imgStr.substring(1,imgStr.length());
        }
        add.setImgUrl(imgStr);
        //这里需要查询实验室的管理员
        LabSubject subjectPo = labSubjectMapper.selectLabSubjectById(labPhotoNote.getSubjectId());
        List<LabSecurityApplyVO> applist = new ArrayList<>();
        if(subjectPo!=null){
            StringBuffer usersStr = new StringBuffer();
            if(subjectPo.getAdminId()!=null){
                usersStr.append(subjectPo.getAdminId()+",");
            }
            if(StringUtils.isNotEmpty(subjectPo.getSafeUserId())){
                usersStr.append(subjectPo.getSafeUserId()+",");
            }
            LabSecurityApplyVO applyvo = new LabSecurityApplyVO();
            applyvo.setSubjectId(subjectPo.getId());
            applist = labSecurityApplyService.selectApplyListEffective(applyvo);
            for(LabSecurityApplyVO applyVO : applist){
                usersStr.append(applyVO.getUserId()+",");
            }
            add.setHandleUsers(usersStr.toString());
        }
        add.setHandleStatus(0);
        add.setCreateTime(DateUtils.getNowDate());
        add.setCreateBy(SecurityUtils.getUsername());
        add.setUserId(SecurityUtils.getUserId());
        SysUser sysUser = SpringUtils.getBean(TokenService.class).getLoginUser().getSysUser();
        add.setDeptId(sysUser.getDeptId());
        add.setDeptName(sysUser.getDeptName());
        labPhotoNoteMapper.insertLabPhotoNote(add);

        //驳回消息他通知
        LabMessageDynamic labMessageDynamic = new LabMessageDynamic();
        labMessageDynamic.setSendRange(3);
        labMessageDynamic.setMessClass(2);
        labMessageDynamic.setContent(add.getDescribe());
        labMessageDynamic.setDynamicId(add.getId());
        labMessageDynamic.setRemark("随手拍待处理");
        int dynamicId = labMessageDynamicService.insertLabMessageDynamic(labMessageDynamic);
        if(dynamicId>0){
            //管理员发送
            if(StringUtils.isNotNull(subjectPo) && StringUtils.isNotNull(subjectPo.getAdminId())) {
                LabMessageUser labMessageUser = new LabMessageUser();
                labMessageUser.setJoinUserId(subjectPo.getAdminId());
                labMessageUser.setIsRead(false);
                labMessageUser.setMessageId(labMessageDynamic.getId());
                labMessageUserService.insertLabMessageUser(labMessageUser);
            }
            //安全员发送
            if(StringUtils.isNotNull(subjectPo) && StringUtils.isNotEmpty(subjectPo.getSafeUserId())){
                String[] ids = subjectPo.getSafeUserId().split(",");
                for (String safeid:ids ) {
                    LabMessageUser labMessageUser = new LabMessageUser();
                    labMessageUser.setJoinUserId(Long.parseLong(safeid));
                    labMessageUser.setIsRead(false);
                    labMessageUser.setMessageId(labMessageDynamic.getId());
                    labMessageUserService.insertLabMessageUser(labMessageUser);
                }

            }
            //实验室准入资格人员发送
            for(LabSecurityApplyVO applyVO : applist){
                LabMessageUser labMessageUser = new LabMessageUser();
                labMessageUser.setJoinUserId(applyVO.getUserId());
                labMessageUser.setIsRead(false);
                labMessageUser.setMessageId(labMessageDynamic.getId());
                labMessageUserService.insertLabMessageUser(labMessageUser);
            }

        }
        return add.getId();
    }


    /**
     * 修改随手拍记录
     *
     * @param labPhotoNote 随手拍记录
     * @return 结果
     */
    @Override
    public int updateLabPhotoNote(LabPhotoNote labPhotoNote) {
        labPhotoNote.setUpdateTime(DateUtils.getNowDate());
        labPhotoNote.setUpdateBy(SecurityUtils.getUsername());
        int flag = labPhotoNoteMapper.updateLabPhotoNote(labPhotoNote);
        if(flag>0){
            LabPhotoNote labPhotoNote1 = labPhotoNoteMapper.selectLabPhotoNoteById(labPhotoNote.getId());
            //驳回消息他通知
            LabMessageDynamic labMessageDynamic = new LabMessageDynamic();
            labMessageDynamic.setSendRange(3);
            labMessageDynamic.setMessClass(2);
            labMessageDynamic.setContent(labPhotoNote.getHandleDescribe());
            labMessageDynamic.setDynamicId(labPhotoNote.getId());
            labMessageDynamic.setRemark("感谢您的反馈");
            int dynamicId = labMessageDynamicService.insertLabMessageDynamic(labMessageDynamic);
            if(dynamicId>0){
                LabMessageUser labMessageUser = new LabMessageUser();
                labMessageUser.setJoinUserId(labPhotoNote1.getUserId());
                labMessageUser.setIsRead(false);
                labMessageUser.setMessageId(labMessageDynamic.getId());
                labMessageUserService.insertLabMessageUser(labMessageUser);
            }
        }
        return flag;
    }

    /**
     * 批量删除随手拍记录
     *
     * @param ids 需要删除的随手拍记录主键
     * @return 结果
     */
    @Override
    public int deleteLabPhotoNoteByIds(Long[] ids) {
        return labPhotoNoteMapper.deleteLabPhotoNoteByIds(ids);
    }

    /**
     * 删除随手拍记录信息
     *
     * @param id 随手拍记录主键
     * @return 结果
     */
    @Override
    public int deleteLabPhotoNoteById(Long id) {
        return labPhotoNoteMapper.deleteLabPhotoNoteById(id);
    }

    /**
     * 查询登录人接收随手拍统计
     *
     * @param labPhotoNote 随手拍记录
     * @return 查询登录人接收随手拍统计
     */
    @Override
    public LabPhotoNoteVO receivePhotoNote(LabPhotoNote labPhotoNote) {
        return labPhotoNoteMapper.receivePhotoNote(labPhotoNote);
    }
}
