package com.ruoyi.app.service.impl;

import java.util.List;
import java.util.OptionalLong;

import com.ruoyi.app.DTO.PigpenDTO;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.app.mapper.PigpenMapper;
import com.ruoyi.app.model.Pigpen;
import com.ruoyi.app.service.IPigpenService;
import org.springframework.util.ObjectUtils;

/**
 * 待宰圈Service业务层处理
 * 
 * @author coede
 * @date 2025-03-19
 */
@Service
public class PigpenServiceImpl implements IPigpenService 
{
    @Autowired
    private PigpenMapper pigpenMapper;

    /**
     * 查询待宰圈
     * 
     * @param id 待宰圈主键
     * @return 待宰圈
     */
    @Override
    public Pigpen selectPigpenById(Long id)
    {
        return pigpenMapper.selectPigpenById(id);
    }

    /**
     * 查询待宰圈列表
     * 
     * @param pigpen 待宰圈
     * @return 待宰圈
     */
    @Override
    public List<Pigpen> selectPigpenList(Pigpen pigpen)
    {
        return pigpenMapper.selectPigpenList(pigpen);
    }
    @Override
    public List<PigpenDTO> printPigpenList(Pigpen pigpen)
    {
        return pigpenMapper.printPigpenList(pigpen);
    }

    /**
     * 校验待宰圈名称是否唯一
     *
     * @param pigpen 待宰圈
     * @return 结果
     */
    @Override
    public boolean checkPigpenNameUnique(Pigpen pigpen)
    {
        Long pigpenId = StringUtils.isNull(pigpen.getId()) ? -1L : pigpen.getId();
        Pigpen info = pigpenMapper.checkPigpenNameUnique(pigpen.getPigpenName());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != pigpenId.longValue())
        {
            return false;
        }
        return true;
    }


    /**
     * 新增待宰圈
     * 
     * @param pigpen 待宰圈
     * @return 结果
     */
    @Override
    public int insertPigpen(Pigpen pigpen)
    {
        pigpen.setCreateTime(DateUtils.getNowDate());
        if(ObjectUtils.isEmpty(pigpen.getPigpenName())) {
            //获取表中最大id值
            pigpen.setPigpenName(getNextNo());
        }
        return pigpenMapper.insertPigpen(pigpen);
    }

    /**
     * 修改待宰圈
     * 
     * @param pigpen 待宰圈
     * @return 结果
     */
    @Override
    public int updatePigpen(Pigpen pigpen)
    {
        pigpen.setUpdateTime(DateUtils.getNowDate());
        return pigpenMapper.updatePigpen(pigpen);
    }

    /**
     * 批量删除待宰圈
     * 
     * @param ids 需要删除的待宰圈主键
     * @return 结果
     */
    @Override
    public int deletePigpenByIds(Long[] ids)
    {
        return pigpenMapper.deletePigpenByIds(ids);
    }

    /**
     * 删除待宰圈信息
     * 
     * @param id 待宰圈主键
     * @return 结果
     */
    @Override
    public int deletePigpenById(Long id)
    {
        return pigpenMapper.deletePigpenById(id);
    }

    @Override
    public String getNextNo() {
        //获取表中最大id值
        List<Pigpen> list = pigpenMapper.selectPigpenList(null);
        OptionalLong maxId = list.stream().mapToLong(Pigpen::getId).max();
        return String.format("%02d", maxId.getAsLong()+1);
    }
}
