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

import cn.hutool.core.util.StrUtil;
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.core.constant.SecurityConstants;
import com.kxs.common.core.constant.enums.ErrorTypeEnum;
import com.kxs.common.core.exception.GlobalCustomerException;
import com.kxs.common.core.util.R;
import com.kxs.common.core.util.RetOps;
import com.kxs.common.security.util.SecurityUtils;
import com.kxs.product.api.feign.RemoteKxsProductService;
import com.kxs.store.api.dto.kxsapp.store.StoreSearchListDTO;
import com.kxs.store.api.model.*;
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.MachineOrderStatusEnum;
import com.kxs.store.biz.mapper.*;
import com.kxs.store.biz.service.KxsWarehouseService;
import com.kxs.user.api.feign.RemoteKxsUserService;
import com.kxs.user.api.model.KxsUser;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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;

    private final RemoteKxsUserService remoteKxsUserService;
    private final RemoteKxsProductService remoteKxsProductService;

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

        Long userId = SecurityUtils.getUser().getId();
        R<KxsUser> kxsUserR = remoteKxsUserService.loadUserById(userId, SecurityConstants.FROM_IN);
        KxsUser user = RetOps.of(kxsUserR)
                .getData()
                .orElseThrow(() -> new GlobalCustomerException(ErrorTypeEnum.USER_NOT_FOUND.getDescription()));
        //包含自身的
        String[] pidPaths = (user.getPidPath() + userId).split(StrUtil.COMMA);
        param.setWhsUserIds(pidPaths);

        return baseMapper.searchList(page, param);
    }

    @Override
    public List<StoreListVO> userStoreList(Integer reqType) {
        Long userId = SecurityUtils.getUser().getId();
        List<StoreListVO> storeListVOS = baseMapper.userStoreList(userId);
        for (StoreListVO storeListVO : storeListVOS) {
            if(reqType == 0){
                //待发货
                Long unSendCount = kxsMachineApplyMapper.selectCount(Wrappers.<KxsMachineApply>lambdaQuery()
                        .eq(KxsMachineApply::getStatus, MachineOrderStatusEnum.UNDER_REVIEW.getType())
                        .eq(KxsMachineApply::getWarehouseId, storeListVO.getId()));
                storeListVO.setUnfilledTotal(unSendCount.intValue());
            }
            if(reqType == 1){
                //查询仓库库存
                KxsWarehouseStock warehouseStock = kxsWarehouseStockMapper.selectOne(Wrappers.<KxsWarehouseStock>lambdaQuery()
                        .eq(KxsWarehouseStock::getWarehouseId, storeListVO.getId()));
                storeListVO.setStockNum(warehouseStock.getStockNum());
                //查询预发机数量
                Long preMachineCount = kxsMachineAdvanceMapper.selectWarehouseUserCount(storeListVO.getId());
                storeListVO.setPrePosNum(preMachineCount.intValue());
            }
        }
        return storeListVOS;
    }

    @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());

        //库存数
        R<Integer> res = remoteKxsProductService.loadWhsStockNum(warehouseId, SecurityConstants.FROM_IN);
        storeDataVO.setStockNum(res.getData());

        //仓库预发机数量
        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;
    }


}

