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

import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.constant.StatusEnum;
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.exception.LuckException;
import com.tmerclub.cloud.common.i18n.I18nMessage;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.product.dto.StockChangeReasonDTO;
import com.tmerclub.cloud.product.mapper.StockChangeReasonLangMapper;
import com.tmerclub.cloud.product.mapper.StockChangeReasonMapper;
import com.tmerclub.cloud.product.model.StockChangeReason;
import com.tmerclub.cloud.product.model.StockChangeReasonLang;
import com.tmerclub.cloud.product.model.StockChangeReasonStatus;
import com.tmerclub.cloud.product.service.StockChangeReasonLangService;
import com.tmerclub.cloud.product.service.StockChangeReasonService;
import com.tmerclub.cloud.product.service.StockChangeReasonStatusService;
import com.tmerclub.cloud.product.vo.StockChangeReasonVO;
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.Objects;

/**
 * 出入库原因
 *
 * @author lhd
 * @date 2022-06-22 16:57:27
 */
@Service
public class StockChangeReasonServiceImpl implements StockChangeReasonService {

    @Autowired
    private StockChangeReasonMapper stockChangeReasonMapper;

    @Autowired
    private StockChangeReasonLangMapper stockChangeReasonLangMapper;


    @Autowired
    private StockChangeReasonLangService stockChangeReasonLangService;

    @Autowired
    private StockChangeReasonStatusService stockChangeReasonStatusService;

    @Override
    public PageVO<StockChangeReasonVO> page(PageDTO pageDTO, StockChangeReasonDTO stockChangeReasonDTO) {
        return PageUtil.doPage(pageDTO, () -> stockChangeReasonMapper.pageByParams(stockChangeReasonDTO));
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(StockChangeReasonDTO stockChangeReasonDTO) {
        stockChangeReasonDTO.setShopId(AuthUserContext.get().getTenantId());
        stockChangeReasonDTO.setSysType(AuthUserContext.get().getSysType());
        // 校验信息
        this.checkInfo(stockChangeReasonDTO, true);
        // 设置默认值
        stockChangeReasonDTO.setStatus(StatusEnum.ENABLE.value());
        // 保存出入库原因
        StockChangeReason stockChangeReason = BeanUtil.map(stockChangeReasonDTO, StockChangeReason.class);
        stockChangeReason.setStatus(StatusEnum.ENABLE.value());
        stockChangeReasonMapper.save(stockChangeReason);
        StockChangeReasonLang stockChangeReasonLang = BeanUtil.map(stockChangeReasonDTO, StockChangeReasonLang.class);
        stockChangeReasonLang.setStockChangeReasonId(stockChangeReason.getStockChangeReasonId());
        stockChangeReasonLang.setLang(0);
        stockChangeReasonLangMapper.save(stockChangeReasonLang);
    }

    @Override
    public void update(StockChangeReason stockChangeReason) {
        StockChangeReasonDTO stockChangeReasonDTO = BeanUtil.map(stockChangeReason, StockChangeReasonDTO.class);
        // 校验信息
        this.checkInfo(stockChangeReasonDTO, false);
        stockChangeReasonMapper.update(stockChangeReason);
    }

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

    @Override
    public void changeStatus(Long shopId, Long stockChangeReasonId, Integer status) {
        StockChangeReason stockChangeReason = stockChangeReasonMapper.getByStockChangeReasonId(stockChangeReasonId);
        if (Objects.equals(stockChangeReason.getShopId(), Constant.PLATFORM_SHOP_ID) && !Objects.equals(shopId, Constant.PLATFORM_SHOP_ID)) {
            // 商家端改变系统内置的出入库原因状态
            if (Objects.equals(status, StatusEnum.DELETE.value())) {
                //系统内置出入库原因不能删除
                throw new LuckException("系统内置出入库原因不能删");
            }
            // 新增or更新系统出入库原因状态
            StockChangeReasonStatus reasonStatus = stockChangeReasonStatusService.getStatusByShopIdAndReasonId(shopId, stockChangeReasonId);
            boolean isSave = Objects.isNull(reasonStatus);
            if (isSave) {
                reasonStatus = new StockChangeReasonStatus();
                reasonStatus.setShopId(shopId);
                reasonStatus.setStockChangeReasonId(stockChangeReasonId);
                reasonStatus.setStatus(status);
                stockChangeReasonStatusService.save(reasonStatus);
            } else {
                reasonStatus.setStatus(status);
                stockChangeReasonStatusService.update(reasonStatus);
            }
        } else {
            // 非系统出入库原因，直接修改出入库原因
            stockChangeReason.setStatus(status);
            stockChangeReasonMapper.update(stockChangeReason);
        }
    }

    @Override
    public List<StockChangeReasonVO> list(StockChangeReasonDTO stockChangeReasonDTO) {
        return stockChangeReasonMapper.getByShopId(stockChangeReasonDTO);
    }

    /**
     * 校验信息
     * @param stockChangeReasonDTO 出入库原因
     * @param saveOrUpdate   true: 新增时校验  false: 更新时校验
     */
    private void checkInfo(StockChangeReasonDTO stockChangeReasonDTO, Boolean saveOrUpdate) {
        if (!saveOrUpdate && Objects.isNull(stockChangeReasonDTO.getStockChangeReasonId())) {
            throw new LuckException("id不能为空");
        }
        int count = stockChangeReasonLangService.countReason(stockChangeReasonDTO);
        if (count > 0) {
            throw new LuckException("出入库原因不能重复！");
        }

    }
}
