package com.sf.system.deployment.service.impl;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

import com.sf.common.enums.RequestHeaderEnums;
import com.sf.common.utils.DateUtils;
import com.sf.common.utils.StringUtils;
import com.sf.common.utils.http.RequestUtils;
import com.sf.system.deployment.domain.DeploymentWhitelistList;
import com.sf.system.deployment.mapper.DeploymentWhitelistListMapper;
import com.sf.system.deployment.service.IDeploymentWhitelistListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * 白名单成员列Service业务层处理
 * 
 * @author ztzh
 * @date 2024-05-06
 */
@Service
public class DeploymentWhitelistListServiceImpl implements IDeploymentWhitelistListService
{
    @Autowired
    private DeploymentWhitelistListMapper deploymentWhitelistListMapper;

    /**
     * 查询白名单成员列
     * 
     * @param id 白名单成员列主键
     * @return 白名单成员列
     */
    @Override
    public DeploymentWhitelistList selectDeploymentWhitelistListById(Long id)
    {
        return deploymentWhitelistListMapper.selectDeploymentWhitelistListById(id);
    }

    /**
     * 查询白名单成员列列表
     * 
     * @param deploymentWhitelistList 白名单成员列
     * @return 白名单成员列
     */
    @Override
    public List<DeploymentWhitelistList> selectDeploymentWhitelistListList(DeploymentWhitelistList deploymentWhitelistList)
    {
        return deploymentWhitelistListMapper.selectDeploymentWhitelistListList(deploymentWhitelistList);
    }

    /**
     * 新增白名单成员列
     *
     * @param member 白名单成员列
     * @param username
     * @return 结果
     */
    @Override
    public int insertDeploymentWhitelistList(DeploymentWhitelistList member, String username)
    {
        if(null == member.getWhitelistId()){
            throw new SecurityException("参数异常WhitelistId为空");
        }
        member.setAppCode(RequestUtils.getHeader(RequestHeaderEnums.APP_CODE.getCode()));
        member.setCreated(username);
        member.setCreateTime(DateUtils.getNowDate());
        member.setModified(username);
        member.setUpdateTime(DateUtils.getNowDate());
        member.setCreateTime(DateUtils.getNowDate());
        return deploymentWhitelistListMapper.insertDeploymentWhitelistList(member);
    }

    /**
     * 修改白名单成员列
     * 
     * @param deploymentWhitelistList 白名单成员列
     * @return 结果
     */
    @Override
    public int updateDeploymentWhitelistList(DeploymentWhitelistList deploymentWhitelistList)
    {
        deploymentWhitelistList.setUpdateTime(DateUtils.getNowDate());
        return deploymentWhitelistListMapper.updateDeploymentWhitelistList(deploymentWhitelistList);
    }

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

    /**
     * 删除白名单成员列信息
     * 
     * @param id 白名单成员列主键
     * @return 结果
     */
    @Override
    public int deleteDeploymentWhitelistListById(Long id)
    {
        return deploymentWhitelistListMapper.deleteDeploymentWhitelistListById(id);
    }

    @Override
    public int batchImport(MultipartFile file, Long whitelistId, String username) throws IOException {
        String content = "";
        try {
            byte[] data = file.getBytes();
            content = new String(data, StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
        content.replaceAll("/n","");
        String[] split = StringUtils.split(content, ",");
        for (String user : split) {
            DeploymentWhitelistList menber = new DeploymentWhitelistList();
            menber.setWhitelistId(whitelistId);
            menber.setUser(user.trim());
            insertDeploymentWhitelistList(menber, username);
        }
        return 1;
    }
}
