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

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.context.user.AdminUserContext;
import com.hys.app.framework.database.WebPage;
import com.hys.app.framework.database.mybatisplus.base.BaseServiceImpl;
import com.hys.app.framework.exception.ServiceException;
import com.hys.app.mapper.erp.WarehouseMapper;
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.service.erp.ProductStockManager;
import com.hys.app.service.erp.WarehouseManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 仓库业务层实现
 *
 * @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;

    @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);
        warehouseDO.setCompanyId(AdminUserContext.getCompanyId());
        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) {
        removeById(id);
//        productStockManager.deleteStockByWarehouse(id);
    }

    @Override
    public List<WarehouseVO> listAll(Long deptId) {
        List<WarehouseDO> list = baseMapper.selectList(new LambdaQueryWrapper<WarehouseDO>()
                .eq(ObjectUtil.isNotNull(AdminUserContext.getCompanyId()), WarehouseDO::getCompanyId, AdminUserContext.getCompanyId()));
        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("仓库编号已存在");
        }
    }
}

