package com.zd.alg.smartlock.service.impl;

import com.zd.alg.smartlock.domain.SlSubjectRelation;
import com.zd.alg.smartlock.domain.vo.SlSubjectRelationVo;
import com.zd.alg.smartlock.mapper.SlSubjectRelationMapper;
import com.zd.alg.smartlock.service.ISlSubjectRelationService;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.utils.StringUtils;
import com.zd.common.security.service.TokenService;
import com.zd.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 实验室关联Service业务层处理
 *
 * @author zd
 * @date 2022-09-03
 */
@Service
public class SlSubjectRelationServiceImpl implements ISlSubjectRelationService
{
    @Autowired
    private SlSubjectRelationMapper slSubjectRelationMapper;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询实验室关联
     *
     * @param id 实验室关联主键
     * @return 实验室关联
     */
    @Override
    public SlSubjectRelation selectSlSubjectRelationById(Long id)
    {
        return slSubjectRelationMapper.selectSlSubjectRelationById(id);
    }

    /**
     * 查询实验室关联列表
     *
     * @param slSubjectRelation 实验室关联
     * @return 实验室关联
     */
    @Override
    public List<SlSubjectRelationVo> selectSlSubjectRelationList(SlSubjectRelationVo slSubjectRelation)
    {
        return slSubjectRelationMapper.selectSlSubjectRelationList(slSubjectRelation);
    }

    @Override
    public List<SlSubjectRelation> getListByIds(List<Long> ids){
        if (ids.isEmpty()){
            new ArrayList<>();
        }
        return slSubjectRelationMapper.getListByIds(ids);
    }

    /**
     * 新增实验室关联
     *
     * @param vo 实验室关联
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSlSubjectRelation(SlSubjectRelationVo vo)
    {
        List<SlSubjectRelation> list = vo.getSlSubjectList();
        if(list==null || list.size()==0){
            throw new ServiceException("没有要关联的数据！");
        }
        System.out.println(SecurityUtils.getUsername());
        //校验本次提交是否存在已绑定情况
        SlSubjectRelationVo subjectRelation = new SlSubjectRelationVo();
        for (SlSubjectRelation sl:list ) {
            if(StringUtils.isEmpty(sl.getLockCode())){
                throw new ServiceException("房间: "+sl.getRoomName()+",位置: "+sl.getRoomLocation()+"未绑定门锁。");
            }
            subjectRelation.setLockRoomId(sl.getLockRoomId());
            List<SlSubjectRelationVo> sellist=  slSubjectRelationMapper.selectSlSubjectRelationList(subjectRelation);
            if(sellist.size()>0){
                throw new ServiceException("房间: "+sl.getRoomName()+",位置: "+sl.getRoomLocation()+"已被绑定。");
            }
        }

        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        //关系绑定
        for (SlSubjectRelation slsub:list ) {
            slsub.setDeptId(sysUser.getDept().getDeptId());
            slsub.setDeptName(sysUser.getDept().getDeptName());
            slsub.setUserId(sysUser.getUserId());
            slsub.setCreateBy(sysUser.getNickName());
            slsub.setCreatTime(new Date());
            slSubjectRelationMapper.insertSlSubjectRelation(slsub);
        }
        return 1;
    }

    /**
     * 修改实验室关联
     *
     * @param slSubjectRelation 实验室关联
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSlSubjectRelation(SlSubjectRelation slSubjectRelation)
    {
        return slSubjectRelationMapper.updateSlSubjectRelation(slSubjectRelation);
    }

    /**
     * 批量删除实验室关联
     *
     * @param ids 需要删除的实验室关联主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSlSubjectRelationByIds(Long[] ids)
    {
        return slSubjectRelationMapper.deleteSlSubjectRelationByIds(ids);
    }

    /**
     * 删除实验室关联信息
     *
     * @param id 实验室关联主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSlSubjectRelationById(Long id)
    {
        return slSubjectRelationMapper.deleteSlSubjectRelationById(id);
    }
}
