package vip.xiaonuo.warehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.erp.entity.ErpBdRawMaterial;
import vip.xiaonuo.erp.mapper.ErpBdRawMaterialMapper;
import vip.xiaonuo.warehouse.entity.WarehouseMaterialInStock;
import vip.xiaonuo.warehouse.entity.WarehouseMaterialOutStock;
import vip.xiaonuo.warehouse.entity.WarehouseMaterialStock;
import vip.xiaonuo.warehouse.entity.vo.WarehouseMaterialStockVo;
import vip.xiaonuo.warehouse.mapper.WarehouseMaterialInStockMapper;
import vip.xiaonuo.warehouse.mapper.WarehouseMaterialOutStockMapper;
import vip.xiaonuo.warehouse.mapper.WarehouseMaterialStockMapper;
import vip.xiaonuo.warehouse.param.WarehouseMaterialStockParam;
import vip.xiaonuo.warehouse.param.WarehouseMaterialTailParam;
import vip.xiaonuo.warehouse.param.WarehouseStockParam;
import vip.xiaonuo.warehouse.service.IWarehouseMaterialStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 仓库原材料库存 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-17
 */
@Service
public class WarehouseMaterialStockServiceImpl extends ServiceImpl<WarehouseMaterialStockMapper, WarehouseMaterialStock> implements IWarehouseMaterialStockService {

    @Autowired
    private WarehouseMaterialStockMapper stockMapper;
    @Autowired
    private WarehouseMaterialOutStockMapper outStockMapper;
    @Autowired
    private ErpBdRawMaterialMapper rawMaterialMapper;
    @Autowired
    private WarehouseMaterialInStockMapper inStockMapper;

    @Override
    public List<WarehouseMaterialStockVo> listMaterial(WarehouseMaterialStockParam stockParam) {
        String materialId = stockParam.getFnumber();
        String orgId = stockParam.getOrgId();
        QueryWrapper<WarehouseMaterialStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq( "fnumber", materialId);
        queryWrapper.eq( "org_id", orgId);
        queryWrapper.groupBy(" DATE_FORMAT(create_time, '%Y-%m')");
        queryWrapper.orderByAsc("create_time");
        List<WarehouseMaterialStock> stocks = stockMapper.selectList(queryWrapper);
        List<WarehouseMaterialStockVo> list = new ArrayList<>();
        if (stocks.size() != 0) {
            for (WarehouseMaterialStock stock : stocks) {
                WarehouseMaterialStockVo vo = new WarehouseMaterialStockVo();
                vo.setCreateTime(stock.getCreateTime());
                vo.setMaterial(stock.getMaterial());
                vo.setFnumber(stock.getFnumber());
                vo.setMaterialStock(stock.getMaterialStock());
                list.add(vo);
            }
        }
        return list;
    }

    @Override
    public List<WarehouseMaterialOutStock> listMaterialOut(WarehouseMaterialStockParam stockParam) {
        String materialId = stockParam.getFnumber();
        String orgId = stockParam.getOrgId();
        QueryWrapper<WarehouseMaterialOutStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq( "fnumber", materialId);
        queryWrapper.eq( "org_id", orgId);
        queryWrapper.groupBy(" DATE_FORMAT(create_time, '%Y-%m')");
        queryWrapper.orderByAsc("create_time");
        List<WarehouseMaterialOutStock> stocks = outStockMapper.selectList(queryWrapper);
        return stocks;
    }

    @Override
    public List<ErpBdRawMaterial> listRawMaterial(WarehouseStockParam stockParam) {
        String orgId = stockParam.getOrgId();
        QueryWrapper<ErpBdRawMaterial> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_id", orgId);
        queryWrapper.groupBy("fname");
        return rawMaterialMapper.selectList(queryWrapper);
    }

    @Override
    public Page<WarehouseMaterialOutStock> outMaterial(WarehouseMaterialTailParam materialTailParam) {
        String orgId = materialTailParam.getOrgId();
        String materialType = materialTailParam.getMaterialType();
        Integer pageNum = materialTailParam.getPageNum();
        Integer pageSize = materialTailParam.getPageSize();
        Page<WarehouseMaterialOutStock> page = new Page<>(pageNum, pageSize);
        QueryWrapper<WarehouseMaterialOutStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq( "org_id", orgId);
        queryWrapper.eq( "material_type", materialType);
        queryWrapper.orderByAsc("create_time");
        return outStockMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Page<WarehouseMaterialInStock> inMaterial(WarehouseMaterialTailParam materialTailParam) {
        String orgId = materialTailParam.getOrgId();
        String materialType = materialTailParam.getMaterialType();
        Integer pageNum = materialTailParam.getPageNum();
        Integer pageSize = materialTailParam.getPageSize();
        Page<WarehouseMaterialInStock> page = new Page<>(pageNum, pageSize);
        QueryWrapper<WarehouseMaterialInStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq( "org_id", orgId);
        queryWrapper.eq( "material_type", materialType);
        queryWrapper.orderByAsc("create_time");
        return inStockMapper.selectPage(page,queryWrapper);
    }

}
