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 cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.goods.ProductMapper;
import com.hys.app.model.erp.dos.ProductDO;
import com.hys.app.model.erp.dos.ProductStockDO;
import com.hys.app.model.erp.dto.*;
import com.hys.app.model.erp.vo.ProductVO;
import com.hys.app.service.erp.ProductManager;
import com.hys.app.service.erp.ProductStockManager;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * 产品业务层实现
 *
 * @author 张崧
 * 2023-11-30 16:06:44
 */
@Service
@AllArgsConstructor
public class ProductManagerImpl extends BaseServiceImpl<ProductMapper, ProductDO> implements ProductManager {

    private ProductStockManager productStockManager;

    @Override
    public IPage<ProductDO> list(ProductQueryParams queryParams) {
        return page(new Page<>(queryParams.getPageNo(), queryParams.getPageSize()), new LambdaQueryWrapper<ProductDO>()
                .eq(ObjectUtil.isNotNull(AdminUserContext.getCompanyId()),ProductDO::getCompanyId,AdminUserContext.getCompanyId())
                .and(StrUtil.isNotBlank(queryParams.getKeyword()),
                        item -> item.like(ProductDO::getName, queryParams.getKeyword())
                                .or()
                                .like(ProductDO::getSku, queryParams.getKeyword())
                                .or()
                                .like(ProductDO::getBrand, queryParams.getKeyword())
                                .or()
                                .like(ProductDO::getBarCode, queryParams.getKeyword())));

    }

    @Override
    public ProductVO getDetail(String id) {
        return null;
    }

    @Override
    public void add(Long goodsId) {
    }


    @Override
    public void updateWarnStock(List<Long> stockId, Integer warnNum) {
        if (warnNum < 0) {
            throw new ServiceException("预警库存不能小于0");
        }
        List<ProductStockDO> list = productStockManager.list(new LambdaQueryWrapper<ProductStockDO>().in(ProductStockDO::getId, stockId));
        list.forEach(item -> item.setWarningStock(warnNum));
        productStockManager.updateBatchById(list);
    }

    @Override
    public void addProduct(ProductDTO productDTO) {
        ProductDO productDO = BeanUtil.copyProperties(productDTO, ProductDO.class);
        checkParam(productDO);
        save(productDO);
        //添加库存
        productStockManager.addProduct(productDO);
    }

    @Override
    public void updateProduct(ProductDTO productDTO, Long id) {
        ProductDO productDO = getById(id);
        if (ObjectUtil.isNull(productDO)) {
            throw new ServiceException("未查询到商品信息");
        }
        BeanUtil.copyProperties(productDTO, productDO);
        checkParam(productDO);
        updateById(productDO);
    }

    @Override
    public void deletedProduct(Long id) {
        List<ProductStockDO> list = productStockManager.list(new LambdaQueryWrapper<ProductStockDO>()
                .eq(ProductStockDO::getCompanyId, AdminUserContext.getCompanyId())
                .eq(ProductStockDO::getProductId, id).gt(ProductStockDO::getActualStock, 0));
        if (CollUtil.isNotEmpty(list)) {
            throw new ServiceException("删除失败，该商品在仓库里面还有库存");
        }
        //删除库存
        productStockManager.remove(new LambdaQueryWrapper<ProductStockDO>().eq(ProductStockDO::getProductId, id));
        removeById(id);
    }

    private void checkParam(ProductDO productDO) {
        ProductDO oneName = getOne(new LambdaQueryWrapper<ProductDO>()
                .eq(ProductDO::getName, productDO.getName())
                .ne(ObjectUtil.isNotNull(productDO.getId()), ProductDO::getId, productDO.getId()));
        if (ObjectUtil.isNotNull(oneName)) {
            throw new ServiceException("商品名称重复");
        }
        ProductDO oneSn = getOne(new LambdaQueryWrapper<ProductDO>()
                .eq(ProductDO::getSnCode, productDO.getSnCode())
                .ne(ObjectUtil.isNotNull(productDO.getId()), ProductDO::getId, productDO.getId()));
        if (ObjectUtil.isNotNull(oneSn)) {
            throw new ServiceException("商品sn码重复");
        }
    }
}


