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.mapper.SheepFileMapper;
import com.zhyc.module.biosafety.domain.SwMedicineUsage;
import com.zhyc.module.biosafety.domain.SwMedicineUsageDetails;
import com.zhyc.module.biosafety.mapper.SwMedicineUsageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhyc.module.biosafety.mapper.ImmunityMapper;
import com.zhyc.module.biosafety.domain.Immunity;
import com.zhyc.module.biosafety.service.IImmunityService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 免疫Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-15
 */
@Service
public class ImmunityServiceImpl implements IImmunityService 
{
    @Autowired
    private ImmunityMapper immunityMapper;

    @Autowired
    private SwMedicineUsageServiceImpl medicineUsageService;

    @Autowired
    private SwMedicineUsageMapper medicineUsageMapper;

    @Autowired
    private SheepFileMapper sheepFileMapper;


    /**
     * 查询免疫
     * 
     * @param id 免疫主键
     * @return 免疫
     */
    @Override
    public Immunity selectImmunityById(Long id)
    {
        Immunity immunity = immunityMapper.selectImmunityById(id);
        SwMedicineUsage swMedicineUsage = medicineUsageMapper.selectSwMedicineUsageById(immunity.getUsageId());
        immunity.setUsageDetails(swMedicineUsage.getSwMedicineUsageDetailsList());
        return immunity;
    }

    /**
     * 查询免疫列表
     * 
     * @param immunity 免疫
     * @return 免疫
     */
    @Override
    @DataScope(deptAlias = "s", userAlias = "s")
    public List<Immunity> selectImmunityList(Immunity immunity)
    {
        String[] sheepNos = null;
        if (immunity.getSheepNo() != null && !immunity.getSheepNo().isEmpty()) {
            if (immunity.getSheepNo().contains(" ")) {
                sheepNos = immunity.getSheepNo().split(" ");
            }
        }
        immunity.setSheepNos(sheepNos);
        return immunityMapper.selectImmunityList(immunity);
    }

    /**
     * 新增免疫
     * 
     * @param immunity 免疫
     * @return 结果
     */
    @Override
    public int insertImmunity(Immunity immunity)
    {
        String username = SecurityUtils.getLoginUser().getUser().getNickName();

        //        使用记录的文件
        SwMedicineUsage medicineUsage = new SwMedicineUsage();
        medicineUsage.setSwMedicineUsageDetailsList(immunity.getUsageDetails());
        medicineUsage.setName("羊只免疫");
        medicineUsage.setUseType("0");
        medicineUsage.setDatetime(immunity.getDatetime());
        medicineUsage.setCreateBy(username);


        List<Immunity> immunities = new ArrayList<>();

        immunity.setUpdateBy(username);
        immunity.setCreateTime(DateUtils.getNowDate());

        for (Integer sheepId : immunity.getSheepIds()) {
            SheepFile sheepFile = sheepFileMapper.selectSheepFileById(Long.valueOf(sheepId));

            Immunity imm = new Immunity();
            BeanUtils.copyProperties(immunity, imm);
            imm.setSheepId(Long.valueOf(sheepId));
            imm.setVariety(sheepFile.getVariety() != null ? sheepFile.getVariety() : "");
            imm.setSheepType(sheepFile.getName() != null ? sheepFile.getName() : "");
            imm.setMonthAge(sheepFile.getMonthAge() != null ? sheepFile.getMonthAge() : 0);
            imm.setGender(sheepFile.getGender() != null ? String.valueOf(sheepFile.getGender()) : "");
            imm.setBreed(sheepFile.getBreed() != null ? sheepFile.getBreed() : "");
            imm.setParity(sheepFile.getParity() != null ? sheepFile.getParity() : 0);
            medicineUsage.setSheepId(sheepId);
//                获取药品使用记录的id
            Integer usageId = medicineUsageService.insertSwMedicineUsage(medicineUsage);

            imm.setUsageId(usageId);
            immunities.add(imm);
        }


        immunity.setCreateTime(DateUtils.getNowDate());
        return immunityMapper.insertImmunity(immunities);
    }

    /**
     * 修改免疫
     * 
     * @param immunity 免疫
     * @return 结果
     */
    @Override
    @Transactional
    public int updateImmunity(Immunity immunity)
    {
        for (SwMedicineUsageDetails usageDetail : immunity.getUsageDetails()) {
            usageDetail.setMediUsage(immunity.getUsageId());
        }
        medicineUsageMapper.deleteSwMedicineUsageDetailsByMediUsage(immunity.getUsageId());
        medicineUsageMapper.batchSwMedicineUsageDetails(immunity.getUsageDetails());
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        immunity.setUpdateBy(username);
        immunity.setUpdateTime(DateUtils.getNowDate());
        return immunityMapper.updateImmunity(immunity);
    }

    /**
     * 批量删除免疫
     * 
     * @param ids 需要删除的免疫主键
     * @return 结果
     */
    @Override
    public int deleteImmunityByIds(Long[] ids)
    {
        return immunityMapper.deleteImmunityByIds(ids);
    }

    /**
     * 删除免疫信息
     * 
     * @param id 免疫主键
     * @return 结果
     */
    @Override
    public int deleteImmunityById(Long id)
    {
        return immunityMapper.deleteImmunityById(id);
    }
}
