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

import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

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.HealthMapper;
import com.zhyc.module.biosafety.domain.Health;
import com.zhyc.module.biosafety.service.IHealthService;

/**
 * 保健Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-15
 */
@Service
public class HealthServiceImpl implements IHealthService 
{
    @Autowired
    private HealthMapper healthMapper;

    @Autowired
    private SwMedicineUsageServiceImpl medicineUsageService;

    @Autowired
    private SwMedicineUsageMapper medicineUsageMapper;

    @Autowired
    private SheepFileMapper sheepFileMapper;

    /**
     * 查询保健
     * 
     * @param id 保健主键
     * @return 保健
     */
    @Override
    public Health selectHealthById(Long id)
    {
        Health health = healthMapper.selectHealthById(id);
        SwMedicineUsage swMedicineUsage = medicineUsageMapper.selectSwMedicineUsageById(health.getUsageId());
        health.setUsageDetails(swMedicineUsage.getSwMedicineUsageDetailsList());
        return health;
    }

    /**
     * 查询保健列表
     * 
     * @param health 保健
     * @return 保健
     */
    @Override
    @DataScope(deptAlias = "s", userAlias = "s")
    public List<Health> selectHealthList(Health health)
    {
        String[] sheepNos = null;
        if (health.getSheepNo() != null && !health.getSheepNo().isEmpty()) {
            if (health.getSheepNo().contains(" ")) {
                sheepNos = health.getSheepNo().split(" ");
            }
        }
        health.setSheepNos(sheepNos);
        return healthMapper.selectHealthList(health);
    }

    /**
     * 新增保健
     * 
     * @param health 保健
     * @return 结果
     */
    @Transient
    @Override
    public int insertHealth(Health health)
    {
        String username = SecurityUtils.getLoginUser().getUser().getNickName();

        //        使用记录的文件
        SwMedicineUsage medicineUsage = new SwMedicineUsage();
        medicineUsage.setSwMedicineUsageDetailsList(health.getUsageDetails());
        medicineUsage.setName("羊只保健");
        medicineUsage.setUseType("2");
        medicineUsage.setDatetime(health.getDatetime());

        List<Health> healths = new ArrayList<>();
        health.setCreateBy(username);
        health.setCreateTime(DateUtils.getNowDate());
        for (Integer sheepId : health.getSheepIds()) {
            SheepFile sheepFile = sheepFileMapper.selectSheepFileById(Long.valueOf(sheepId));
            Health heal  = new Health();
            BeanUtils.copyProperties(health, heal);
            heal.setSheepId(Long.valueOf(sheepId));
//            处理数据为空的情况
            heal.setVariety(sheepFile.getVariety() != null ? sheepFile.getVariety() : "");
            heal.setSheepType(sheepFile.getName() != null ? sheepFile.getName() : "");
            heal.setMonthAge(sheepFile.getMonthAge() != null ? sheepFile.getMonthAge() : 0);
            heal.setGender(sheepFile.getGender() != null ? String.valueOf(sheepFile.getGender()) : "");
            heal.setBreed(sheepFile.getBreed() != null ? sheepFile.getBreed() : "");
            heal.setParity(sheepFile.getParity() != null ? sheepFile.getParity() : 0);


            medicineUsage.setSheepId(sheepId);
//                获取药品使用记录的id
            Integer usageId = medicineUsageService.insertSwMedicineUsage(medicineUsage);

            heal.setUsageId(usageId);
            healths.add(heal);
        }
        return healthMapper.insertHealth(healths);
    }

    /**
     * 修改保健
     * 
     * @param health 保健
     * @return 结果
     */
    @Override
    public int updateHealth(Health health)
    {
        for (SwMedicineUsageDetails usageDetail : health.getUsageDetails()) {
            usageDetail.setMediUsage(health.getUsageId());
        }
        medicineUsageMapper.deleteSwMedicineUsageDetailsByMediUsage(health.getUsageId());
        medicineUsageMapper.batchSwMedicineUsageDetails(health.getUsageDetails());
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        health.setUpdateBy(username);
        health.setUpdateTime(DateUtils.getNowDate());
        return healthMapper.updateHealth(health);
    }

    /**
     * 批量删除保健
     * 
     * @param ids 需要删除的保健主键
     * @return 结果
     */
    @Override
    public int deleteHealthByIds(Long[] ids)
    {
        return healthMapper.deleteHealthByIds(ids);
    }

    /**
     * 删除保健信息
     * 
     * @param id 保健主键
     * @return 结果
     */
    @Override
    public int deleteHealthById(Long id)
    {
        return healthMapper.deleteHealthById(id);
    }
}
