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

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.I18nMessage;
import com.tmerclub.cloud.product.dto.StockChangeReasonDTO;
import com.tmerclub.cloud.product.mapper.StockChangeReasonLangMapper;
import com.tmerclub.cloud.product.model.StockChangeReasonLang;
import com.tmerclub.cloud.product.service.StockChangeReasonLangService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 出入库原因-国际化
 *
 * @author lhd
 * @date 2022-06-22 16:57:27
 */
@Service
public class StockChangeReasonLangServiceImpl implements StockChangeReasonLangService {

    @Autowired
    private StockChangeReasonLangMapper stockChangeReasonLangMapper;

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

    @Override
    public StockChangeReasonLang getByStockChangeReasonId(Long stockChangeReasonId) {
        List<StockChangeReasonLang> reasonLangs = stockChangeReasonLangMapper.getByStockChangeReasonId(stockChangeReasonId);
        // 中0，英1，与LanguageEnum字段不一致，需额外减1
        Integer lang = I18nMessage.getLang() - 1;
        for (StockChangeReasonLang reasonLang : reasonLangs) {
            if (Objects.equals(lang, reasonLang.getLang())) {
                return reasonLang;
            }
        }
        return null;
    }

    @Override
    public void save(StockChangeReasonLang stockChangeReasonLang) {
        stockChangeReasonLangMapper.save(stockChangeReasonLang);
    }

    @Override
    public void update(StockChangeReasonLang stockChangeReasonLang) {
        stockChangeReasonLangMapper.update(stockChangeReasonLang);
    }

    @Override
    public void deleteById(Long stockChangeReasonId) {
        stockChangeReasonLangMapper.deleteById(stockChangeReasonId);
    }

    @Override
    public Integer countReason(StockChangeReasonDTO stockChangeReasonDTO) {
        return stockChangeReasonLangMapper.countReason(stockChangeReasonDTO);
    }
}
