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.security.service.TokenService;
import com.zd.common.security.utils.SaveUtil;

import com.zd.laboratory.domain.LabSubject;
import com.zd.laboratory.domain.vo.LabSecuritySubjectmaterialVO;
import com.zd.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.laboratory.mapper.LabSecuritySubjectmaterialMapper;
import com.zd.laboratory.domain.LabSecuritySubjectmaterial;
import com.zd.laboratory.service.ILabSecuritySubjectmaterialService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 实验室和准入模板关系Service业务层处理
 *
 * @author zd
 * @date 2022-02-08
 */
@Service
public class LabSecuritySubjectmaterialServiceImpl implements ILabSecuritySubjectmaterialService
{
    @Autowired
    private LabSecuritySubjectmaterialMapper labSecuritySubjectmaterialMapper;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询实验室和准入模板关系
     *
     * @param id 实验室和准入模板关系主键
     * @return 实验室和准入模板关系
     */
    @Override
    public LabSecuritySubjectmaterial selectLabSecuritySubjectmaterialById(Long id)
    {
        return labSecuritySubjectmaterialMapper.selectLabSecuritySubjectmaterialById(id);
    }

    /**
     * 查询实验室和准入模板关系列表
     *
     * @param labSecuritySubjectmaterial 实验室和准入模板关系
     * @return 实验室和准入模板关系
     */
    @Override
    public List<LabSecuritySubjectmaterial> selectLabSecuritySubjectmaterialList(LabSecuritySubjectmaterial labSecuritySubjectmaterial)
    {
        return labSecuritySubjectmaterialMapper.selectLabSecuritySubjectmaterialList(labSecuritySubjectmaterial);
    }

    /**
     * 新增实验室和准入模板关系
     *
     * @param labSecuritySubjectmaterial 实验室和准入模板关系
     * @return 结果
     */
    @Override
    public int insertLabSecuritySubjectmaterial(LabSecuritySubjectmaterial labSecuritySubjectmaterial)
    {
        //设置其他公共字段
        SaveUtil.setCommonAttr(labSecuritySubjectmaterial);
        return labSecuritySubjectmaterialMapper.insertLabSecuritySubjectmaterial(labSecuritySubjectmaterial);
    }

    /**
     * 修改实验室和准入模板关系
     *
     * @param labSecuritySubjectmaterial 实验室和准入模板关系
     * @return 结果
     */
    @Override
    public int updateLabSecuritySubjectmaterial(LabSecuritySubjectmaterial labSecuritySubjectmaterial)
    {
        return labSecuritySubjectmaterialMapper.updateLabSecuritySubjectmaterial(labSecuritySubjectmaterial);
    }

    /**
     * 批量删除实验室和准入模板关系
     *
     * @param ids 需要删除的实验室和准入模板关系主键
     * @return 结果
     */
    @Override
    public int deleteLabSecuritySubjectmaterialByIds(Long[] ids)
    {
        return labSecuritySubjectmaterialMapper.deleteLabSecuritySubjectmaterialByIds(ids);
    }

    /**
     * 删除实验室和准入模板关系信息
     *
     * @param id 实验室和准入模板关系主键
     * @return 结果
     */
    @Override
    public int deleteLabSecuritySubjectmaterialById(Long id)
    {
        return labSecuritySubjectmaterialMapper.deleteLabSecuritySubjectmaterialById(id);
    }

    /**
     * 查询教职工模板
     *
     * @return
     */
    @Override
    public List<LabSecuritySubjectmaterialVO> selectTeacherList() {
        return labSecuritySubjectmaterialMapper.selectTeacherList();
    }

    /**
     * 查询学生模板
     *
     * @return
     */
    @Override
    public List<LabSecuritySubjectmaterialVO> selectStudentList() {
        return labSecuritySubjectmaterialMapper.selectStudentList();
    }

    /**
     * 根据实验室ID删除
     *
     * @param id
     * @return
     */
    @Override
    public int deleteLabSecurityMaterialDataBysubjectId(Long id) {
        return labSecuritySubjectmaterialMapper.deleteLabSecurityMaterialDataBysubjectId(id);
    }

    /***
     * 模板插入
     * @param labSecuritySubjectmaterialVO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addList(LabSecuritySubjectmaterialVO labSecuritySubjectmaterialVO) {
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        List<LabSubject> labSubjectList= labSecuritySubjectmaterialVO.getLabSubjectList();//实验室集合
        //删除改实验室模板重新添加
        labSecuritySubjectmaterialMapper.deleteLabSecurityMaterialDataBysubjectId(labSubjectList.get(0).getId());
        int id=0;
        for (int i=0;i<labSubjectList.size();i++){
            //教职工模板
            List<LabSecuritySubjectmaterial> teacherList= labSecuritySubjectmaterialVO.getTeacherList();
            if(teacherList!=null && teacherList.size()>0){
                for (int j=0;j<teacherList.size();j++){
                    //插入数据
                    teacherList.get(j).setCretaId(sysUser.getUserId());//创建人
                    teacherList.get(j).setCreateTime(new Date());//创建时间
                    teacherList.get(j).setSubjectId(labSubjectList.get(i).getId());//实验室id
                    teacherList.get(j).setMaterialId(teacherList.get(j).getMaterialId());//模板id
                    teacherList.get(j).setMaterialType(2L);//模板类型
                    id= labSecuritySubjectmaterialMapper.insertLabSecuritySubjectmaterial(teacherList.get(j));
                }
            }


            //学生模板
            List<LabSecuritySubjectmaterial> studentList= labSecuritySubjectmaterialVO.getStudentList();
            if(studentList!=null && studentList.size()>0) {
                for (int k = 0; k < studentList.size(); k++) {
                    //插入数据
                    studentList.get(k).setCretaId(sysUser.getUserId());
                    studentList.get(k).setCreateTime(new Date());
                    studentList.get(k).setSubjectId(labSubjectList.get(i).getId());//实验室id
                    studentList.get(k).setMaterialId(studentList.get(k).getMaterialId());//模板id
                    studentList.get(k).setMaterialType(3L);//模板类型
                    id = labSecuritySubjectmaterialMapper.insertLabSecuritySubjectmaterial(studentList.get(k));
                }
            }
        }
        return id;
    }
}