package com.backendsys.modules.b2c.good.service.impl;

import com.backendsys.exception.CustException;
import com.backendsys.modules.b2c.good.dao.GoodCategoryDao;
import com.backendsys.modules.b2c.good.dao.GoodCategoryI18nDao;
import com.backendsys.modules.b2c.good.entity.GoodCategory;
import com.backendsys.modules.b2c.good.entity.GoodCategoryI18n;
import com.backendsys.modules.b2c.good.service.GoodCategoryService;
import com.backendsys.modules.common.utils.ValidationUtil;
import com.backendsys.utils.response.PageEntity;
import com.backendsys.utils.response.PageInfoResult;
import com.backendsys.utils.v2.PageUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class GoodCategoryServiceImpl implements GoodCategoryService {

    @Autowired
    private GoodCategoryDao goodCategoryDao;
    @Autowired
    private GoodCategoryI18nDao goodCategoryI18nDao;

    /**
     * 获取商品分类列表
     */
    @Override
    public PageEntity selectGoodCategoryList(GoodCategory goodCategory) {
        PageUtils.startPage();  // 分页
        List<Map<String, Object>> list = goodCategoryDao.selectGoodCategoryList(goodCategory);
        return new PageInfoResult(list).toEntity();
    }

    /**
     * 获取商品分类详情
     */
    @Override
    public Map<String, Object> selectGoodCategoryDetail(GoodCategory goodCategory) {
        Map<String, Object> detail = goodCategoryDao.selectGoodCategoryDetail(goodCategory);
        if (detail == null) throw new CustException("商品分类不存在");
        return detail;
    }

    /**
     * 创建商品分类
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> insertGoodCategory(GoodCategory goodCategory) {

        goodCategoryDao.insert(goodCategory);

        // 批量插入 (翻译字段)
        Long good_category_id = goodCategory.getId();
        List<GoodCategoryI18n> translations = goodCategory.getTranslations().stream()
                .peek(item -> { ValidationUtil.validateObject(item, GoodCategoryI18n.Create.class); })
                .map(item -> { item.setGood_category_id(good_category_id); return item; })
                .collect(Collectors.toList());
        goodCategoryI18nDao.insertBatch(translations);

        return Map.of("good_category_id", good_category_id);
    }

    /**
     * 编辑商品分类
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> updateGoodCategory(GoodCategory goodCategory) {

        Long good_category_id = goodCategory.getGood_category_id();

        GoodCategory goodCategoryDetail = goodCategoryDao.selectById(good_category_id);
        if (goodCategoryDetail == null) throw new CustException("商品分类不存在");

        // 更新
        goodCategory.setId(good_category_id);
        goodCategoryDao.updateById(goodCategory);

        // 批量更新 (翻译字段)
        List<GoodCategoryI18n> translations = goodCategory.getTranslations().stream()
            .peek(item -> { ValidationUtil.validateObject(item, GoodCategoryI18n.Update.class); })
            .map(item -> { item.setGood_category_id(good_category_id); return item; })
            .collect(Collectors.toList());

        goodCategoryI18nDao.updateBatch(translations);

        return Map.of("good_category_id", good_category_id);
    }

    /**
     * 删除商品分类
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> deleteGoodCategory(GoodCategory goodCategory) {

        Long good_category_id = goodCategory.getGood_category_id();

        GoodCategory detail = goodCategoryDao.selectById(good_category_id);
        if (detail == null) throw new CustException("商品分类不存在");

        goodCategoryDao.deleteById(good_category_id);
        goodCategoryI18nDao.delete(new QueryWrapper<GoodCategoryI18n>().eq("good_category_id", good_category_id));

        return Map.of("good_category_id", good_category_id);
    }

}
