package com.shzg.project.worn.service.impl;

import java.util.List;
import com.shzg.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shzg.project.worn.mapper.WornMaterialMapper;
import com.shzg.project.worn.domain.WornMaterial;
import com.shzg.project.worn.service.IWornMaterialService;

/**
 * 物料Service业务层处理
 * 
 * @author shzg
 * @date 2026-03-11
 */
@Service
public class WornMaterialServiceImpl implements IWornMaterialService 
{
    @Autowired
    private WornMaterialMapper wornMaterialMapper;

    /**
     * 查询物料
     * 
     * @param id 物料主键
     * @return 物料
     */
    @Override
    public WornMaterial selectWornMaterialById(Long id)
    {
        return wornMaterialMapper.selectWornMaterialById(id);
    }

    /**
     * 查询物料列表
     * 
     * @param wornMaterial 物料
     * @return 物料
     */
    @Override
    public List<WornMaterial> selectWornMaterialList(WornMaterial wornMaterial)
    {
        return wornMaterialMapper.selectWornMaterialList(wornMaterial);
    }

    /**
     * 新增物料
     * 
     * @param wornMaterial 物料
     * @return 结果
     */
    @Override
    public int insertWornMaterial(WornMaterial wornMaterial)
    {
        wornMaterial.setCreateTime(DateUtils.getNowDate());

        // 生成物料编码
        String maxCode = wornMaterialMapper.selectMaxMaterialCode();

        String newCode;

        if (maxCode == null)
        {
            newCode = "F0001";
        }
        else
        {
            int num = Integer.parseInt(maxCode.substring(1));
            num++;

            newCode = "F" + String.format("%04d", num);
        }

        wornMaterial.setMaterialCode(newCode);

        return wornMaterialMapper.insertWornMaterial(wornMaterial);
    }

    /**
     * 修改物料
     * 
     * @param wornMaterial 物料
     * @return 结果
     */
    @Override
    public int updateWornMaterial(WornMaterial wornMaterial)
    {
        wornMaterial.setUpdateTime(DateUtils.getNowDate());
        return wornMaterialMapper.updateWornMaterial(wornMaterial);
    }

    /**
     * 批量删除物料
     * 
     * @param ids 需要删除的物料主键
     * @return 结果
     */
    @Override
    public int deleteWornMaterialByIds(Long[] ids)
    {
        return wornMaterialMapper.deleteWornMaterialByIds(ids);
    }

    /**
     * 删除物料信息
     * 
     * @param id 物料主键
     * @return 结果
     */
    @Override
    public int deleteWornMaterialById(Long id)
    {
        return wornMaterialMapper.deleteWornMaterialById(id);
    }
}
