package com.zg.project.wisdom.service.impl;

import java.util.List;
import com.zg.common.utils.DateUtils;
import com.zg.project.wisdom.domain.vo.StockStatisticVO;
import com.zg.project.wisdom.mapper.RkRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zg.project.wisdom.mapper.RkInfoMapper;
import com.zg.project.wisdom.domain.RkInfo;
import com.zg.project.wisdom.service.IRkInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 库存单据明细Service业务层处理
 * 
 * @author zg
 * @date 2026-01-20
 */
@Service
public class RkInfoServiceImpl implements IRkInfoService 
{
    @Autowired
    private RkInfoMapper rkInfoMapper;

    @Autowired
    private RkRecordMapper rkRecordMapper;

    /**
     * 查询库存单据明细
     * 
     * @param id 库存单据明细主键
     * @return 库存单据明细
     */
    @Override
    public RkInfo selectRkInfoById(Long id)
    {
        return rkInfoMapper.selectRkInfoById(id);
    }

    /**
     * 查询库存单据明细列表
     * 
     * @param rkInfo 库存单据明细
     * @return 库存单据明细
     */
    @Override
    public List<RkInfo> selectRkInfoList(RkInfo rkInfo)
    {
        return rkInfoMapper.selectRkInfoList(rkInfo);
    }

    /**
     * 新增库存单据明细
     * 
     * @param rkInfo 库存单据明细
     * @return 结果
     */
    @Override
    public int insertRkInfo(RkInfo rkInfo)
    {
        rkInfo.setCreateTime(DateUtils.getNowDate());
        return rkInfoMapper.insertRkInfo(rkInfo);
    }

    /**
     * 修改库存单据明细
     * 
     * @param rkInfo 库存单据明细
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateRkInfo(RkInfo rkInfo)
    {
        rkInfo.setUpdateTime(DateUtils.getNowDate());

        // ① 更新 rk_info
        int rows = rkInfoMapper.updateRkInfo(rkInfo);

        // ② 同步更新 rk_record（按 rk_info_id）
        rkRecordMapper.updateByRkInfo(rkInfo);

        return rows;
    }
    /**
     * 批量删除库存单据明细
     * 
     * @param ids 需要删除的库存单据明细主键
     * @return 结果
     */
    @Override
    public int deleteRkInfoByIds(Long[] ids)
    {
        return rkInfoMapper.deleteRkInfoByIds(ids);
    }

    /**
     * 删除库存单据明细信息
     * 
     * @param id 库存单据明细主键
     * @return 结果
     */
    @Override
    public int deleteRkInfoById(Long id)
    {
        return rkInfoMapper.deleteRkInfoById(id);
    }


    @Override
    public StockStatisticVO getStockStatistic(RkInfo query) {
        return rkInfoMapper.selectStockStatisticByCondition(query);
    }
}
