package com.mortals.xhx.module.product.service.impl;

import com.mortals.framework.model.PageInfo;
import com.mortals.xhx.module.category.model.CategoryEntity;
import com.mortals.xhx.module.category.service.CategoryService;
import com.mortals.xhx.module.company.model.CompanyEntity;
import com.mortals.xhx.module.company.model.CompanyProductEntity;
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.setCompanyId(companyIds);
            item.setCategoryId(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());
        }

        if (!ObjectUtils.isEmpty(entity.getCompanyId())) {
            List<CompanyProductEntity> companyProductList = Arrays.asList(entity.getCompanyId().split(",")).stream().map(item -> {
                CompanyEntity companyEntity = companyService.get(Long.parseLong(item), context);
                CompanyProductEntity companyProductEntity = new CompanyProductEntity();
                companyProductEntity.initAttrValue();
                companyProductEntity.setProductId(entity.getId());
                companyProductEntity.setProductName(entity.getProductName());
                companyProductEntity.setCompanyId(companyEntity.getId());
                companyProductEntity.setCompanyName(companyEntity.getCompanyName());
                companyProductEntity.setCreateUserId(this.getContextUserId(context));
                companyProductEntity.setCreateTime(new Date());
                return companyProductEntity;
            }).collect(Collectors.toList());
            if (!ObjectUtils.isEmpty(companyProductList)) {
                companyProductService.save(companyProductList);
            }
        }

        if (!ObjectUtils.isEmpty(entity.getCategoryId())) {
            List<ProductCategoryEntity> productCategoryList = Arrays.asList(entity.getCategoryId().split(",")).stream().map(item -> {
                CategoryEntity categoryEntity = categoryService.get(Long.parseLong(item), context);
                ProductCategoryEntity productCategoryEntity = new ProductCategoryEntity();
                productCategoryEntity.initAttrValue();
                productCategoryEntity.setProductId(entity.getId());
                productCategoryEntity.setProductName(entity.getProductName());
                productCategoryEntity.setCategoryId(categoryEntity.getId());
                productCategoryEntity.setCategoryName(categoryEntity.getName());
                productCategoryEntity.setCreateUserId(this.getContextUserId(context));
                productCategoryEntity.setCreateTime(new Date());
                return productCategoryEntity;
            }).collect(Collectors.toList());

            if (!ObjectUtils.isEmpty(productCategoryList)) {
                productCategoryService.save(productCategoryList);
            }
        }


        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());
        }

        if (!ObjectUtils.isEmpty(entity.getCompanyId())) {
            List<CompanyProductEntity> companyProductList = Arrays.asList(entity.getCompanyId().split(",")).stream().map(item -> {
                Long[] companyProductRemoveIds = companyProductService.find(new CompanyProductQuery().productId(Long.parseLong(item))).stream().map(i -> i.getId()).toArray(Long[]::new);
                if (!ObjectUtils.isEmpty(companyProductRemoveIds)) {
                    companyProductService.remove(companyProductRemoveIds, context);
                }
                CompanyEntity companyEntity = companyService.get(Long.parseLong(item), context);
                CompanyProductEntity companyProductEntity = new CompanyProductEntity();
                companyProductEntity.initAttrValue();
                companyProductEntity.setProductId(entity.getId());
                companyProductEntity.setProductName(entity.getProductName());
                companyProductEntity.setCompanyId(companyEntity.getId());
                companyProductEntity.setCompanyName(companyEntity.getCompanyName());
                companyProductEntity.setCreateUserId(this.getContextUserId(context));
                companyProductEntity.setCreateTime(new Date());
                return companyProductEntity;
            }).collect(Collectors.toList());
            if (!ObjectUtils.isEmpty(companyProductList)) {
                companyProductService.save(companyProductList);
            }
        }

        if (!ObjectUtils.isEmpty(entity.getCategoryId())) {
            List<ProductCategoryEntity> productCategoryList = Arrays.asList(entity.getCategoryId().split(",")).stream().map(item -> {
                Long[] productCategoryRemoveIds = productCategoryService.find(new ProductCategoryQuery().productId(Long.parseLong(item))).stream().map(i -> i.getId()).toArray(Long[]::new);
                if (!ObjectUtils.isEmpty(productCategoryRemoveIds)) {
                    productCategoryService.remove(productCategoryRemoveIds, context);
                }

                CategoryEntity categoryEntity = categoryService.get(Long.parseLong(item), context);
                ProductCategoryEntity productCategoryEntity = new ProductCategoryEntity();
                productCategoryEntity.initAttrValue();
                productCategoryEntity.setProductId(entity.getId());
                productCategoryEntity.setProductName(entity.getProductName());
                productCategoryEntity.setCategoryId(categoryEntity.getId());
                productCategoryEntity.setCategoryName(categoryEntity.getName());
                productCategoryEntity.setCreateUserId(this.getContextUserId(context));
                productCategoryEntity.setCreateTime(new Date());
                return productCategoryEntity;
            }).collect(Collectors.toList());

            if (!ObjectUtils.isEmpty(productCategoryList)) {
                productCategoryService.save(productCategoryList);
            }
        }


        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);
    }
}