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

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.zhyc.common.annotation.DataScope;
import com.zhyc.common.utils.DateUtils;
import com.zhyc.common.utils.SecurityUtils;
import com.zhyc.module.base.domain.BasSheep;
import com.zhyc.module.base.domain.SheepFile;
import com.zhyc.module.base.mapper.BasSheepMapper;
import com.zhyc.module.base.mapper.SheepFileMapper;
import com.zhyc.module.biosafety.domain.Treatment;
import com.zhyc.module.biosafety.mapper.DiagnosisMapper;
import com.zhyc.module.biosafety.mapper.TreatmentMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhyc.module.biosafety.domain.Diagnosis;
import com.zhyc.module.biosafety.service.IDiagnosisService;
import org.springframework.transaction.annotation.Transactional;



/**
 * 诊疗结果Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-15
 */
@Service
public class DiagnosisServiceImpl implements IDiagnosisService 
{
    @Autowired
    private DiagnosisMapper diagnosisMapper;
    @Autowired
    private SheepFileMapper sheepFileMapper;
    @Autowired
    private BasSheepMapper sheepMapper;
    @Autowired
    private TreatmentMapper treatmentMapper;

    /**
     * 查询诊疗结果
     * 
     * @param id 诊疗结果主键
     * @return 诊疗结果
     */
    @Override
    public Diagnosis selectDiagnosisById(Long id)
    {

        return diagnosisMapper.selectDiagnosisById(id);
    }

    /**
     * 查询诊疗结果列表
     * 
     * @param diagnosis 诊疗结果
     * @return 诊疗结果
     */
    @Override
    @DataScope(deptAlias = "sd", userAlias = "sd")
    public List<Diagnosis> selectDiagnosisList(Diagnosis diagnosis)
    {
        String[] sheepNos = null;
        if (diagnosis.getSheepNo() != null && !diagnosis.getSheepNo().isEmpty()) {
            if (diagnosis.getSheepNo().contains(" ")) {
                sheepNos = diagnosis.getSheepNo().split(" ");
            }
        }
        diagnosis.setSheepNos(sheepNos);
        return diagnosisMapper.selectDiagnosisList(diagnosis);
    }

    /**
     * 新增诊疗结果
     * 
     * @param diagnosis 诊疗结果
     * @return 结果
     */
    @Override
    @Transactional
    public int insertDiagnosis(Diagnosis diagnosis)
    {
        SheepFile sheepFile = sheepFileMapper.selectSheepFileById(diagnosis.getSheepId());
        diagnosis.setSheepType(sheepFile.getName());
        diagnosis.setParity(String.valueOf(sheepFile.getParity()));
        diagnosis.setGender(String.valueOf(sheepFile.getGender()));
        diagnosis.setMonthAge(sheepFile.getMonthAge());

        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        diagnosis.setCreateBy(username);
        diagnosis.setCreateTime(DateUtils.getNowDate());

        if (diagnosis.getSheepfoldId() != null)
            if (!Objects.equals(sheepFile.getSheepfoldId(), diagnosis.getSheepfoldId())) {
                BasSheep basSheep = new BasSheep();
                basSheep.setId(diagnosis.getSheepId());
                basSheep.setSheepfoldId(diagnosis.getSheepfoldId());
                sheepMapper.updateBasSheep(basSheep);
            }

//        更改治疗记录的状态
        Treatment treatment = new Treatment();
        treatment.setId(diagnosis.getTreatId());
        if (diagnosis.getResult().equals("0")){
            treatment.setStatus("-1");
            treatmentMapper.updateTreatment(treatment);
        }else if (diagnosis.getResult().equals("1")){
            treatment.setStatus("2");
            treatmentMapper.updateTreatment(treatment);
        }
//        转入其他羊舍
        return diagnosisMapper.insertDiagnosis(diagnosis);
    }


    @Override
    @Transactional
    public int insertDiagnosisList(Diagnosis diagnosis) {
        if (diagnosis.getTreatIds() != null && diagnosis.getTreatIds().length > 0){
            for (Integer treatId : diagnosis.getTreatIds()) {
                System.out.println(treatId);
                Treatment treatment = treatmentMapper.selectTreatmentById(Long.valueOf(treatId));
                System.out.println(treatment);
//                BasSheep basSheep = sheepMapper.selectBasSheepById(treatment.getSheepId());

                Diagnosis diag = new Diagnosis();
                BeanUtils.copyProperties(treatment,diag);
                diag.setResult(diagnosis.getResult());
                diag.setParity(String.valueOf(treatment.getParity()));


                // treatment 和 diagnosis 是你的两个实体对象
                Date start = treatment.getDatetime();   // 已经是 Date
                Date end   = diagnosis.getDatetime();   // 已经是 Date
                diag.setBegindate(start);
                diag.setEnddate(end);
                long oneDayMillis = 24 * 60 * 60 * 1000L;
                long days = (end.getTime() / oneDayMillis) - (start.getTime() / oneDayMillis);
                if (days<0){
                    days=0;
                }

                diag.setTreatDay(days);
                diag.setTreatId(treatment.getId());


                String username = SecurityUtils.getLoginUser().getUser().getNickName();
                diag.setCreateBy(username);
                diag.setCreateTime(DateUtils.getNowDate());
                diagnosisMapper.insertDiagnosis(diag);
                treatment.setDiagId(diag.getId());

                //        更改治疗记录的状态
                if (diagnosis.getResult().equals("0")){
                    treatment.setStatus("-1");
                    treatmentMapper.updateTreatment(treatment);
                }else if (diagnosis.getResult().equals("1")){
                    treatment.setStatus("2");
                    treatmentMapper.updateTreatment(treatment);
                }
                if (diagnosis.getSheepfoldId() != null){
                    BasSheep basSheep = new BasSheep();
                    basSheep.setId(diagnosis.getSheepId());
                    basSheep.setSheepfoldId(diagnosis.getSheepfoldId());
                    sheepMapper.updateBasSheep(basSheep);
                    }
            }
        }

        return 1;
    }

    /**
     * 修改诊疗结果
     * 
     * @param diagnosis 诊疗结果
     * @return 结果
     */
    @Override
    @Transactional
    public int updateDiagnosis(Diagnosis diagnosis)
    {
        BasSheep basSheep = new BasSheep();
        basSheep.setId(diagnosis.getSheepId());
        basSheep.setSheepfoldId(diagnosis.getSheepfoldId());
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        basSheep.setUpdateBy(username);
        basSheep.setUpdateTime(DateUtils.getNowDate());
        diagnosis.setUpdateBy(username);
        diagnosis.setUpdateTime(DateUtils.getNowDate());
        sheepMapper.updateBasSheep(basSheep);
        return diagnosisMapper.updateDiagnosis(diagnosis);
    }

    /**
     * 批量删除诊疗结果
     * 
     * @param ids 需要删除的诊疗结果主键
     * @return 结果
     */
    @Override
    public int deleteDiagnosisByIds(Long[] ids)
    {
        return diagnosisMapper.deleteDiagnosisByIds(ids);
    }

    /**
     * 删除诊疗结果信息
     * 
     * @param id 诊疗结果主键
     * @return 结果
     */
    @Override
    public int deleteDiagnosisById(Long id)
    {
        return diagnosisMapper.deleteDiagnosisById(id);
    }

}
