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

import java.util.List;

import com.sf.common.utils.DateUtils;
import com.sf.system.deployment.domain.DeploymentWhitelistInfo;
import com.sf.system.deployment.domain.DeploymentWhitelistList;
import com.sf.system.deployment.mapper.DeploymentWhitelistInfoMapper;
import com.sf.system.deployment.mapper.DeploymentWhitelistListMapper;
import com.sf.system.deployment.service.IDeploymentWhitelistInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.sf.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 白名单列表Service业务层处理
 *
 * @author ztzh
 * @date 2024-05-06
 */
@Service
public class DeploymentWhitelistInfoServiceImpl implements IDeploymentWhitelistInfoService {
    @Autowired
    private DeploymentWhitelistInfoMapper deploymentWhitelistInfoMapper;
    @Autowired
    private DeploymentWhitelistListMapper deploymentWhitelistListMapper;

    /**
     * 查询白名单列表
     *
     * @param id 白名单列表主键
     * @return 白名单列表
     */
    @Override
    public DeploymentWhitelistInfo selectDeploymentWhitelistInfoById(Long id) {
        return deploymentWhitelistInfoMapper.selectDeploymentWhitelistInfoById(id);
    }

    /**
     * 查询白名单列表列表
     *
     * @param deploymentWhitelistInfo 白名单列表
     * @return 白名单列表
     */
    @Override
    public List<DeploymentWhitelistInfo> selectDeploymentWhitelistInfoList(DeploymentWhitelistInfo deploymentWhitelistInfo) {
        List<DeploymentWhitelistInfo> infos = deploymentWhitelistInfoMapper.selectDeploymentWhitelistInfoList(deploymentWhitelistInfo);
        infos.forEach(deploymentWhitelistInfo1 -> {
            DeploymentWhitelistList deploymentWhitelistList = new DeploymentWhitelistList();
            deploymentWhitelistList.setWhitelistId(deploymentWhitelistInfo1.getId());
            List<DeploymentWhitelistList> list = deploymentWhitelistListMapper.selectDeploymentWhitelistListList(deploymentWhitelistList);
            deploymentWhitelistInfo1.setDeploymentWhitelistListList(list);
        });
        return infos;
    }

    /**
     * 新增白名单列表
     *
     * @param deploymentWhitelistInfo 白名单列表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDeploymentWhitelistInfo(DeploymentWhitelistInfo deploymentWhitelistInfo) {
        deploymentWhitelistInfo.setCreateTime(DateUtils.getNowDate());
        int rows = deploymentWhitelistInfoMapper.insertDeploymentWhitelistInfo(deploymentWhitelistInfo);
        insertDeploymentWhitelistList(deploymentWhitelistInfo);
        return rows;
    }


    /**
     * 修改白名单列表
     *
     * @param deploymentWhitelistInfo 白名单列表
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDeploymentWhitelistInfo(DeploymentWhitelistInfo deploymentWhitelistInfo) {
        return deploymentWhitelistInfoMapper.updateDeploymentWhitelistInfo(deploymentWhitelistInfo);
    }

    @Override
    public int publish(Long id) {
        DeploymentWhitelistInfo deploymentWhitelistInfo = deploymentWhitelistInfoMapper.selectDeploymentWhitelistInfoById(id);
        deploymentWhitelistInfo.setStatus(1L);
        return deploymentWhitelistInfoMapper.updateDeploymentWhitelistInfo(deploymentWhitelistInfo);
    }

    @Override
    public int soldOut(Long id) {
        DeploymentWhitelistInfo deploymentWhitelistInfo = deploymentWhitelistInfoMapper.selectDeploymentWhitelistInfoById(id);
        deploymentWhitelistInfo.setStatus(0L);
        return deploymentWhitelistInfoMapper.updateDeploymentWhitelistInfo(deploymentWhitelistInfo);
    }

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

    /**
     * 删除白名单列表信息
     *
     * @param id 白名单列表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDeploymentWhitelistInfoById(Long id) {
        deploymentWhitelistInfoMapper.deleteDeploymentWhitelistListByWhitelistId(id);
        return deploymentWhitelistInfoMapper.deleteDeploymentWhitelistInfoById(id);
    }


    /**
     * 新增白名单成员列信息
     *
     * @param deploymentWhitelistInfo 白名单列表对象
     */
    public void insertDeploymentWhitelistList(DeploymentWhitelistInfo deploymentWhitelistInfo) {
        List<DeploymentWhitelistList> deploymentWhitelistListList = deploymentWhitelistInfo.getDeploymentWhitelistListList();
        Long id = deploymentWhitelistInfo.getId();
        if (StringUtils.isNotNull(deploymentWhitelistListList)) {
            List<DeploymentWhitelistList> list = new ArrayList<DeploymentWhitelistList>();
            for (DeploymentWhitelistList deploymentWhitelistList : deploymentWhitelistListList) {
                deploymentWhitelistList.setWhitelistId(id);
                list.add(deploymentWhitelistList);
            }
            if (list.size() > 0) {
                deploymentWhitelistInfoMapper.batchDeploymentWhitelistList(list);
            }
        }
    }
}
