
package com.tmerclub.cloud.order.service.impl;

import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.util.PageUtil;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.i18n.LanguageEnum;
import com.tmerclub.cloud.order.mapper.PurchaseOrderItemLangMapper;
import com.tmerclub.cloud.order.model.PurchaseOrderItem;
import com.tmerclub.cloud.order.model.PurchaseOrderItemLang;
import com.tmerclub.cloud.order.service.PurchaseOrderItemLangService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 订单项-国际化
 *
 * @author lhd
 * @date 2022-06-27 17:49:07
 */
@Service
public class PurchaseOrderItemLangServiceImpl implements PurchaseOrderItemLangService {

    @Autowired
    private PurchaseOrderItemLangMapper purchaseOrderItemLangMapper;

    @Override
    public PageVO<PurchaseOrderItemLang> page(PageDTO pageDTO) {
        return PageUtil.doPage(pageDTO, () -> purchaseOrderItemLangMapper.list());
    }

    @Override
    public PurchaseOrderItemLang getByPurchaseItemId(Long purchaseItemId) {
        return purchaseOrderItemLangMapper.getByPurchaseItemId(purchaseItemId);
    }

    @Override
    public void save(PurchaseOrderItemLang purchaseOrderItemLang) {
        purchaseOrderItemLangMapper.save(purchaseOrderItemLang);
    }

    @Override
    public void update(PurchaseOrderItemLang purchaseOrderItemLang) {
        purchaseOrderItemLangMapper.update(purchaseOrderItemLang);
    }

    @Override
    public void deleteById(Long purchaseItemId) {
        purchaseOrderItemLangMapper.deleteById(purchaseItemId);
    }

    @Override
    public void saveOrderItemLang(List<PurchaseOrderItem> purchaseOrderItems) {
        List<PurchaseOrderItemLang> orderItemLangList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (PurchaseOrderItem orderItem : purchaseOrderItems) {
            for (PurchaseOrderItemLang orderItemLang : orderItem.getPurchaseOrderItemLangList()) {
                // sharing 需要单独字段做主键，暂只保存中文数据，避免插入key重复
                if (Objects.equals(orderItemLang.getLang(), LanguageEnum.LANGUAGE_ZH_CN.getLang())) {
                    orderItemLang.setPurchaseItemId(orderItem.getPurchaseItemId());
                    orderItemLangList.add(orderItemLang);
                }
            }
        }
        purchaseOrderItemLangMapper.saveBatch(orderItemLangList);
    }


}
