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

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.module.biosafety.service.ISwMedicineUsageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.zhyc.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.zhyc.module.biosafety.domain.SwMedicineUsageDetails;
import com.zhyc.module.biosafety.mapper.SwMedicineUsageMapper;
import com.zhyc.module.biosafety.domain.SwMedicineUsage;

/**
 * 药品使用记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-12
 */
@Service
public class SwMedicineUsageServiceImpl implements ISwMedicineUsageService
{
    @Autowired
    private SwMedicineUsageMapper swMedicineUsageMapper;

    /**
     * 查询药品使用记录
     * 
     * @param id 药品使用记录主键
     * @return 药品使用记录
     */
    @Override
    public SwMedicineUsage selectSwMedicineUsageById(Integer id)
    {
        return swMedicineUsageMapper.selectSwMedicineUsageById(id);
    }

    /**
     * 查询药品使用记录列表
     * 
     * @param swMedicineUsage 药品使用记录
     * @return 药品使用记录
     */
    @Override
    @DataScope(deptAlias = "smu", userAlias = "smu")
    public List<SwMedicineUsage> selectSwMedicineUsageList(SwMedicineUsage swMedicineUsage)
    {
        String[] sheepNos = null;
        if (swMedicineUsage.getSheepNo() != null && !swMedicineUsage.getSheepNo().isEmpty()) {
            if (swMedicineUsage.getSheepNo().contains(" ")) {
                sheepNos = swMedicineUsage.getSheepNo().split(" ");
                swMedicineUsage.setSheepNos(sheepNos);
                swMedicineUsage.setSheepNo(null);
            }
        }
        return swMedicineUsageMapper.selectSwMedicineUsageList(swMedicineUsage);
    }

    /**
     * 新增药品使用记录
     * 
     * @param swMedicineUsage 药品使用记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSwMedicineUsage(SwMedicineUsage swMedicineUsage)
    {
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        swMedicineUsage.setCreateBy(username);
        swMedicineUsage.setCreateTime(DateUtils.getNowDate());
        int rows = swMedicineUsageMapper.insertSwMedicineUsage(swMedicineUsage);
        insertSwMedicineUsageDetails(swMedicineUsage);
        return swMedicineUsage.getId();
    }

    /**
     * 修改药品使用记录
     * 
     * @param swMedicineUsage 药品使用记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSwMedicineUsage(SwMedicineUsage swMedicineUsage)
    {
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        swMedicineUsage.setUpdateBy(username);
        swMedicineUsage.setUpdateTime(DateUtils.getNowDate());
        swMedicineUsageMapper.deleteSwMedicineUsageDetailsByMediUsage(swMedicineUsage.getId());
        insertSwMedicineUsageDetails(swMedicineUsage);
        return swMedicineUsageMapper.updateSwMedicineUsage(swMedicineUsage);
    }

    /**
     * 批量删除药品使用记录
     * 
     * @param ids 需要删除的药品使用记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSwMedicineUsageByIds(Long[] ids)
    {
        swMedicineUsageMapper.deleteSwMedicineUsageDetailsByMediUsages(ids);
        return swMedicineUsageMapper.deleteSwMedicineUsageByIds(ids);
    }

    /**
     * 删除药品使用记录信息
     * 
     * @param id 药品使用记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSwMedicineUsageById(Integer id)
    {
        swMedicineUsageMapper.deleteSwMedicineUsageDetailsByMediUsage(id);
        return swMedicineUsageMapper.deleteSwMedicineUsageById(id);
    }

    @Override
    public List<SwMedicineUsageDetails> selectSwMedicineUsageDetailsById(Integer id) {
        return swMedicineUsageMapper.selectSwMedicineUsageDetailsById(id);
    }

    /**
     * 新增药品使用记录详情信息
     * 
     * @param swMedicineUsage 药品使用记录对象
     */
    public void insertSwMedicineUsageDetails(SwMedicineUsage swMedicineUsage)
    {
        List<SwMedicineUsageDetails> swMedicineUsageDetailsList = swMedicineUsage.getSwMedicineUsageDetailsList();
        Integer id = swMedicineUsage.getId();
        if (StringUtils.isNotNull(swMedicineUsageDetailsList))
        {
            List<SwMedicineUsageDetails> list = new ArrayList<SwMedicineUsageDetails>();
            for (SwMedicineUsageDetails swMedicineUsageDetails : swMedicineUsageDetailsList)
            {
                swMedicineUsageDetails.setMediUsage(id);
                list.add(swMedicineUsageDetails);
            }
            if (list.size() > 0)
            {
                swMedicineUsageMapper.batchSwMedicineUsageDetails(list);
            }
        }
    }
}
