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

import cn.hutool.core.util.IdUtil;
import com.backendsys.exception.CustException;
import com.backendsys.modules.b2c.good.dao.GoodDao;
import com.backendsys.modules.b2c.good.dao.GoodI18nDao;
import com.backendsys.modules.b2c.good.entity.Good;
import com.backendsys.modules.b2c.good.entity.GoodI18n;
import com.backendsys.modules.b2c.good.service.GoodService;
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 GoodServiceImpl implements GoodService {

    @Autowired
    private GoodDao goodDao;
    @Autowired
    private GoodI18nDao goodI18nDao;

    /**
     * 获取商品列表
     */
    @Override
    public PageEntity selectGoodList(Good good) {
        PageUtils.startPage();  // 分页
        List<Map<String, Object>> list = goodDao.selectGoodList(good);
        return new PageInfoResult(list).toEntity();
    }

    /**
     * 获取商品详情
     */
    @Override
    public Map<String, Object> selectGoodDetail(Good good) {
        Map<String, Object> detail = goodDao.selectGoodDetail(good);
        if (detail == null) throw new CustException("商品不存在");
        return detail;
    }

    /**
     * 创建商品
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> insertGood(Good good) {

        // 生成随机UUID
        good.setUid(IdUtil.simpleUUID().substring(0, 10));
        goodDao.insert(good);

        // 批量插入 (翻译字段)
        Long good_id = good.getId();
        List<GoodI18n> translations = good.getTranslations().stream()
                .peek(item -> { ValidationUtil.validateObject(item, GoodI18n.Create.class); })
                .map(item -> { item.setGood_id(good_id); return item; })
                .collect(Collectors.toList());
        goodI18nDao.insertBatch(translations);

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

    /**
     * 编辑商品
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> updateGood(Good good) {

        Long good_id = good.getGood_id();

        Good goodDetail = goodDao.selectById(good_id);
        if (goodDetail == null) throw new CustException("商品不存在");

        // 更新
        good.setId(good_id);
        goodDao.updateById(good);

        // 批量更新 (翻译字段)
        List<GoodI18n> translations = good.getTranslations().stream()
            .peek(item -> { ValidationUtil.validateObject(item, GoodI18n.Update.class); })
            .map(t -> { t.setGood_id(good_id); return t; })
            .collect(Collectors.toList());

        goodI18nDao.updateBatch(translations);

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

    /**
     * 删除商品
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> deleteGood(Good good) {

        Long good_id = good.getGood_id();

        Good detail = goodDao.selectById(good_id);
        if (detail == null) throw new CustException("商品不存在");

        goodDao.deleteById(good_id);
        goodI18nDao.delete(new QueryWrapper<GoodI18n>().eq("good_id", good_id));

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

}
