package com.zd.laboratory.service.impl;

import java.util.Date;
import java.util.List;

import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.datascope.annotation.DataScope;
import com.zd.common.security.service.TokenService;
import com.zd.common.security.utils.SaveUtil;

import com.zd.laboratory.domain.LabCheckRecord;
import com.zd.laboratory.domain.vo.LabCheckRecordReleaseVO;
import com.zd.laboratory.mqtt.constants.MqttConstants;
import com.zd.laboratory.mqtt.entiy.MessageBody;
import com.zd.laboratory.mqtt.service.impl.CommonSend;
import com.zd.laboratory.service.ILabCheckMachineMsgService;
import com.zd.laboratory.service.ILabCheckRecordService;
import com.zd.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.laboratory.mapper.LabCheckRecordReleaseMapper;
import com.zd.laboratory.domain.LabCheckRecordRelease;
import com.zd.laboratory.service.ILabCheckRecordReleaseService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 检查发布关系Service业务层处理
 *
 * @author zd
 * @date 2022-02-17
 */
@Service
public class LabCheckRecordReleaseServiceImpl implements ILabCheckRecordReleaseService
{
    @Autowired
    private LabCheckRecordReleaseMapper labCheckRecordReleaseMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ILabCheckMachineMsgService checkMachineMsgService;

    @Autowired
    private ILabCheckRecordService labCheckRecordService;

    @Autowired
    private CommonSend commonSend;
    /**
     * 查询检查发布关系
     *
     * @param id 检查发布关系主键
     * @return 检查发布关系
     */
    @Override
    public LabCheckRecordRelease selectLabCheckRecordReleaseById(Long id)
    {
        return labCheckRecordReleaseMapper.selectLabCheckRecordReleaseById(id);
    }

    /**
     * 查询检查发布关系列表
     *
     * @param labCheckRecordRelease 检查发布关系
     * @return 检查发布关系
     */
    @Override
    public List<LabCheckRecordRelease> selectLabCheckRecordReleaseList(LabCheckRecordRelease labCheckRecordRelease)
    {
        return labCheckRecordReleaseMapper.selectLabCheckRecordReleaseList(labCheckRecordRelease);
    }

    /**
     * 新增检查发布关系
     *
     * @param labCheckRecordRelease 检查发布关系
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertLabCheckRecordRelease(LabCheckRecordRelease labCheckRecordRelease)
    {
        //设置其他公共字段
        //SaveUtil.setCommonAttr(labCheckRecordRelease);

        //提交的检查项id集合
        String[] ids =labCheckRecordRelease.getCheckId().split(",");
        Long[] idscheck = new Long[ids.length];

        //循环发送通知
        for (int i=0;i<ids.length;i++){
            idscheck[i]= Long.valueOf(ids[i]);
            //获取检查记录
            LabCheckRecord labCheckRecord= labCheckRecordService.selectLabCheckRecordById(Long.valueOf(ids[i]) );
            //查看是什么类型整改 发送对应通知 0一般整改改为1，否则为2 重大整改 发送给实验室负责人
            checkMachineMsgService.insertLabCheckMachineTjz(labCheckRecord.getZgType()==0?1:2, labCheckRecord.getId(),1);
        }

        //获取当前登录用户信息
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        labCheckRecordRelease.setUserId(sysUser.getUserId());
        labCheckRecordRelease.setCreateBy(sysUser.getNickName());
        labCheckRecordRelease.setDeptId(sysUser.getDept().getDeptId());
        labCheckRecordRelease.setDeptName(sysUser.getDept().getDeptName());
        labCheckRecordRelease.setCreateTime(new Date());

        //数据入库操作 刷新安全检查表 发布状态
        labCheckRecordService.updateLabCheckRecordByIds(idscheck);
        //数据入库操作 发布数据入库
        int id= labCheckRecordReleaseMapper.insertLabCheckRecordRelease(labCheckRecordRelease);

        if(id>0){
            //3 有新的发布信息
            commonSend.send(MqttConstants.LAB_BIG_VIEW_CHECKAPPLY, new MessageBody("有新的检查发布。", 3));
        }
        return id;
    }

    /**
     * 修改检查发布关系
     *
     * @param labCheckRecordRelease 检查发布关系
     * @return 结果
     */
    @Override
    public int updateLabCheckRecordRelease(LabCheckRecordRelease labCheckRecordRelease)
    {
        return labCheckRecordReleaseMapper.updateLabCheckRecordRelease(labCheckRecordRelease);
    }

    /**
     * 批量删除检查发布关系
     *
     * @param ids 需要删除的检查发布关系主键
     * @return 结果
     */
    @Override
    public int deleteLabCheckRecordReleaseByIds(Long[] ids)
    {
        return labCheckRecordReleaseMapper.deleteLabCheckRecordReleaseByIds(ids);
    }

    /**
     * 删除检查发布关系信息
     *
     * @param id 检查发布关系主键
     * @return 结果
     */
    @Override
    public int deleteLabCheckRecordReleaseById(Long id)
    {
        return labCheckRecordReleaseMapper.deleteLabCheckRecordReleaseById(id);
    }

    /**
     * 检查项id是否已发布
     *
     * @param labCheckRecordRelease
     * @return
     */
    @Override
    public List<LabCheckRecordRelease> selectLabCheckRecordReleaseListExist(LabCheckRecordRelease labCheckRecordRelease) {
        return labCheckRecordReleaseMapper.selectLabCheckRecordReleaseListExist(labCheckRecordRelease);
    }

    /***
     * 发布集合查询
     * @param labCheckRecordReleaseVO
     * @return
     */
    @Override
    @DataScope(deptAlias = "d" , userAlias = "u" , permi = PerPrefix.LABORATORY_RELEASE)
    public List<LabCheckRecordReleaseVO> selectListByParameter(LabCheckRecordReleaseVO labCheckRecordReleaseVO) {
        return labCheckRecordReleaseMapper.selectListByParameter(labCheckRecordReleaseVO);
    }

    /****
     * 获取最新的一条发布数据
     * @return
     */
    @Override
    public LabCheckRecordRelease selectLabCheckRecordReleaseFirst() {
        return labCheckRecordReleaseMapper.selectLabCheckRecordReleaseFirst();
    }
}
