package com.huimv.management.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huimv.common.exception.RRException;
import com.huimv.management.dao.YearPigBaseDao;
import com.huimv.management.entity.HealthMedicalRecordEntity;
import com.huimv.management.entity.YearPigBaseEntity;
import com.huimv.management.service.YearPigBaseService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

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.HealthImmuneRecordDao;
import com.huimv.management.entity.HealthImmuneRecordEntity;
import com.huimv.management.service.HealthImmuneRecordService;


@Service("healthImmuneRecordService")
public class HealthImmuneRecordServiceImpl extends ServiceImpl<HealthImmuneRecordDao, HealthImmuneRecordEntity> implements HealthImmuneRecordService {

    @Autowired
    private YearPigBaseDao yearPigBaseDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //根据牧场查询
        String farmId = (String) params.get("formFarmId");
        String eartag = (String)params.get("eartag");

        QueryWrapper<HealthImmuneRecordEntity> wrapper = new QueryWrapper<HealthImmuneRecordEntity>();
        wrapper.eq(StringUtils.isNotBlank(farmId),"farm_id",Integer.parseInt(farmId));
        if (StringUtils.isNotBlank(eartag)){
            wrapper.like(StringUtils.isNotBlank(eartag),"pig_id",eartag)
            .or().like("man_name",eartag).or().like("vaccine_name",eartag)
            .or().like("create_time",eartag);
        }

        String vaccineName = (String)params.get("vaccineName");
        String manName= (String)params.get("manName");
        wrapper.eq(StringUtils.isNotBlank(vaccineName),"vaccine_name",vaccineName);
        wrapper.eq(StringUtils.isNotBlank(manName),"manName",manName);


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

        return new PageUtils(page);
    }

    @Override
    public void saveAll(String[] eartags, String vaccineName, Integer farmId, String manName, Integer manId) {

        StringJoiner sj = new StringJoiner(",","未找到耳标号为："," 的信息，请检查输入的耳标号是否有误！");
        int originLength = sj.length();
        for (String eartag : eartags) {
            YearPigBaseEntity yearPigBaseEntity = yearPigBaseDao.selectOne(Wrappers.<YearPigBaseEntity>lambdaQuery().eq(YearPigBaseEntity::getEartag, eartag));
            if (yearPigBaseEntity == null) {
                sj.add(eartag);
            }
        }
        if (sj.length() > originLength) {
            throw new RRException(sj.toString(), 1001);
        }

        HealthImmuneRecordEntity healthImmuneRecordEntity = new HealthImmuneRecordEntity();
        healthImmuneRecordEntity.setFarmId(farmId);
        healthImmuneRecordEntity.setManId(manId);
        healthImmuneRecordEntity.setManName(manName);
        healthImmuneRecordEntity.setVaccineName(vaccineName);
        for (String eartag : eartags) {
            healthImmuneRecordEntity.setPigId(eartag);
            save(healthImmuneRecordEntity);
        }
    }

}