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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hys.app.converter.erp.WarehouseEntryProductConverter;
import com.hys.app.framework.database.WebPage;
import com.hys.app.framework.database.mybatisplus.base.BaseServiceImpl;
import com.hys.app.framework.database.mybatisplus.wrapper.QueryWrapperX;
import com.hys.app.mapper.erp.ProductStockMapper;
import com.hys.app.mapper.erp.WarehouseEntryProductMapper;
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.WarehouseEntryProductDO;
import com.hys.app.model.erp.dto.WareQueryGoodParams;
import com.hys.app.model.erp.dto.WarehouseEntryProductDTO;
import com.hys.app.model.erp.dto.WarehouseEntryProductQueryParams;
import com.hys.app.model.erp.dto.WarehouseInQueryParams;
import com.hys.app.model.erp.vo.GoodsVO;
import com.hys.app.model.erp.vo.WarehouseEntryItemV1;
import com.hys.app.model.erp.vo.WarehouseEntryProductVO;
import com.hys.app.service.erp.ProductStockManager;
import com.hys.app.service.erp.WarehouseEntryProductManager;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 入库单商品明细业务层实现
 *
 * @author 张崧
 * @since 2023-12-05 15:30:10
 */
@Service
public class WarehouseEntryProductManagerImpl extends BaseServiceImpl<WarehouseEntryProductMapper, WarehouseEntryProductDO> implements WarehouseEntryProductManager {

    @Autowired
    private WarehouseEntryProductConverter converter;

    @Autowired
    private ProductStockManager productStockManager;

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public WebPage<WarehouseEntryProductVO> list(WarehouseEntryProductQueryParams queryParams) {
        WebPage<WarehouseEntryProductDO> webPage = baseMapper.selectPage(queryParams);
        return converter.convert(webPage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(WarehouseEntryProductDTO warehouseEntryProductDTO) {
        save(converter.convert(warehouseEntryProductDTO));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<WarehouseEntryItemV1> selectList(WarehouseInQueryParams request) {
        return baseMapper.queryList(request);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long selectCount(WarehouseInQueryParams request) {
        return baseMapper.selectCount(request);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(WarehouseEntryProductDTO warehouseEntryProductDTO) {
        updateById(converter.convert(warehouseEntryProductDTO));
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        removeById(id);
    }

    @Override
    public void deleteByWarehouseEntryId(Long warehouseEntryId) {
        lambdaUpdate().eq(WarehouseEntryProductDO::getWarehouseEntryId, warehouseEntryId).remove();
    }

    @Override
    public void deleteByWarehouseEntryIds(List<Long> warehouseEntryIds) {
        lambdaUpdate().in(WarehouseEntryProductDO::getWarehouseEntryId, warehouseEntryIds).remove();
    }

    @Override
    public List<WarehouseEntryProductDO> listByWarehouseEntryId(Long warehouseEntryId) {
        return lambdaQuery().eq(WarehouseEntryProductDO::getWarehouseEntryId, warehouseEntryId).list();
    }

    @Override
    public List<WarehouseEntryProductDO> listByWarehouseEntryIds(List<Long> warehouseEntryIds) {
        return lambdaQuery()
                .in(WarehouseEntryProductDO::getWarehouseEntryId, warehouseEntryIds)
                .list();
    }

    @Override
    public void updateReturnNum(Long id, Integer returnNum) {
        lambdaUpdate()
                .setSql("return_num = return_num + " + returnNum)
                .eq(WarehouseEntryProductDO::getId, id)
                .update();
    }
}

