package com.ruoyi.material.service.impl;

import java.util.List;

import com.ruoyi.material.domain.CMaterial;
import com.ruoyi.material.domain.temp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.material.domain.CMaterialCost;
import com.ruoyi.material.mapper.CMaterialMapper;
import com.ruoyi.material.service.ICMaterialService;

/**
 * 物料管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-02-28
 */
@Service
public class CMaterialServiceImpl implements ICMaterialService
{
    @Autowired
    private CMaterialMapper cMaterialMapper;

    /**
     * 查询物料管理
     *
     * @param materialId 物料管理主键
     * @return 物料管理
     */
    @Override
    public CMaterial selectCMaterialByMaterialId(Long materialId)
    {
        return cMaterialMapper.selectCMaterialByMaterialId(materialId);
    }

    /**
     * 查询物料管理列表
     *
     * @param cMaterial 物料管理
     * @return 物料管理
     */
    @Override
    public List<CMaterial> selectCMaterialList(CMaterial cMaterial)
    {
        return cMaterialMapper.selectCMaterialList(cMaterial);
    }

    /**
     * 新增物料管理
     *
     * @param cMaterial 物料管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCMaterial(CMaterial cMaterial)
    {
        int rows = cMaterialMapper.insertCMaterial(cMaterial);
        insertCMaterialCost(cMaterial);
        return rows;
    }

    /**
     * 修改物料管理
     *
     * @param cMaterial 物料管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCMaterial(CMaterial cMaterial)
    {
        cMaterialMapper.deleteCMaterialCostByCostMaterialId(cMaterial.getMaterialId());
        insertCMaterialCost(cMaterial);
        return cMaterialMapper.updateCMaterial(cMaterial);
    }

    /**
     * 批量删除物料管理
     *
     * @param materialIds 需要删除的物料管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCMaterialByMaterialIds(Long[] materialIds)
    {
        cMaterialMapper.deleteCMaterialCostByCostMaterialIds(materialIds);
        return cMaterialMapper.deleteCMaterialByMaterialIds(materialIds);
    }

    /**
     * 删除物料管理信息
     *
     * @param materialId 物料管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCMaterialByMaterialId(Long materialId)
    {
        cMaterialMapper.deleteCMaterialCostByCostMaterialId(materialId);
        return cMaterialMapper.deleteCMaterialByMaterialId(materialId);
    }

    /**
     * 新增物料成本信息
     *
     * @param cMaterial 物料管理对象
     */
    public void insertCMaterialCost(CMaterial cMaterial)
    {
        List<CMaterialCost> cMaterialCostList = cMaterial.getCMaterialCostList();
        Long materialId = cMaterial.getMaterialId();
        if (StringUtils.isNotNull(cMaterialCostList))
        {
            List<CMaterialCost> list = new ArrayList<CMaterialCost>();
            for (CMaterialCost cMaterialCost : cMaterialCostList)
            {
                cMaterialCost.setCostMaterialId(materialId);
                list.add(cMaterialCost);
            }
            if (list.size() > 0)
            {
                cMaterialMapper.batchCMaterialCost(list);
            }
        }
    }

    /**
     * 拉取临时物料及用量表
     * @return
     */
    @Override
    public List<temp> selectTemp() {
        return cMaterialMapper.selectTemp();
    }
}
