package com.bonus.boot.manager.manager.service.impl;

import com.bonus.boot.manager.basic.entity.MapBean;
import com.bonus.boot.manager.manager.dao.TypeDao;
import com.bonus.boot.manager.manager.dao.UtilDao;
import com.bonus.boot.manager.manager.model.SysType;
import com.bonus.boot.manager.manager.service.TypeService;
import com.bonus.boot.manager.manager.utils.StringHelper;
import com.bonus.boot.manager.manager.utils.UserUtil;
import com.bonus.boot.manager.physical.entity.CountImportBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

@Service
public class TypeServiceImpl implements TypeService {

	private static final Logger log = LoggerFactory.getLogger("adminLogger");
	@Autowired
	private TypeDao typeDao;
	@Resource
	private UtilDao utilDao;
	@Autowired
	private BCryptPasswordEncoder passwordEncoder;
	@Override
	public List<SysType> gettyMsgList(SysType o) {
		return typeDao.gettyMsgList(o);
	}

	@Override
	public int delById(SysType bean) {
		return typeDao.delById(bean);
	}

	@Override
	public int addInfo(SysType bean) {
		int num = utilDao.determineIfExists("pb_base_illness_type","illness_type ",bean.getIllnessType(),"");
		if(num > 0){
			num = 3;
		}else {
			num = typeDao.addInfo(bean);
		}
		return num;
	}

	@Override
	public int updateInfo(SysType bean) {
		int num = utilDao.determineIfExists("pb_base_illness_type","illness_type",bean.getIllnessType(),bean.getId()+"");
		if(num > 0){
			num = 3;
		}else {
			num = typeDao.updateInfo(bean);
		}
		return num;
	}

	@Override
	public SysType gettyMsgListOne(SysType o) {
		return typeDao.gettyMsgListOne(o);
	}

	@Override
	public List<CountImportBean> getStatisticalGroup(CountImportBean o) {
		o.setHospitalId(UserUtil.getLoginUser().getHospitalId());
		List<CountImportBean> statisticalGroupList =  typeDao.getStatisticalGroup(o);
		List<CountImportBean> finalList =  statisticalGroupList;
		List<CountImportBean> statisticalInfo =  typeDao.getStatisticalInfo(o);
		long time1 = System.currentTimeMillis();
		for (int i=0;i<statisticalInfo.size();i++) {
			String illnessType = statisticalInfo.get(i).getIllnessType();
			tag:for (int j=0;j<statisticalGroupList.size();j++) {
				String illnessType2 = statisticalGroupList.get(j).getIllnessType();
				if(illnessType.equals(illnessType2)){
					int num = finalList.get(j).getAbnormalNum();
					int upNum = finalList.get(j).getFortyJCUp();
					int downNum = finalList.get(j).getFortyJCDown();
					finalList.get(j).setAbnormalNum(++num);
					int old =  statisticalInfo.get(i).getOld();
					if(old<40){
						finalList.get(j).setFortyJCDown(++downNum);
					}else{
						finalList.get(j).setFortyJCUp(++upNum);
					}
					break tag;
				}

			}

		}
		long time2 = System.currentTimeMillis();
		System.err.println("耗时时间："+(time2-time1));
		for (int i=0;i<finalList.size();i++){
			int thisNum = finalList.get(i).getThisNum();
			int abnormalNum = finalList.get(i).getAbnormalNum();
			String num = StringHelper.getPercent(abnormalNum,thisNum);
			finalList.get(i).setTotalDetectionRate(num);
		}

		return finalList;
	}


	@Override
	public List<MapBean> getPositiveDisease(MapBean o) {
		return typeDao.getPositiveDisease(o);
	}

	public List<CountImportBean> SexAnomalyIndexStatistics(CountImportBean o) {
		o.setHospitalId(UserUtil.getLoginUser().getHospitalId());
		List<CountImportBean> statisticalGroupList =  typeDao.getStatisticalGroup(o);
		List<CountImportBean> finalList =  statisticalGroupList;
		List<CountImportBean> statisticalInfo =  typeDao.getStatisticalInfo(o);
		for (int i=0;i<statisticalInfo.size();i++) {
			String illnessType = statisticalInfo.get(i).getIllnessType();
			String sex =statisticalInfo.get(i).getSex();
			tag:for (int j=0;j<statisticalGroupList.size();j++) {
				String illnessType2 = statisticalGroupList.get(j).getIllnessType();
				String pSex = statisticalGroupList.get(j).getPSex();
				String ssex = "";
				boolean tf = false;
				if("0".equals(pSex)){
					ssex = "男";
				}else if("1".equals(pSex)){
					ssex = "女";
				}else if("2".equals(pSex)){
					tf = true;
				}
				//System.err.println("illnessType="+illnessType+" ,illnessType2="+illnessType2+" ,sex="+sex+" ,ssex="+ssex+" ,pSex="+pSex);
				if(illnessType.equals(illnessType2) && (ssex.equals(sex) || tf)){
					int num = finalList.get(j).getAbnormalNum();
					finalList.get(j).setAbnormalNum(++num);
					break tag;
				}

			}
		}
		for (int i=0;i<finalList.size();i++){
			int thisNum = finalList.get(i).getThisNum();
			int abnormalNum = finalList.get(i).getAbnormalNum();
			String num = StringHelper.getPercent(abnormalNum,thisNum);
			finalList.get(i).setTotalDetectionRate(num);
			finalList.get(i).setSex("0".equals(o.getSex())?"男":"女");
		}
		return finalList;
	}
}
