package com.izouma.dingdong.service.merchant;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.izouma.dingdong.config.Constants;
import com.izouma.dingdong.converter.LongArrayConverter;
import com.izouma.dingdong.domain.merchant.Goods;
import com.izouma.dingdong.domain.merchant.MerchantClassification;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.merchant.GoodsRepo;
import com.izouma.dingdong.repo.merchant.MerchantClassificationRepo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class MerchantClassificationService {

    private MerchantClassificationRepo merchantClassificationRepo;

    private MerchantService merchantService;

    private GoodsRepo goodsRepo;


    /*
    添加商家自定义分类
     */
    public MerchantClassification add(Long id, MerchantClassification classification) {


        Long merchantId = merchantService.findMerchantId(id);
        if (ObjectUtil.isNotNull(merchantClassificationRepo.findByMerchantIdAndName(merchantId, classification.getName()))) {
            throw new BusinessException("该分类已存在");
        }

        MerchantClassification build = MerchantClassification.builder()
                .name(classification.getName())
                .goodsIds(classification.getGoodsIds())
                .sort(classification.getSort())
                .enabled(true)
                .type(4)
                .build();

        //根据用户ID设置商户ID
        build.setMerchantId(merchantId);
        if (ObjectUtil.isNull(classification.getIsOpen())) {
            build.setIsOpen(true);
        } else {
            build.setIsOpen(classification.getIsOpen());
        }

        return merchantClassificationRepo.save(build);
    }

    /*
    显示类别下的商品列表
     */
    public List<Goods> showGoods(Long classificationId) {
        MerchantClassification merchantClassification = merchantClassificationRepo.findById(classificationId)
                .orElseThrow(new BusinessException("分类不存在"));
        LongArrayConverter converter = new LongArrayConverter();
        //List<Long> goodsIds = merchantClassification.getGoodsIds();
        List<Long> goodsIds = converter.convertToEntityAttribute(merchantClassification.getGoodsIds());

        List<Goods> goods = CollUtil.newArrayList();
        goodsIds.forEach(s ->
                goods.add(goodsRepo.findById(s).orElse(null))
        );
        return goods;
    }

    /**
     * 分类下添加商品
     *
     * @param classificationId 分类Id
     * @param string           拼接好的goodsId字符串
     * @return 分类
     */
    public MerchantClassification saveGoods(Long classificationId, String string) {
        //查找分类
        MerchantClassification merchantClassification = merchantClassificationRepo.findById(classificationId)
                .orElseThrow(new BusinessException("无分类"));

        //List<Long> goodsIds = merchantClassification.getGoodsIds();
        LongArrayConverter converter = new LongArrayConverter();
        List<Long> longs = converter.convertToEntityAttribute(string);

        List<Long> goodsIds = converter.convertToEntityAttribute(merchantClassification.getGoodsIds());

        longs.stream().filter(l -> !goodsIds.contains(l)).forEach(goodsIds::add);
        //goodsIds.addAll(longs);

        String ids = converter.convertToDatabaseColumn(goodsIds);

        merchantClassification.setGoodsIds(ids);

        return merchantClassificationRepo.save(merchantClassification);
    }

    /*
    分类下一个个添加商品
     */
    public void saveOneGoods(Long classificationId, Long goodsId) {
        //查找分类
        MerchantClassification merchantClassification = merchantClassificationRepo.findById(classificationId)
                .orElseThrow(new BusinessException("无分类"));

//        List<Long> ids = merchantClassification.getGoodsIds();

        LongArrayConverter converter = new LongArrayConverter();
        List<Long> ids = converter.convertToEntityAttribute(merchantClassification.getGoodsIds());

        if (!ids.contains(goodsId)) {
            ids.add(goodsId);
            merchantClassification.setGoodsIds(converter.convertToDatabaseColumn(ids));
            //如果是点单必读
            if (merchantClassification.getName().equals(Constants.CLASSIFICATION_NAME3)) {
                if (!merchantClassification.getIsShow()) {
                    merchantClassification.setIsShow(true);
                }
            }
            merchantClassificationRepo.save(merchantClassification);
        }
    }

    /*
    分类下删除商品
     */
    public void delGoods(Long classificationId, Long goodId) {
        //查找分类
        MerchantClassification merchantClassification = merchantClassificationRepo.findById(classificationId)
                .orElseThrow(new BusinessException("无分类"));

        LongArrayConverter converter = new LongArrayConverter();
        List<Long> goodsIds = converter.convertToEntityAttribute(merchantClassification.getGoodsIds());

        //List<Long> goodsIds = merchantClassification.getGoodsIds();
        goodsIds.remove(goodId);

        merchantClassification.setGoodsIds(converter.convertToDatabaseColumn(goodsIds));

        merchantClassificationRepo.save(merchantClassification);
    }

}
