package com.mortals.xhx.module.product.service.impl;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.product.dao.ProductDao;
import com.mortals.xhx.module.product.model.ProductEntity;
import com.mortals.xhx.module.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.product.model.ProductQuestionEntity;
import com.mortals.xhx.module.product.model.ProductQuestionQuery;
import com.mortals.xhx.module.product.service.ProductQuestionService;
import org.springframework.util.ObjectUtils;
import java.util.Date;
import java.util.Arrays;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
/**
* ProductService
* 产品 service实现
*
* @author zxfei
* @date 2023-09-11
*/
@Service("productService")
@Slf4j
public class ProductServiceImpl extends AbstractCRUDServiceImpl<ProductDao, ProductEntity, Long> implements ProductService {
    @Autowired
    private ProductQuestionService productQuestionService;
    
    @Override
    protected void saveAfter(ProductEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getProductQuestionList())){
            entity.getProductQuestionList().stream().peek(item->{
                item.setProductId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
            }).count();
            productQuestionService.save(entity.getProductQuestionList());
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(ProductEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getProductQuestionList())){
            Long[] productQuestionIds = productQuestionService.find(new ProductQuestionQuery().productId(entity.getId())).stream().map(ProductQuestionEntity::getId).toArray(Long[]::new);
            productQuestionService.remove(productQuestionIds,context);
            entity.getProductQuestionList().stream().peek(item ->{
                item.setProductId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
                item.setUpdateUserId(this.getContextUserId(context));
                item.setUpdateTime(new Date());
            }).count();
            productQuestionService.save(entity.getProductQuestionList());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<ProductQuestionEntity> productQuestionlist = productQuestionService.find(new ProductQuestionQuery().productIdList(Arrays.asList(ids)));
        productQuestionService.removeList(productQuestionlist,context);
        super.removeAfter(ids, context, result);
    }
}