package com.zd.laboratory.service.impl;

import java.util.*;

import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.datascope.annotation.DataScope;
import com.zd.common.security.service.TokenService;
import com.zd.laboratory.domain.LabWhiteJoinSublist;
import com.zd.laboratory.domain.vo.LabWhitelistVO;
import com.zd.laboratory.mapper.LabWhiteJoinSublistMapper;
import com.zd.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.laboratory.mapper.LabWhitelistMapper;
import com.zd.laboratory.domain.LabWhitelist;
import com.zd.laboratory.service.ILabWhitelistService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 白名单Service业务层处理
 *
 * @author zd
 * @date 2022-01-26
 */
@Service
public class LabWhitelistServiceImpl implements ILabWhitelistService
{
    @Autowired
    private LabWhitelistMapper labWhitelistMapper;

    @Autowired
    private LabWhiteJoinSublistMapper labWhiteJoinSublistMapper;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询白名单
     *
     * @param id 白名单主键
     * @return 白名单
     */
    @Override
    public LabWhitelist selectLabWhitelistById(Long id)
    {
        return labWhitelistMapper.selectLabWhitelistById(id);
    }

    /**
     * 查询白名单列表
     *
     * @param labWhitelist 白名单
     * @return 白名单
     */
    @Override
    @DataScope(deptAlias = "d" , userAlias = "u" , permi = PerPrefix.LABORATORY_WHITELIST)
    public List<LabWhitelist> selectLabWhitelistList(LabWhitelist labWhitelist)
    {
        return labWhitelistMapper.selectLabWhitelistList(labWhitelist);
    }

    /**
     * 查询白名单关联的实验室列表
     *
     * @param labWhitelistVO 白名单
     * @return 白名单
     */
    @Override
    public List<LabWhitelist> selectLabWhiteJoinSubList(LabWhitelistVO labWhitelistVO)
    {
        return labWhitelistMapper.selectLabWhiteJoinSubList(labWhitelistVO);
    }

    /**
     * 新增白名单
     *
     * @param labWhitelist 白名单
     * @return 结果
     */
    @Override
    public int insertLabWhitelist(LabWhitelist labWhitelist)
    {
        LabWhitelist llt = new LabWhitelist();
        llt.setUserId(labWhitelist.getUserId());
        List<LabWhitelist> list = labWhitelistMapper.selectLabWhitelistList(llt);
        if(list!=null && list.size()>0){
            throw new ServiceException("该用户已在白名单存在");
        }

        labWhitelist.setCreateTime(new Date());
        return labWhitelistMapper.insertLabWhitelist(labWhitelist);
    }


    /**
     * 修改白名单以及关联的实验室表
     *
     * @param labWhitelistVo 白名单
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int editWhiteBySubJoin(LabWhitelistVO labWhitelistVo)
    {
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        labWhiteJoinSublistMapper.deleteLabWhiteJoinSublistByUserId(labWhitelistVo.getUserId());
        Optional.ofNullable(labWhitelistVo.getLabWhiteJoinSubList()).orElseGet(Collections::emptyList)
                .stream().forEach(a->{
            a.setWhiteUserId(labWhitelistVo.getUserId());
            a.setCreateBy(sysUser.getNickName());
            a.setCreateTime(new Date());
            labWhiteJoinSublistMapper.insertLabWhiteJoinSublist(a);
        });
        return 1;
    }

    /**
     * 修改白名单
     *
     * @param labWhitelist 白名单
     * @return 结果
     */
    @Override
    public int updateLabWhitelist(LabWhitelist labWhitelist)
    {
        return labWhitelistMapper.updateLabWhitelist(labWhitelist);
    }

    /**
     * 批量删除白名单
     *
     * @param ids 需要删除的白名单主键
     * @return 结果
     */
    @Override
    public int deleteLabWhitelistByIds(Long[] ids)
    {
        return labWhitelistMapper.deleteLabWhitelistByIds(ids);
    }

    /**
     * 删除白名单信息
     *
     * @param id 白名单主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteLabWhitelistById(Long id)
    {
        //根据用户id删除关联表数据
        LabWhitelist labWhitelist= labWhitelistMapper.selectLabWhitelistById(id);
        if(labWhitelist!=null){
            LabWhiteJoinSublist labWhiteJoinSublist = new LabWhiteJoinSublist();
            labWhiteJoinSublist.setWhiteUserId(labWhitelist.getUserId());
            labWhiteJoinSublistMapper.deleteLabWhiteJoinSublistByParameter(labWhiteJoinSublist);
        }


        //删除白名单表数据
        return labWhitelistMapper.deleteLabWhitelistById(id);
    }
}
