package com.huimv.management.service.impl;

import cn.hutool.core.date.DateUtil;
import com.huimv.management.dao.HealthImmuneRecordDao;
import com.huimv.management.dao.HealthIsolationRecordDao;
import com.huimv.management.entity.HealthImmuneRecordEntity;
import com.huimv.management.entity.HealthIsolationRecordEntity;
import com.huimv.management.entity.dto.DiseaseDto;
import com.huimv.management.entity.dto.ImmuneCountDto;
import com.huimv.management.entity.dto.IsolationDto;
import com.huimv.management.entity.dto.TreatResultDto;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.common.utils.PageUtils;
import com.huimv.common.utils.Query;

import com.huimv.management.dao.HealthMedicalRecordDao;
import com.huimv.management.entity.HealthMedicalRecordEntity;
import com.huimv.management.service.HealthMedicalRecordService;


@Service("healthMedicalRecordService")
public class HealthMedicalRecordServiceImpl extends ServiceImpl<HealthMedicalRecordDao, HealthMedicalRecordEntity> implements HealthMedicalRecordService {

    @Autowired
    private HealthMedicalRecordDao healthMedicalRecordDao;

    @Autowired
    private HealthImmuneRecordDao healthImmuneRecordDao;

    @Autowired
    private HealthIsolationRecordDao healthIsolationRecordDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        //根据牧场查询
        String farmId = (String) params.get("formFarmId");
        QueryWrapper<HealthMedicalRecordEntity> wrapper = new QueryWrapper<HealthMedicalRecordEntity>();
        wrapper.eq(StringUtils.isNotBlank(farmId),"farm_id",farmId);

        //耳标
        String pigId = (String) params.get("eartag");
        wrapper.like(StringUtils.isNotBlank(pigId),"pig_id",pigId);

        //疾病名称
        String diseaseName = (String) params.get("diseaseName");
        wrapper.eq(StringUtils.isNotBlank(diseaseName),"disease_name",diseaseName);

        //病因
        String causeName = (String) params.get("causeName");
        wrapper.eq(StringUtils.isNotBlank(causeName),"cause_name",causeName);

        //症状
        String symptomName = (String) params.get("symptomName");
        wrapper.eq(StringUtils.isNotBlank(symptomName),"symptom_name",symptomName);

        //兽医姓名
        String doctor = (String) params.get("doctor");
        wrapper.eq(StringUtils.isNotBlank(doctor),"doctor",doctor);

        //治疗方案
        String treatmentPlan = (String) params.get("treatmentPlan");
        wrapper.eq(StringUtils.isNotBlank(treatmentPlan),"treatment_plan",treatmentPlan);

        //治疗结果
        String result = (String) params.get("result");
        wrapper.eq(StringUtils.isNotBlank(result),"result",result);

        IPage<HealthMedicalRecordEntity> page = this.page(
                new Query<HealthMedicalRecordEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public Map<String, Object> diseaseCount(String type, String farmId) {
        Map<String,Object> resultMap = new HashMap<>();

        Date endTime = DateUtil.date();
        Date startTime;

        switch (type){
            //今日
            case "1":
                startTime= DateUtil.beginOfDay(endTime);
                break;
            //本月
            case "2":
                startTime= DateUtil.beginOfMonth(endTime);
                break;
            //本周
            default:
                startTime = DateUtil.beginOfWeek(endTime);
        }


        QueryWrapper<HealthMedicalRecordEntity> medicalWrapper = new QueryWrapper<HealthMedicalRecordEntity>();
        medicalWrapper.eq("farm_id",Integer.parseInt(farmId));

        //大于等于
        medicalWrapper.ge("create_time",startTime);
        //逻辑删除
        medicalWrapper.apply("effective = 1");



        //疾病统计
        List<DiseaseDto> diseaseDtos  = healthMedicalRecordDao.diseaseCount(medicalWrapper);
        resultMap.put("diseaseCount",diseaseDtos);

        //治愈统计
        List<TreatResultDto> treatResultDtos = new ArrayList<>();

        medicalWrapper.eq("result",1);
        TreatResultDto dto =new TreatResultDto();
        dto.setTreatResult(1+"");
        Integer healthCount1 = healthMedicalRecordDao.selectCount(medicalWrapper);
        dto.setTreatResultCount( healthCount1);
        treatResultDtos.add(dto);

        medicalWrapper.clear();
        medicalWrapper.eq("result",2);
        medicalWrapper.eq("farm_id",Integer.parseInt(farmId));
        medicalWrapper.ge("create_time",startTime);
        TreatResultDto dto2 =new TreatResultDto();
        dto2.setTreatResult(2+"");
        Integer healthCount2 = healthMedicalRecordDao.selectCount(medicalWrapper);
        dto2.setTreatResultCount(healthCount2 );
        treatResultDtos.add(dto2);
        if (healthCount1 == 0 && healthCount2 == 0){
            resultMap.put("treatResultCount",null);
        }else {
            resultMap.put("treatResultCount",treatResultDtos);
        }






        //疫苗统计
        QueryWrapper<HealthImmuneRecordEntity>  immuneWrapper = new QueryWrapper<HealthImmuneRecordEntity>();

        immuneWrapper.eq("farm_id",farmId);
        immuneWrapper.le("create_time",endTime);
        immuneWrapper.ge("create_time",startTime);
        immuneWrapper.apply("effective = 1");

        List<ImmuneCountDto> immuneCountDtos = healthImmuneRecordDao.vaccineCoount(immuneWrapper);
        resultMap.put("imuneCount",immuneCountDtos);

        //隔离统计  healthIsolationRecordDao
        QueryWrapper<HealthIsolationRecordEntity> isolationWrapper = new QueryWrapper<HealthIsolationRecordEntity>();
        isolationWrapper.eq("farm_id",farmId);
        isolationWrapper.le("create_time",endTime);
        isolationWrapper.ge("create_time",startTime);
        isolationWrapper.apply("effective = 1");

        List<IsolationDto> isolationDtos = healthIsolationRecordDao.isolationCount(isolationWrapper);
        resultMap.put("isolationCount",isolationDtos);


        return resultMap;
    }

    @Override
    public void updateCureResult(List<Integer> asList,Integer result) {
        HealthMedicalRecordEntity recordEntity = new HealthMedicalRecordEntity();
        for (Integer id : asList) {
            recordEntity.setId(id);
            recordEntity.setResult(result);
            healthMedicalRecordDao.updateById(recordEntity);
        }
    }
}