package com.zd.laboratory.service.impl;

import java.util.Date;
import java.util.List;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.security.utils.SaveUtil;

import com.zd.laboratory.mapper.LabMessageUserMapper;
import com.zd.system.api.laboratory.domain.LabMessageUser;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.laboratory.mapper.LabMessageDynamicMapper;
import com.zd.laboratory.domain.LabMessageDynamic;
import com.zd.laboratory.service.ILabMessageDynamicService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author zd
 * @date 2022-03-21
 */
@Service
public class LabMessageDynamicServiceImpl implements ILabMessageDynamicService
{
    @Autowired
    private LabMessageDynamicMapper labMessageDynamicMapper;
    @Autowired
    private LabMessageUserMapper labMessageUserMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public LabMessageDynamic selectLabMessageDynamicById(Long id)
    {
        return labMessageDynamicMapper.selectLabMessageDynamicById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param labMessageDynamic 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<LabMessageDynamic> selectLabMessageDynamicList(LabMessageDynamic labMessageDynamic)
    {
        return labMessageDynamicMapper.selectLabMessageDynamicList(labMessageDynamic);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param labMessageDynamic 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertLabMessageDynamic(LabMessageDynamic labMessageDynamic)
    {
        //设置其他公共字段
        SaveUtil.setCommonAttr(labMessageDynamic);
        return labMessageDynamicMapper.insertLabMessageDynamic(labMessageDynamic);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param labMessageDynamic 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertLabMessageDynamicByTask(LabMessageDynamic labMessageDynamic)
    {
        return labMessageDynamicMapper.insertLabMessageDynamic(labMessageDynamic);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param labMessageDynamic 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateLabMessageDynamic(LabMessageDynamic labMessageDynamic)
    {
        labMessageDynamic.setUpdateTime(DateUtils.getNowDate());
        labMessageDynamic.setUpdateBy(SecurityUtils.getUsername());
        return labMessageDynamicMapper.updateLabMessageDynamic(labMessageDynamic);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteLabMessageDynamicByIds(Long[] ids)
    {
        return labMessageDynamicMapper.deleteLabMessageDynamicByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteLabMessageDynamicById(Long id)
    {
        return labMessageDynamicMapper.deleteLabMessageDynamicById(id);
    }


    @Override
    public int addMessageAndUser(LabMessageDynamic labMessageDynamic) {
        List<LabMessageUser> messageUserList = labMessageDynamic.getMessageUserList();
        if(CollectionUtils.isNotEmpty(messageUserList)){
            SaveUtil.setCommonAttr(labMessageDynamic);
            labMessageDynamicMapper.insertLabMessageDynamic(labMessageDynamic);

            Date date = new Date();
            for (LabMessageUser labMessageUser : messageUserList) {
                labMessageUser.setMessageId(labMessageDynamic.getId());
                labMessageUser.setCreateTime(date);
            }
            labMessageUserMapper.insertLabMessageUserBatch(messageUserList);
        }

        return 1;
    }
}
