package com.younike.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.time.LocalDateTime;
import java.util.stream.Collectors;

import com.younike.common.utils.SecurityUtils;
import com.younike.common.utils.StringUtils;
import com.younike.system.domain.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.younike.system.mapper.GoodsImageMapper;
import com.younike.system.domain.GoodsImage;
import com.younike.system.service.IGoodsImageService;

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

/**
 * 商品图片Service业务层处理
 * 
 * @author elev
 * @date 2022-01-06
 */
@Service
public class GoodsImageServiceImpl implements IGoodsImageService {
    @Autowired
    private GoodsImageMapper goodsImageMapper;

    /**
     * 查询商品图片
     * 
     * @param imageId 商品图片主键
     * @return 商品图片
     */
    @Override
    public GoodsImage selectGoodsImageByImageId(String imageId) {
        return goodsImageMapper.selectGoodsImageByImageId(imageId);
    }

    /**
     * 查询商品图片列表
     * 
     * @param goodsImage 商品图片
     * @return 商品图片
     */
    @Override
    public List<GoodsImage> selectGoodsImageList(GoodsImage goodsImage) {
        return goodsImageMapper.selectGoodsImageList(goodsImage);
    }

    /**
     * 新增商品图片
     * 
     * @param goodsImage 商品图片
     * @return 结果
     */
    @Override
    public int insertGoodsImage(GoodsImage goodsImage) {
        goodsImage.setCreateTime(LocalDateTime.now());
        return goodsImageMapper.insertGoodsImage(goodsImage);
    }

    /**
     * 修改商品图片
     * 
     * @param goodsImage 商品图片
     * @return 结果
     */
    @Override
    public int updateGoodsImage(GoodsImage goodsImage) {
        goodsImage.setUpdateTime(LocalDateTime.now());
        return goodsImageMapper.updateGoodsImage(goodsImage);
    }

    /**
     * 批量删除商品图片
     * 
     * @param imageIds 需要删除的商品图片主键
     * @return 结果
     */
    @Override
    public int deleteGoodsImageByImageIds(String[] imageIds) {
        return goodsImageMapper.deleteGoodsImageByImageIds(imageIds);
    }

    /**
     * 删除商品图片信息
     * 
     * @param imageId 商品图片主键
     * @return 结果
     */
    @Override
    public int deleteGoodsImageByImageId(String imageId)
    {
        return goodsImageMapper.deleteGoodsImageByImageId(imageId);
    }

    @Override
    public void changeGoodsImages(Long goodsId, String images) {
        goodsImageMapper.removeGoodsImageByGoodsId(goodsId);
        if (StringUtils.isBlank(images)){
            return;
        }
        Long currentUser = SecurityUtils.getUserId();
        LocalDateTime now = LocalDateTime.now();
        int sort = 1;
        List<GoodsImage> goodsImageList = new ArrayList<>();
        for (String url: Arrays.asList(images.split(","))){
            GoodsImage goodsImage = new GoodsImage();
            goodsImage.setGoodsId(goodsId.toString());
            goodsImage.setUrl(url);
            goodsImage.setType("001");
            goodsImage.setSorting(sort++);
            goodsImage.setFaceImg(url);
            goodsImage.setCreateBy(currentUser.toString());
            goodsImage.setCreateTime(now);
            goodsImageList.add(goodsImage);
        }
        goodsImageMapper.insertBatch(goodsImageList);
    }

    @Override
    public int removeGoodsImageByGoodsId(Long goodsId) {
        return goodsImageMapper.removeGoodsImageByGoodsId(goodsId);
    }

    @Override
    public void insertBatch(List<GoodsImage> goodsImageList) {
        goodsImageMapper.insertBatch(goodsImageList);
    }

    @Override
    public List<GoodsImage> goodsImageList(List<Long> goodsIds) {
        return goodsImageMapper.goodsImageList(goodsIds);
    }
}
