package com.mortals.xhx.module.product.service.impl;
import com.mortals.framework.model.PageInfo;
import com.mortals.xhx.module.category.service.CategoryService;
import com.mortals.xhx.module.company.model.CompanyProductQuery;
import com.mortals.xhx.module.company.service.CompanyProductService;
import com.mortals.xhx.module.company.service.CompanyService;
import com.mortals.xhx.module.product.model.*;
import com.mortals.xhx.module.product.service.ProductCategoryService;
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.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
/**
* ProductService
* 产品 service实现
*
* @author zxfei
* @date 2023-09-18
*/
@Service("productService")
@Slf4j
public class ProductServiceImpl extends AbstractCRUDServiceImpl<ProductDao, ProductEntity, Long> implements ProductService {
    @Autowired
    private ProductQuestionService productQuestionService;

    @Autowired
    private ProductCategoryService productCategoryService;
    @Autowired
    private CompanyProductService companyProductService;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CompanyService companyService;



    @Override
    protected void findAfter(ProductEntity params, PageInfo pageInfo, Context context, List<ProductEntity> list) throws AppException {
        list.forEach(item->{
            String categoryIds = productCategoryService.find(new ProductCategoryQuery().productId(item.getId())).stream().map(i -> i.getCategoryId().toString()).collect(Collectors.joining(","));
            String companyIds = companyProductService.find(new CompanyProductQuery().productId(item.getId())).stream().map(i -> i.getProductId().toString()).collect(Collectors.joining(","));
            item.setCompanyIds(companyIds);
            item.setCategoryIds(categoryIds);
        });
    }

    @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.setProductName(entity.getProductName());
                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.setProductName(entity.getProductName());
                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);
    }
}