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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhyc.common.exception.ServiceException;
import com.zhyc.common.utils.DateUtils;
import com.zhyc.common.utils.StringUtils;
import com.zhyc.module.produce.breed.mapper.ScEmbryoFlushMapper;
import com.zhyc.module.produce.breed.service.IScEmbryoFlushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhyc.module.frozen.mapper.DdFeMapper;
import com.zhyc.module.frozen.domain.DdFe;
import com.zhyc.module.frozen.service.IDdFeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 冻胚库存Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-29
 */
@Service
public class DdFeServiceImpl implements IDdFeService
{
    @Autowired
    private DdFeMapper ddFeMapper;
    @Autowired
    private ScEmbryoFlushMapper scEmbryoFlushMapper;
    @Autowired
    private IScEmbryoFlushService scEmbryoFlushService;
    /**
     * 查询冻胚库存
     *
     * @param id 冻胚库存主键
     * @return 冻胚库存
     */
    @Override
    public DdFe selectDdFeById(Long id)
    {
        return ddFeMapper.selectDdFeById(id);
    }

    /**
     * 查询冻胚库存列表
     *
     * @param ddFe 冻胚库存
     * @return 冻胚库存
     */
    @Override
    public List<DdFe> selectDdFeList(DdFe ddFe)
    {
        return ddFeMapper.selectDdFeList(ddFe);
    }

    /**
     * 新增冻胚库存
     *
     * @param ddFe 冻胚库存
     * @return 结果
     */
    @Override
    public int insertDdFe(DdFe ddFe)
    {
        if (ddFeMapper.existsByCode(ddFe.getCode()) > 0) {
            throw new ServiceException("胚胎编号已存在，请勿重复录入");
        }
        ddFe.setCreateTime(DateUtils.getNowDate());
        return ddFeMapper.insertDdFe(ddFe);
    }

    /**
     * 修改冻胚库存
     *
     * @param ddFe 冻胚库存
     * @return 结果
     */
    @Override
    public int updateDdFe(DdFe ddFe)
    {
        return ddFeMapper.updateDdFe(ddFe);
    }

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

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

    @Override
    public Map<String, Object> getLastFlushInfoByEwe(String eweNo) {
        // 1. 冲胚数据
        Map<String, Object> flush = ddFeMapper.selectFlushByEwe(eweNo);
        if (flush == null) return null;

        String ramId = (String) flush.get("ramId");
        if (StringUtils.isBlank(ramId)) return null;

        // 2. 公羊品种
        Map<String, Object> ram = scEmbryoFlushMapper.selectSheepInfoByManageTag(ramId);
        if (ram == null) {
            throw new ServiceException("供体公羊耳号在羊只档案中未找到");
        }
        String ramBreed = (String) ram.get("variety");

        // 3. 母羊品种
        Map<String, Object> ewe = scEmbryoFlushMapper.selectSheepInfoByManageTag(eweNo);
        if (ewe == null) {
            throw new ServiceException("供体母羊耳在羊只档案中未找到");
        }
        String eweBreed = (String) ewe.get("variety");

        // 4. 胚胎品种
        String embryoBreed = scEmbryoFlushService.calculateEmbryoVariety(ramBreed, eweBreed);

        // 5. 封装 4 字段
        Map<String, Object> rsp = new HashMap<>(8);
        rsp.put("drId", ramId);
        rsp.put("drBreed", ramBreed);
        rsp.put("deBreed", eweBreed);
        rsp.put("embBreed", embryoBreed);
        rsp.put("gradeA", flush.get("gradeA"));
        rsp.put("gradeB", flush.get("gradeB"));
        rsp.put("gradeC", flush.get("gradeC"));
        rsp.put("gradeD", flush.get("gradeD"));
        return rsp;
    }

//    废弃
    @Override
    public int discard(DdFe dto) {
        return ddFeMapper.updateDiscard(dto);
    }
}
