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.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.DisinfectMapper;
import com.zhyc.module.biosafety.domain.Disinfect;
import com.zhyc.module.biosafety.service.IDisinfectService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 消毒记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-15
 */
@Service
public class DisinfectServiceImpl implements IDisinfectService 
{
    @Autowired
    private DisinfectMapper disinfectMapper;

    @Autowired
    private SwMedicineUsageServiceImpl medicineUsageService;

    @Autowired
    private SwMedicineUsageMapper medicineUsageMapper;

    @Autowired
    private SheepFileMapper sheepFileMapper;

    /**
     * 查询消毒记录
     * 
     * @param id 消毒记录主键
     * @return 消毒记录
     */
    @Override
    public Disinfect selectDisinfectById(Long id)
    {
        Disinfect disinfect = disinfectMapper.selectDisinfectById(id);
        SwMedicineUsage swMedicineUsage = medicineUsageService.selectSwMedicineUsageById(disinfect.getUsageId());
        disinfect.setUsageDetails(swMedicineUsage.getSwMedicineUsageDetailsList());
        return disinfect;
    }

    /**
     * 查询消毒记录列表
     * 
     * @param disinfect 消毒记录
     * @return 消毒记录
     */
    @Override
    @DataScope(deptAlias = "sd", userAlias = "sd")
    public List<Disinfect> selectDisinfectList(Disinfect disinfect)
    {
        return disinfectMapper.selectDisinfectList(disinfect);
    }

    /**
     * 新增消毒记录
     * 
     * @param disinfect 消毒记录
     * @return 结果
     */
    @Override
    public int insertDisinfect(Disinfect disinfect)
    {
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        //        使用记录的文件
        SwMedicineUsage medicineUsage = new SwMedicineUsage();
        medicineUsage.setSwMedicineUsageDetailsList(disinfect.getUsageDetails());
        medicineUsage.setName("羊舍消毒");
        medicineUsage.setUseType("3");
        medicineUsage.setDatetime(disinfect.getDatetime());


        List<Disinfect> disinfects = new ArrayList<>();

        disinfect.setCreateBy(username);
        disinfect.setCreateTime(DateUtils.getNowDate());

        for (Integer sheepfold : disinfect.getSheepfoldIds()) {
            Disinfect dis = new Disinfect();
            BeanUtils.copyProperties(disinfect,dis);
            dis.setSheepfoldId(sheepfold);

            medicineUsage.setSheepfoldId(sheepfold);
//                获取药品使用记录的id
            Integer usageId = medicineUsageService.insertSwMedicineUsage(medicineUsage);
            dis.setUsageId(usageId);
            disinfects.add(dis);
        }

        return disinfectMapper.insertDisinfect(disinfects);
    }

    /**
     * 修改消毒记录
     * 
     * @param disinfect 消毒记录
     * @return 结果
     */
    @Override
    @Transactional
    public int updateDisinfect(Disinfect disinfect)
    {
        for (SwMedicineUsageDetails usageDetail : disinfect.getUsageDetails()) {
            usageDetail.setMediUsage(disinfect.getUsageId());
        }
        medicineUsageMapper.deleteSwMedicineUsageDetailsByMediUsage(disinfect.getUsageId());
        medicineUsageMapper.batchSwMedicineUsageDetails(disinfect.getUsageDetails());
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        disinfect.setUpdateBy(username);
        disinfect.setUpdateTime(DateUtils.getNowDate());
        return disinfectMapper.updateDisinfect(disinfect);
    }

    /**
     * 批量删除消毒记录
     * 
     * @param ids 需要删除的消毒记录主键
     * @return 结果
     */
    @Override
    public int deleteDisinfectByIds(Long[] ids)
    {
        return disinfectMapper.deleteDisinfectByIds(ids);
    }

    /**
     * 删除消毒记录信息
     * 
     * @param id 消毒记录主键
     * @return 结果
     */
    @Override
    public int deleteDisinfectById(Long id)
    {
        return disinfectMapper.deleteDisinfectById(id);
    }
}
