package com.younike.system.service.impl;

import java.util.List;
import java.time.LocalDateTime;

import com.younike.common.utils.SecurityUtils;
import com.younike.common.utils.bean.BeanUtils;
import com.younike.system.domain.vo.GoodsServerVO;
import com.younike.system.domain.vo.GoodsServerResultVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.younike.system.mapper.GoodsServerMapper;
import com.younike.system.domain.GoodsServer;
import com.younike.system.service.IGoodsServerService;

import static java.util.stream.Collectors.toList;

/**
 * 商品服务人员信息Service业务层处理
 * 
 * @author elev
 * @date 2022-01-08
 */
@Service
public class GoodsServerServiceImpl implements IGoodsServerService {
    @Autowired
    private GoodsServerMapper goodsServerMapper;
    
    @Override
    public List<GoodsServerResultVo> serverGoodsList(Long serverId) {
        return goodsServerMapper.serverGoodsList(serverId);
    }

    /**
     * 查询商品服务人员信息
     * 
     * @param goodsId 商品服务人员信息主键
     * @return 商品服务人员信息
     */
    @Override
    public GoodsServer selectGoodsServerByGoodsId(Long goodsId) {
        return goodsServerMapper.selectGoodsServerByGoodsId(goodsId);
    }

    /**
     * 查询商品服务人员信息列表
     * 
     * @param goodsServer 商品服务人员信息
     * @return 商品服务人员信息
     */
    @Override
    public List<GoodsServer> selectGoodsServerList(GoodsServer goodsServer) {
        return goodsServerMapper.selectGoodsServerList(goodsServer);
    }

    /**
     * 新增商品服务人员信息
     * 
     * @param goodsServer 商品服务人员信息
     * @return 结果
     */
    @Override
    public int insertGoodsServer(GoodsServer goodsServer) {
        goodsServer.setCreateTime(LocalDateTime.now());
        return goodsServerMapper.insertGoodsServer(goodsServer);
    }

    /**
     * 修改商品服务人员信息
     * 
     * @param goodsServer 商品服务人员信息
     * @return 结果
     */
    @Override
    public int updateGoodsServer(GoodsServer goodsServer) {
        goodsServer.setUpdateTime(LocalDateTime.now());
        return goodsServerMapper.updateGoodsServer(goodsServer);
    }

    /**
     * 批量删除商品服务人员信息
     * 
     * @param goodsIds 需要删除的商品服务人员信息主键
     * @return 结果
     */
    @Override
    public int deleteGoodsServerByGoodsIds(Long[] goodsIds) {
        return goodsServerMapper.deleteGoodsServerByGoodsIds(goodsIds);
    }

    /**
     * 删除商品服务人员信息信息
     * 
     * @param goodsId 商品服务人员信息主键
     * @return 结果
     */
    @Override
    public int deleteGoodsServerByGoodsId(Long goodsId)
    {
        return goodsServerMapper.deleteGoodsServerByGoodsId(goodsId);
    }

    @Override
    public void removeGoodsServerByGoodsId(Long goodsId) {
        goodsServerMapper.removeGoodsServerByGoodsId(goodsId);
    }

    @Override
    public void insertBatch(List<GoodsServer> goodsServerList) {
        goodsServerMapper.insertBatch(goodsServerList);
    }

    @Override
    public List<GoodsServerVO> goodsServerList(List<Long> goodsIds) {
        return goodsServerMapper.goodsServerList(goodsIds);
    }

    @Override
    public void changeGoodsServers(Long goodsId, List<GoodsServerVO> serverUserList) {
        removeGoodsServerByGoodsId(goodsId);
        if (CollectionUtils.isEmpty(serverUserList)){
            return;
        }
        Long currentUser = SecurityUtils.getUserId();
        LocalDateTime now = LocalDateTime.now();
        List<GoodsServer> goodsServerList = serverUserList.stream().map(goodsServerVO -> {
            GoodsServer goodsServer = new GoodsServer();
            BeanUtils.copyBeanProp(goodsServer, goodsServerVO);
            goodsServer.setGoodsId(goodsId);
            goodsServer.setCreateTime(now);
            goodsServer.setCreateBy(currentUser.toString());
            return goodsServer;
        }).collect(toList());
        insertBatch(goodsServerList);
    }
}
