package com.hys.app.service.erp.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hys.app.converter.erp.WarehouseConverter;
import com.hys.app.framework.database.WebPage;
import com.hys.app.framework.exception.ServiceException;
import com.hys.app.framework.database.mybatisplus.base.BaseServiceImpl;
import com.hys.app.mapper.erp.WarehouseMapper;
import com.hys.app.mapper.goods.GoodsMapper;
import com.hys.app.model.erp.dos.GoodsDO;
import com.hys.app.model.erp.dos.ProductStockDO;
import com.hys.app.model.erp.dos.WarehouseDO;
import com.hys.app.model.erp.dto.WarehouseDTO;
import com.hys.app.model.erp.dto.WarehouseQueryParams;
import com.hys.app.model.erp.vo.WarehouseVO;
import com.hys.app.model.system.dos.DeptDO;
import com.hys.app.service.erp.*;
import com.hys.app.service.system.AdminUserManager;
import com.hys.app.service.system.DeptManager;
import com.hys.app.util.ValidateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.hys.app.framework.util.CollectionUtils.convertList;

/**
 * 仓库业务层实现
 *
 * @author 张崧
 * 2023-11-30 16:35:16
 */
@Service
public class WarehouseManagerImpl extends BaseServiceImpl<WarehouseMapper, WarehouseDO> implements WarehouseManager {

    @Autowired
    private WarehouseConverter converter;

    @Autowired
    private ProductStockManager productStockManager;

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public WebPage<WarehouseVO> list(WarehouseQueryParams queryParams) {
        WebPage<WarehouseDO> warehouseDOWebPage = baseMapper.selectPage(queryParams);
        return converter.convert(warehouseDOWebPage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(WarehouseDTO warehouseDTO) {
        check(warehouseDTO);
        WarehouseDO warehouseDO = converter.convert(warehouseDTO);
        save(warehouseDO);
        productStockManager.initStockByWarehouse(warehouseDO.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(WarehouseDTO warehouseDTO) {
        check(warehouseDTO);
        updateById(converter.convert(warehouseDTO));
    }

    @Override
    public WarehouseVO getDetail(Long id) {
        return converter.convert(getById(id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        //查询所有上架的商品
        List<GoodsDO> list = goodsMapper.selectList(new LambdaQueryWrapper<GoodsDO>().eq(GoodsDO::getMarketEnable, true));
        List<Long> collect = list.stream().map(GoodsDO::getId).collect(Collectors.toList());

        List<ProductStockDO> doList = productStockManager.list(new LambdaQueryWrapper<ProductStockDO>()
                .eq(ProductStockDO::getWarehouseId, id)
                .in(ProductStockDO::getGoodsId, collect).gt(ProductStockDO::getUsableStock, 0));
        if (CollUtil.isNotEmpty(doList)) {
            throw new ServiceException("该仓库已关联入库单，不能进行删除操作");
        }
        removeById(id);
        productStockManager.deleteStockByWarehouse(id);
    }

    @Override
    public List<WarehouseVO> listAll(Long deptId) {
        List<WarehouseDO> list = baseMapper.selectList();
        return converter.convert(list);
    }

    private void check(WarehouseDTO warehouseDTO) {
        LambdaQueryWrapper<WarehouseDO> wrapper = new LambdaQueryWrapper<WarehouseDO>().ne(ObjectUtil.isNotNull(warehouseDTO.getId()), WarehouseDO::getId, warehouseDTO.getId()).eq(WarehouseDO::getSn, warehouseDTO.getSn());
        if (count(wrapper) > 1) {
            throw new ServiceException("仓库编号已存在");
        }
    }
}

