package com.czg.service.account.service.impl;

import com.czg.account.dto.pad.*;
import com.czg.account.entity.*;
import com.czg.account.service.*;
import com.czg.exception.ApiNotPrintException;
import com.czg.product.entity.ProdSku;
import com.czg.product.entity.Product;
import com.czg.product.service.ProdSkuService;
import com.czg.product.service.ProductService;
import com.czg.product.vo.ProductVO;
import com.czg.service.account.mapper.PadProductCategoryDetailMapper;
import com.czg.utils.JoinQueryWrapper;
import com.czg.utils.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
@Service
public class PadProdServiceImpl implements PadProdService {
    @Resource
    private PadProductCategoryDetailMapper padProductCategoryDetailMapper;
    @Resource
    private PadProductCategoryService padProductCategoryService;
    @DubboReference
    private ProductService productService;
    @DubboReference
    private ProdSkuService prodSkuService;
    @Resource
    private ShopProdCategoryService shopProdCategoryService;
    @Resource
    private PadLayoutService padLayoutService;
    @Resource
    private PadProductCategoryDetailService padProductCategoryDetailService;

    @Override
    public Page<PadProductCategoryDTO> pageInfo(Page<PadProductCategoryDTO> objectPage, Long productCategoryId, Long shopId) {
        PageHelper.startPage(PageUtil.buildPageHelp());
        return PageUtil.convert(new PageInfo<>(padProductCategoryDetailMapper.selectPageByKeyAndShopId(shopId, productCategoryId)));
    }

    @Override
    public PadDetailDTO detail(Long shopId, Long padProductCategory) {
        PadProductCategory padCategory = padProductCategoryService.getOne(new QueryWrapper().eq(PadProductCategory::getShopId, shopId).eq(PadProductCategory::getId, padProductCategory));
        if (padCategory == null) {
            throw new ApiNotPrintException("pad商品分类不存在");
        }

        PadDetailDTO padDetailDTO = new PadDetailDTO();
        padDetailDTO.setId(padProductCategory);
        padDetailDTO.setPadLayoutId(padCategory.getPadLayoutId());
        List<Long> productIds = padProductCategoryDetailMapper.selectListByQuery(new QueryWrapper().eq(PadProductCategoryDetail::getPadProductCategoryId, padProductCategory)).stream().map(PadProductCategoryDetail::getProductId).toList();
        padDetailDTO.setProductIdList(productIds);
        if (!productIds.isEmpty()) {
            List<ProductVO> products = productService.listAndLowPrice(shopId, productIds);
            products.parallelStream().forEach(item -> {
                List<ProdSku> skuList = prodSkuService.list(new QueryWrapper().eq(ProdSku::getProductId, item.getId()));
                item.setSkuList(skuList);
            });
            padDetailDTO.setProductList(products);
        }

        return padDetailDTO;
    }

    @Override
    public Boolean add(Long shopId, PadDetailAddDTO padDetailAddDTO) {
        long count = shopProdCategoryService.count(new QueryWrapper().eq(ShopProdCategory::getId, padDetailAddDTO.getProductCategoryId()));
        if (count == 0) {
            throw new ApiNotPrintException("商品分类不存在");
        }

        checkInfo(shopId, padDetailAddDTO.getPadLayoutId(), padDetailAddDTO.getProductIdList());

        PadProductCategory padProductCategory = new PadProductCategory().setPadLayoutId(padDetailAddDTO.getPadLayoutId()).setProductCategoryId(padDetailAddDTO.getProductCategoryId())
                .setShopId(shopId);
        padProductCategory.setSort(0);
        padProductCategoryService.save(padProductCategory);

        ArrayList<PadProductCategoryDetail> details = new ArrayList<>();
        padDetailAddDTO.getProductIdList().forEach(item -> details.add(new PadProductCategoryDetail().setPadProductCategoryId(padProductCategory.getId()).setProductId(item)));
        return padProductCategoryDetailService.saveBatch(details);
    }

    @Override
    public Boolean edit(Long shopId, PadDetailEditDTO padDetailEditDTO) {
        PadProductCategory category = padProductCategoryService.getOne(new QueryWrapper().eq(PadProductCategory::getShopId, shopId).eq(PadProductCategory::getId, padDetailEditDTO.getId()));
        if (category == null) {
            throw new ApiNotPrintException("pad商品不存在");
        }

        checkInfo(shopId, padDetailEditDTO.getPadLayoutId(), padDetailEditDTO.getProductIdList());

        category.setPadLayoutId(padDetailEditDTO.getPadLayoutId());
        category.setSort(padDetailEditDTO.getSort());
        padProductCategoryService.updateById(category);

        if (padDetailEditDTO.getProductIdList() != null && !padDetailEditDTO.getProductIdList().isEmpty()) {
            padProductCategoryDetailService.remove(new QueryWrapper().eq(PadProductCategoryDetail::getPadProductCategoryId, padDetailEditDTO.getId()));
            ArrayList<PadProductCategoryDetail> details = new ArrayList<>();
            padDetailEditDTO.getProductIdList().forEach(item -> details.add(new PadProductCategoryDetail().setPadProductCategoryId(padDetailEditDTO.getId()).setProductId(item)));
            return padProductCategoryDetailService.saveBatch(details);
        }
        return true;
    }

    private void checkInfo(Long shopId, Long padLayoutId, List<Long> productIdList) {
        long layOutCount = padLayoutService.count(new QueryWrapper().eq(PadLayout::getId, padLayoutId).eq(PadLayout::getIsDel, 0));
        if (layOutCount == 0) {
            throw new ApiNotPrintException("pad布局不存在");
        }

        if (productIdList != null && !productIdList.isEmpty()) {
            long proCount = productService.count(new QueryWrapper().eq(Product::getShopId, shopId).in(Product::getId, productIdList));
            if (proCount != productIdList.size()) {
                throw new ApiNotPrintException("存在错误商品id");
            }
        }
    }

    @Override
    public Boolean del(Long shopId, PadDetailDelDTO delDTO) {
        PadProductCategory category = padProductCategoryService.getOne(new QueryWrapper().eq(PadProductCategory::getShopId, shopId).eq(PadProductCategory::getId, delDTO.getId()));
        if (category == null) {
            throw new ApiNotPrintException("pad商品不存在");
        }
        padProductCategoryService.removeById(delDTO.getId());

        return padProductCategoryDetailService.remove(new QueryWrapper().eq(PadProductCategoryDetail::getPadProductCategoryId, delDTO.getId()));
    }
}
