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

import java.util.ArrayList;
import java.util.List;

import com.zhyc.common.annotation.DataScope;
import com.zhyc.common.utils.DateUtils;
import com.zhyc.common.utils.SecurityUtils;
import com.zhyc.common.utils.bean.BeanUtils;
import com.zhyc.module.base.domain.SheepFile;
import com.zhyc.module.base.service.impl.SheepFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhyc.module.biosafety.mapper.QuarantineReportMapper;
import com.zhyc.module.biosafety.domain.QuarantineReport;
import com.zhyc.module.biosafety.service.IQuarantineReportService;

/**
 * 检疫记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-14
 */
@Service
public class QuarantineReportServiceImpl implements IQuarantineReportService 
{
    @Autowired
    private QuarantineReportMapper quarantineReportMapper;
    @Autowired
     private SheepFileServiceImpl sheepFileService;

    /**
     * 查询检疫记录
     * 
     * @param id 检疫记录主键
     * @return 检疫记录
     */
    @Override
    public QuarantineReport selectQuarantineReportById(Long id)
    {
        return quarantineReportMapper.selectQuarantineReportById(id);
    }

    /**
     * 查询检疫记录列表
     * 
     * @param quarantineReport 检疫记录
     * @return 检疫记录
     */
    @Override
    @DataScope(deptAlias = "sqr", userAlias = "sqr")
    public List<QuarantineReport> selectQuarantineReportList(QuarantineReport quarantineReport)
    {
        String[] sheepNos = null;
        if (quarantineReport.getSheepNo() != null && !quarantineReport.getSheepNo().isEmpty()) {
            if (quarantineReport.getSheepNo().contains(" ")) {
                sheepNos = quarantineReport.getSheepNo().split(" ");
            }
        }
        quarantineReport.setSheepNos(sheepNos);
        return quarantineReportMapper.selectQuarantineReportList(quarantineReport);
    }

    /**
     * 新增检疫记录
     * 
     * @param quarantineReport 检疫记录
     * @return 结果
     */
    @Override
    public int insertQuarantineReport(QuarantineReport quarantineReport)
    {
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        quarantineReport.setCreateBy(username);
        quarantineReport.setCreateTime(DateUtils.getNowDate());
        if (quarantineReport.getResult()==null || quarantineReport.getResult().length()==0){
            quarantineReport.setStatus(0);
        }else {
            quarantineReport.setStatus(1);
        }
        List<QuarantineReport> list = new ArrayList<QuarantineReport>();
        for (String sheepId : quarantineReport.getIds()) {
            SheepFile sheepFile = sheepFileService.selectSheepFileById(Long.parseLong(sheepId));
            QuarantineReport quarantine= new QuarantineReport();
            BeanUtils.copyProperties(quarantineReport, quarantine);
            quarantine.setSheepId(sheepFile.getId());
            quarantine.setSheepNo(sheepFile.getElectronicTags());
            quarantine.setVariety(sheepFile.getVariety() != null ? sheepFile.getVariety() : "");
            quarantine.setSheepType(sheepFile.getName());

//            性别前端处理
            quarantine.setGender(String.valueOf(sheepFile.getGender()));
            quarantine.setMonthAge(sheepFile.getMonthAge());
            quarantine.setParity(sheepFile.getParity());
            quarantine.setBreed(sheepFile.getBreed());
            list.add(quarantine);
        }
        return quarantineReportMapper.insertQuarantineReport(list);
    }

    /**
     * 修改检疫记录
     * 
     * @param quarantineReport 检疫记录
     * @return 结果
     */
    @Override
    public int updateQuarantineReport(QuarantineReport quarantineReport)
    {
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        quarantineReport.setUpdateBy(username);
        quarantineReport.setUpdateTime(DateUtils.getNowDate());
        return quarantineReportMapper.updateQuarantineReport(quarantineReport);
    }

    /**
     * 批量删除检疫记录
     * 
     * @param ids 需要删除的检疫记录主键
     * @return 结果
     */
    @Override
    public int deleteQuarantineReportByIds(Long[] ids)
    {
        return quarantineReportMapper.deleteQuarantineReportByIds(ids);
    }

    /**
     * 删除检疫记录信息
     * 
     * @param id 检疫记录主键
     * @return 结果
     */
    @Override
    public int deleteQuarantineReportById(Long id)
    {
        return quarantineReportMapper.deleteQuarantineReportById(id);
    }
}
