package com.kxs.store.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.common.security.util.SecurityUtils;
import com.kxs.store.api.dto.kxsapp.store.StoreSearchListDTO;
import com.kxs.store.api.model.*;
import com.kxs.store.api.vo.kxsapp.store.StoreBrandListVO;
import com.kxs.store.api.vo.kxsapp.store.StoreDataVO;
import com.kxs.store.api.vo.kxsapp.store.StoreListVO;
import com.kxs.store.api.vo.kxsapp.store.StoreSearchListVO;
import com.kxs.store.biz.constant.enums.MachineOrderEnum;
import com.kxs.store.biz.constant.enums.MachineOrderStatusEnum;
import com.kxs.store.biz.mapper.*;
import com.kxs.store.biz.service.KxsWarehouseService;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.units.qual.K;
import org.springframework.stereotype.Service;

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

/**
 * 客小爽仓库表(KxsWarehouse)表服务实现类
 *
 * @author 系统
 * @since 2024-01-08 09:46:01
 */
@Service("kxsWarehouseService")
@RequiredArgsConstructor
public class KxsWarehouseServiceImpl extends ServiceImpl<KxsWarehouseMapper, KxsWarehouse> implements KxsWarehouseService {

    private final KxsMachineApplyMapper kxsMachineApplyMapper;

    private final KxsWarehouseStockMapper kxsWarehouseStockMapper;

    private final KxsMachineAdvanceMapper kxsMachineAdvanceMapper;

    private final KxsMachineRecycleMapper kxsMachineRecycleMapper;
    private final KxsWarehouseMapper kxsWarehouseMapper;

    @Override
    public IPage<StoreSearchListVO> searchList(Page<StoreSearchListVO> page, StoreSearchListDTO param) {

        return baseMapper.searchList(page, param);
    }

    @Override
    public List<StoreListVO> userStoreList() {
        Long userId = SecurityUtils.getUser().getId();

        return baseMapper.userStoreList(userId);
    }

    @Override
    public StoreDataVO storeData(Integer warehouseId) {

        StoreDataVO storeDataVO = new StoreDataVO();
        //待发货
        Long unSendCount = kxsMachineApplyMapper.selectCount(Wrappers.<KxsMachineApply>lambdaQuery()
                .eq(KxsMachineApply::getStatus, MachineOrderStatusEnum.UNDER_REVIEW.getType())
                .eq(KxsMachineApply::getWarehouseId, warehouseId));
        storeDataVO.setUnSendCount(unSendCount.intValue());

        //库存数
        KxsWarehouseStock warehouseStock = kxsWarehouseStockMapper.selectOne(Wrappers.<KxsWarehouseStock>lambdaQuery()
                .eq(KxsWarehouseStock::getWarehouseId, warehouseId));
        storeDataVO.setStockNum(warehouseStock.getStockNum());

        //仓库预发机数量
        Long preMachineCount = kxsMachineAdvanceMapper.selectWarehouseUserCount(warehouseId);
        storeDataVO.setPreMachineCount(preMachineCount.intValue());

        //待回收机具数量
        KxsWarehouse warehouse = baseMapper.selectById(warehouseId);
        Integer recycleCount = kxsMachineRecycleMapper.storeRecoverableData(warehouse.getUserId(), 0);
        storeDataVO.setRecycleCount(recycleCount);

        return storeDataVO;
    }


}

