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.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: xielianghe
 * @Date: 2020/2/4 15:44
 */
@Service
@Transactional(readOnly = true)
public class MsReportService {

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

    public List<MsSuspected> getByReportId(Long reportId) {
        return msSuspectedMapper.selectByReportId(reportId);
    }

    public List<MsSuspected> getByReportReportDate(String reportDate, Long userCreate) {

        List<MsReport> listReport = msReportMapper.selectByReportDate(reportDate, userCreate);
        List<MsSuspected> listSuspected = null;
        if (listReport != null && listReport.size() > 0) {
            listSuspected = msSuspectedMapper.selectByReportId(Long.parseLong(listReport.get(0).getReportId().toString()));
        }
        return listSuspected;
    }

    public MsReport getReportByDateNow(Long userCreate) {
        Date t = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String reportDate = df.format(t);
        List<MsReport> listReport = msReportMapper.selectByReportDate(reportDate, userCreate);
        MsReport msReport = new MsReport();
        if (listReport != null && listReport.size() > 0) {
            msReport = msReportMapper.selectById(Long.parseLong(listReport.get(0).getReportId().toString()));
        }
        return msReport;
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void addReport(String safetyNum, Long userCreate) {
        //如果是当前日期 已经添加
        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(Integer.parseInt(safetyNum));
            msReport.setSuspectedNum(0);
            msReport.setRemarks("");
            msReport.setReportDate(DateTime.now().toDate());
            msReport.setReportStatus(1);
            msReport.setMsStatus(1);
            msReport.setUserCreate(userCreate);
            msReport.setTimeCreate(DateTime.now());
            msReport.setUserUpdate(userCreate);
            msReport.setTimeUpdate(DateTime.now());
            msReportMapper.insert(msReport);
        } else {
            //更新居家成员
            MsReport msReport = msReportMapper.selectById(listReport.get(0).getReportId());
            if (msReport != null) {
                List<MsSuspected> list= msSuspectedMapper.selectByReportId(msReport.getReportId());
                if(list==null || list.size()==0)
                    msReport.setMsStatus(1);
                else
                    msReport.setMsStatus(2);
                msReport.setReportStatus(1);
                msReport.setSafetyNum(Integer.parseInt(safetyNum));
                msReport.setUserUpdate(0L);
                msReport.setTimeUpdate(DateTime.now());
                msReportMapper.updateById(msReport);
            }
        }
    }
}
