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 com.sun.org.apache.bcel.internal.generic.NEW;
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.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, String conditionStatus, Integer medical, String remarks) {
        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);
        if (listReport == null|| listReport.size()==0) {
            MsReport msReport = new MsReport();
            msReport.setReportId(IdWorker.getId());
            msReport.setHouseId(0L);
            msReport.setSafetyNum(0);
            msReport.setSuspectedNum(0);
            msReport.setRemarks("");
            msReport.setReportDate(DateTime.now().toDate());
            msReport.setMsStatus(0);
            msReport.setUserCreate(0L);
            msReport.setTimeCreate(DateTime.now());
            msReport.setUserUpdate(0L);
            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.setFamilyRelative(1);//默认为家人
        msSuspected.setConditionStatus(conditionStatus);
        msSuspected.setMedical(medical);
        msSuspected.setRemarks(remarks);
        msSuspected.setUserCreate(0L);
        msSuspected.setTimeCreate(DateTime.now());
        msSuspected.setUserUpdate(0L);
        msSuspected.setTimeUpdate(DateTime.now());
        msSuspectedMapper.insert(msSuspected);
    }
}
