package com.fjhx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.mapper.StockExMapper;
import com.fjhx.service.StockExService;
import com.fjhx.utils.PageUtil;
import com.fjhx.utils.UserClientUtil;
import com.fjhx.utils.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class StockExServiceImpl implements StockExService {

    @Autowired
    private StockExMapper stockExMapper;

    @Override
    public Page<Map<String, Object>> getInOutPage(Map<String, String> condition) {

        String keyword = condition.get("keyword");
        String goodsId = condition.get("goodsId");

        QueryWrapper<?> wrapper = WrapperUtil.init(condition)
                .eq("sj.type", "type")
                .eq("sj.warehouse_id", "warehouseId")
                .eq("sj.details_type", "detailsType")
                .periodOfTime("sj.create_time")
                .createTimeDesc("sj")
                .getWrapper();

        Page<Map<String, Object>> inOutPage = stockExMapper.getInOutPage(PageUtil.createPageMap(condition), wrapper, keyword, goodsId);

        List<Map<String, Object>> records = inOutPage.getRecords();

        if (records.size() == 0) {
            return inOutPage;
        }

        // 赋值操作人名称
        UserClientUtil.setUserName(records, "createUser", "createUser");

        return inOutPage;
    }

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

        String keyword = condition.get("keyword");
        String goodsId = condition.get("goodsId");

        QueryWrapper<?> wrapper = WrapperUtil.init(condition)
                .eq("s.warehouse_id", "warehouseId")
                .createTimeDesc("s")
                .getWrapper();

        return stockExMapper.getPage(PageUtil.createPageMap(condition), wrapper, keyword, goodsId);
    }

}
