package com.zhyc.module.frozen.service.impl;

import java.util.List;

import com.zhyc.common.exception.ServiceException;
import com.zhyc.common.utils.DateUtils;
import com.zhyc.module.base.domain.BasSheep;
import com.zhyc.module.base.domain.BasSheepVariety;
import com.zhyc.module.base.mapper.BasSheepMapper;
import com.zhyc.module.base.mapper.BasSheepVarietyMapper;
import com.zhyc.module.frozen.domain.DdFe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhyc.module.frozen.mapper.DdFsMapper;
import com.zhyc.module.frozen.domain.DdFs;
import com.zhyc.module.frozen.service.IDdFsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

/**
 * 冻精库存Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-29
 */
@Service
public class DdFsServiceImpl implements IDdFsService {
    @Autowired
    private DdFsMapper ddFsMapper;
    @Autowired
    private BasSheepMapper basSheepMapper;
    @Autowired
    private BasSheepVarietyMapper basSheepVarietyMapper;

    /**
     * 查询冻精库存
     *
     * @param id 冻精库存主键
     * @return 冻精库存
     */
    @Override
    public DdFs selectDdFsById(Long id) {
        return ddFsMapper.selectDdFsById(id);
    }

    /**
     * 查询冻精库存列表
     *
     * @param ddFs 冻精库存
     * @return 冻精库存
     */
    @Override
    public List<DdFs> selectDdFsList(DdFs ddFs) {
        return ddFsMapper.selectDdFsList(ddFs);
    }

    /**
     * 新增冻精库存
     *
     * @param ddFs 冻精库存
     * @return 结果
     */
    @Override
    public int insertDdFs(DdFs ddFs) {
        BasSheep ram = basSheepMapper.selectBasSheepByManageTags(ddFs.getCode());
        if (ram == null) {
            throw new ServiceException("公羊不存在");
        }
        if (ram.getStatusId() == null || !ram.getStatusId().equals(1L)) {
            throw new ServiceException("羊只不在群");
        }
        if (ram.getGender() == null || !ram.getGender().equals(2L)) {
            throw new ServiceException("该耳号对应不是公羊");
        }

        if (ddFs.getBreed() == null || ddFs.getBreed().trim().isEmpty()) {
            BasSheepVariety variety = basSheepVarietyMapper.selectBasSheepVarietyById(ram.getVarietyId());
            ddFs.setBreed(variety == null ? "" : variety.getVariety());
        }
        ddFs.setCreateTime(DateUtils.getNowDate());
        return ddFsMapper.insertDdFs(ddFs);
    }

    /**
     * 修改冻精库存
     *
     * @param ddFs 冻精库存
     * @return 结果
     */
    @Override
    public int updateDdFs(DdFs ddFs) {
        return ddFsMapper.updateDdFs(ddFs);
    }

    /**
     * 批量删除冻精库存
     *
     * @param ids 需要删除的冻精库存主键
     * @return 结果
     */
    @Override
    public int deleteDdFsByIds(Long[] ids) {
        return ddFsMapper.deleteDdFsByIds(ids);
    }

    /**
     * 删除冻精库存信息
     *
     * @param id 冻精库存主键
     * @return 结果
     */
    @Override
    public int deleteDdFsById(Long id) {
        return ddFsMapper.deleteDdFsById(id);
    }

    //    废弃
    @Override
    public int discard(List<DdFs> list) {
        return list.stream()
                .mapToInt(item -> {
                    DdFs update = new DdFs();
                    update.setId(item.getId());
                    update.setStat("废弃");
                    update.setDiscardTxt(item.getDiscardTxt());
                    return ddFsMapper.updateDdFs(update);
                })
                .sum();
    }
}
