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

import java.util.ArrayList;
import java.util.Arrays;
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.Deworm;
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.DewormMapper;
import com.zhyc.module.biosafety.service.IDewormService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 驱虫Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-15
 */
@Service
public class DewormServiceImpl implements IDewormService 
{
    @Autowired
    private DewormMapper dewormMapper;
    @Autowired
    private SwMedicineUsageServiceImpl medicineUsageService;
    @Autowired
    private SwMedicineUsageMapper medicineUsageMapper;
    @Autowired
    private SheepFileMapper sheepFileMapper;

    /**
     * 查询驱虫
     * 
     * @param id 驱虫主键
     * @return 驱虫
     */
    @Override
    public Deworm selectDewormById(Long id)
    {
        Deworm deworm = dewormMapper.selectDewormById(id);
        SwMedicineUsage swMedicineUsage = medicineUsageMapper.selectSwMedicineUsageById(deworm.getUsageId());
        deworm.setUsageDetails(swMedicineUsage.getSwMedicineUsageDetailsList());
        return deworm;
    }

    /**
     * 查询驱虫列表
     * 
     * @param deworm 驱虫
     * @return 驱虫
     */
    @Override
    @DataScope(deptAlias = "s", userAlias = "s")
    public List<Deworm> selectDewormList(Deworm deworm)
    {
        String[] sheepNos = null;
        if (deworm.getSheepNo() != null && !deworm.getSheepNo().isEmpty()) {
            if (deworm.getSheepNo().contains(" ")) {
                sheepNos = deworm.getSheepNo().split(" ");
                deworm.setSheepNos(sheepNos);
                deworm.setSheepNo(null);
            }
        }

        return dewormMapper.selectDewormList(deworm);
    }

    /**
     * 新增驱虫
     * 
     * @param deworm 驱虫
     * @return 结果
     */
    @Override
    @Transactional
    public int insertDeworm(Deworm deworm)
    {
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        //        使用记录的文件
        SwMedicineUsage medicineUsage = new SwMedicineUsage();
        medicineUsage.setSwMedicineUsageDetailsList(deworm.getUsageDetails());
        medicineUsage.setName("羊只驱虫");
        medicineUsage.setUseType("1");
        medicineUsage.setDatetime(deworm.getDatetime());

            List<Deworm> deworms = new ArrayList<>();

            deworm.setCreateBy(username);
            deworm.setCreateTime(DateUtils.getNowDate());
            for (Integer sheepId : deworm.getSheepIds()) {
                SheepFile sheepFile = sheepFileMapper.selectSheepFileById(Long.valueOf(sheepId));

                Deworm dew  = new Deworm(); 
                BeanUtils.copyProperties(deworm, dew);
                dew.setSheepId(Long.valueOf(sheepId));
                dew.setVariety(sheepFile.getVariety() != null ? sheepFile.getVariety() : "");
                dew.setSheepType(sheepFile.getName() != null ? sheepFile.getName() : "");
                dew.setMonthAge(sheepFile.getMonthAge() != null ? sheepFile.getMonthAge() : 0);
                dew.setGender(sheepFile.getGender() != null ? String.valueOf(sheepFile.getGender()) : "");
                dew.setBreed(sheepFile.getBreed() != null ? sheepFile.getBreed() : "");
                dew.setParity(sheepFile.getParity() != null ? sheepFile.getParity() : 0);

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


        return dewormMapper.insertDeworm(deworms);
    }

    /**
     * 修改驱虫
     * 
     * @param deworm 驱虫
     * @return 结果
     */
    @Override
    @Transactional
    public int updateDeworm(Deworm deworm)
    {
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        for (SwMedicineUsageDetails usageDetail : deworm.getUsageDetails()) {
            usageDetail.setMediUsage(deworm.getUsageId());
        }
        medicineUsageMapper.deleteSwMedicineUsageDetailsByMediUsage(deworm.getUsageId());
        SwMedicineUsage swMedicineUsage = new SwMedicineUsage();

        swMedicineUsage.setId(deworm.getUsageId());
        swMedicineUsage.setUpdateBy(username);
        swMedicineUsage.setUpdateTime(DateUtils.getNowDate());
        medicineUsageMapper.updateSwMedicineUsage(swMedicineUsage);

        medicineUsageMapper.batchSwMedicineUsageDetails(deworm.getUsageDetails());
        deworm.setUpdateBy(username);
        deworm.setUpdateTime(DateUtils.getNowDate());
        return dewormMapper.updateDeworm(deworm);
    }

    /**
     * 批量删除驱虫
     * 
     * @param ids 需要删除的驱虫主键
     * @return 结果
     */
    @Override
    public int deleteDewormByIds(Long[] ids)
    {
        return dewormMapper.deleteDewormByIds(ids);
    }

    /**
     * 删除驱虫信息
     * 
     * @param id 驱虫主键
     * @return 结果
     */
    @Override
    public int deleteDewormById(Long id)
    {
        return dewormMapper.deleteDewormById(id);
    }
}
