package cn.wisenergy.service.Manager;

import cn.wisenergy.mapper.AntiFakeMapper;
import cn.wisenergy.mapper.ProductMapper;
import cn.wisenergy.model.app.AntiFake;
import cn.wisenergy.model.app.ProductInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author 86187
 */
@Component
public class AntiFakeManger {
    @Autowired
    private AntiFakeMapper antiFakeMapper;

    @Autowired
    private ProductMapper productMapper;

    @Transactional(rollbackFor = Exception.class)
    public Boolean updateAntiFake(String batchNumber) {
        //修改二维码使用状态
        int updateUseStatus = antiFakeMapper.updateUseStatus(batchNumber);
        return updateUseStatus > 0;
    }

    /**
     * 保存二维码信息和产品认证信息
     *
     * @param list        二维码信息
     * @param productInfo 产品认证信息
     * @return true of false
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean addAntiFakeAndProductInfo(List<AntiFake> list, ProductInfo productInfo) {
        if (!CollectionUtils.isEmpty(list)) {
            int count = antiFakeMapper.creates(list);
            if (count != list.size()) {
                return false;
            }
        }

        if (null != productInfo) {
            int count = productMapper.add(productInfo);
            return count != 0;
        }
        return true;
    }
}
