package com.fjhx.service.stock.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.entity.stock.StockJournal;
import com.fjhx.mapper.stock.StockJournalMapper;
import com.fjhx.params.stock.StockJournalVo;
import com.fjhx.service.stock.StockJournalService;
import com.fjhx.utils.UserClientUtil;
import com.fjhx.utils.wrapperUtil.IWrapper;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 出入库流水记录 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-11-24
 */
@Service
public class StockJournalServiceImpl extends ServiceImpl<StockJournalMapper, StockJournal> implements StockJournalService {

    @Override
    public Page<StockJournal> getPage(Map<String, String> condition) {

        IWrapper<StockJournal> wrapper = IWrapper.getWrapper(condition);
        wrapper.eq("t1", StockJournal::getWarehouseId)
                .eq("t1", StockJournal::getType)
                .eq("t1", StockJournal::getDetailsType)
                .ge("t1", StockJournal::getCreateTime, condition.get("startTime"))
                .le("t1", StockJournal::getCreateTime, condition.get("endTime"))
                .apply(Func.isNotEmpty(condition.get("logisticsInfoCode")), "instr(t3.logistics_info_code, '" + condition.get("logisticsInfoCode") + "') > 0")
                .orderByDesc("t1", StockJournal::getId);

        Page<StockJournal> page = baseMapper.getPage(createPage(condition), wrapper);
        if (Func.isNotEmpty(page.getRecords())) {
            //操作人名称map
            Map<Long, String> userNameMap = UserClientUtil.getUserNameMap(page.getRecords(), StockJournal::getCreateUser);

            for (StockJournal record : page.getRecords()) {
                if (Func.isNotEmpty(userNameMap) && Func.isNotEmpty(userNameMap.get(record.getCreateUser()))) {
                    record.setCreateName(userNameMap.get(record.getCreateUser()));
                }
            }
        }
        return page;
    }

    @Override
    public void add(StockJournalVo stockJournalVo) {
        save(stockJournalVo);
    }

    @Override
    public void edit(StockJournalVo stockJournalVo) {
        updateById(stockJournalVo);
    }

    @Override
    public void delete(StockJournalVo stockJournalVo) {
        removeById(stockJournalVo.getId());
    }

    @Override
    public void add(Integer type, Integer detailsType, Long goodsId, Long warehouseId, BigDecimal changeQuantity, String remarks) {
        StockJournal stockJournal = new StockJournal();
        stockJournal.setType(type);
        stockJournal.setDetailsType(detailsType);
        stockJournal.setGoodsId(goodsId);
        stockJournal.setWarehouseId(warehouseId);
        stockJournal.setChangeQuantity(changeQuantity);
        stockJournal.setActualQuantity(new BigDecimal(BigDecimal.ZERO.intValue()));
        stockJournal.setRemarks(remarks);
        save(stockJournal);
    }

}
