package com.bofeng.service;


import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.bofeng.dao.MsReportMapper;
import com.bofeng.dao.MsSuspectedMapper;
import com.bofeng.entity.MsReport;
import com.bofeng.entity.MsSuspected;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: xielianghe
 * @Date: 2020/2/4 15:44
 */

@Service
@Transactional(readOnly = true)
public class MsSuspectedService {

    @Autowired
    private MsSuspectedMapper msSuspectedMapper;
    @Autowired
    private MsReportMapper msReportMapper;


    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void addSuspected(String userName, Integer grender, Integer age, Integer familyStatus,
                             String statusDesp, Integer medical, BigDecimal temperature, Integer cough, Integer muscle,
                             Integer dyspnea, Integer chest, Integer fatigue, Integer diarrhea, Integer diarrheaNum, String others, Long userCreate) {
        Long reportId = 0L;
        //如果是当前日期 已经添加
        Date t = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String reportDate = df.format(t);
        List<MsReport> listReport = msReportMapper.selectByReportDate(reportDate, userCreate);
        Long houseId = msReportMapper.selectHouseIdByUserId(userCreate);
        if (listReport == null || listReport.size() == 0) {
            MsReport msReport = new MsReport();
            msReport.setReportId(IdWorker.getId());
            msReport.setHouseId(houseId);
            msReport.setSafetyNum(0);
            msReport.setSuspectedNum(0);
            msReport.setRemarks("");
            msReport.setReportDate(DateTime.now().toDate());
            msReport.setMsStatus(2);
            msReport.setUserCreate(userCreate);
            msReport.setTimeCreate(DateTime.now());
            msReport.setUserUpdate(userCreate);
            msReport.setTimeUpdate(DateTime.now());
            msReportMapper.insert(msReport);
            reportId = msReport.getReportId();
        } else {
            reportId = listReport.get(0).getReportId();
        }

        //添加家人
        MsSuspected msSuspected = new MsSuspected();
        msSuspected.setSuspectedId(IdWorker.getId());
        msSuspected.setReportId(reportId);
        msSuspected.setUserName(userName);
        msSuspected.setGrender(grender);//默认为家人
        msSuspected.setAge(age);
        msSuspected.setFamilyStatus(familyStatus);
        msSuspected.setStatusDesp(statusDesp);
        msSuspected.setTemperature(temperature);
        msSuspected.setCough(cough);
        msSuspected.setMuscle(muscle);
        msSuspected.setDiarrhea(dyspnea);
        msSuspected.setChest(chest);
        msSuspected.setFatigue(fatigue);
        msSuspected.setDiarrhea(diarrhea);
        msSuspected.setDiarrheaNum(diarrheaNum);
        msSuspected.setOthers(others);
        msSuspected.setUserCreate(userCreate);
        msSuspected.setTimeCreate(DateTime.now());
        msSuspected.setUserUpdate(userCreate);
        msSuspected.setTimeUpdate(DateTime.now());
        msSuspectedMapper.insert(msSuspected);

        //确证人数
        Integer isTrue = msReportMapper.selectSuspectedNum(reportId, medical);
        MsReport msReport = msReportMapper.selectById(reportId);
        if (msReport != null) {
            if (medical == 0)
                msReport.setSuspectedNum(isTrue);
            if (medical == 1)
                msReport.setSureNum(isTrue);
            msReport.setMsStatus(2);
            // msReport.setUserUpdate(userCreate);
            // msReport.setTimeUpdate(DateTime.now());
            msReportMapper.updateById(msReport);
        }

    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void deleteSuspected(Long suspectedId) {

        MsSuspected msSuspected = msSuspectedMapper.selectById(suspectedId);
        if (msSuspected != null) {
            msSuspectedMapper.deleteById(suspectedId);
            //确证人数
            Integer isTrue = msReportMapper.selectSuspectedNum(msSuspected.getReportId(), msSuspected.getMedical());
            MsReport msReport = msReportMapper.selectById(msSuspected.getReportId());
            if (msReport != null) {
                if (msSuspected.getMedical() == 0)
                    msReport.setSuspectedNum(isTrue);
                if (msSuspected.getMedical() == 1)
                    msReport.setSureNum(isTrue);
                if(isTrue==0)
                    msReport.setMsStatus(1);
                else
                    msReport.setMsStatus(2);
                msReport.setUserUpdate(msReport.getUserCreate());
                msReport.setTimeUpdate(DateTime.now());
                msReportMapper.updateById(msReport);
            }
        }
    }
}
