package com.ruoyi.warehouseBusiness.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.warehouseBusiness.domain.TWarehousebillsitemsElabel;
import com.ruoyi.warehouseBusiness.mapper.TWarehousebillsitemsElabelMapper;
import com.ruoyi.warehouseBusiness.service.ITWarehousebillsitemsElabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class TWarehousebillsitemsElabelServiceImpl implements ITWarehousebillsitemsElabelService {

    @Autowired
    private TWarehousebillsitemsElabelMapper elabelMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insert(List<TWarehousebillsitemsElabel> elabelList) {
        List<String> tempList = elabelList.stream()
                .map(TWarehousebillsitemsElabel::getfContent)
                .distinct()
                .collect(Collectors.toList());

        if (elabelList.size() != tempList.size()) {
            return AjaxResult.error("电子标签重复");
        }

        elabelList.forEach(elabel -> {
            if (ObjectUtil.isNull(elabel.getfId())) {
                elabelMapper.insert(elabel);
            } else {
                elabelMapper.updateById(elabel);
            }
        });
        return AjaxResult.success();
    }

    @Override
    public int deleteById(Long id) {
        return elabelMapper.deleteById(id);
    }

    @Override
    public List<TWarehousebillsitemsElabel> selectByfPid(Long fPid) {
        return elabelMapper.selectByfPid(fPid);
    }

    @Override
    public List<TWarehousebillsitemsElabel> selectByStockId(Long stockId) {
        return elabelMapper.selectByStockId(stockId);
    }
}
