package com.example.controller;

import com.example.bean.DailyReport;
import com.example.bean.RespBean;
import com.example.bean.User;
import com.example.service.DailyReportService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dailyReport")
public class DailyReportController {
    @Autowired
    private DailyReportService dailyReportService;
    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public RespBean addDailyReport(@RequestBody DailyReport dailyReport)
    {
        dailyReport.setDate(new Timestamp(System.currentTimeMillis()));
        int result = dailyReportService.addDailyReport(dailyReport);
        if(result == 1)
        {
            if(dailyReport.getError() == 1)
                return new RespBean("success","有异常，请及时就医");
            else
                return new RespBean("success","添加成功");
        }
        else
            return new RespBean("error","添加失败");
    }

    @PostMapping("/update")
    public RespBean updateDailyReport(@RequestBody DailyReport dailyReport)
    {
        int result = dailyReportService.updateDailyReport(dailyReport);
        if(result == 1)
        {
            if(dailyReport.getError() == 1)
                return new RespBean("success","有异常，请及时就医");
            else
                return new RespBean("success","添加成功");
        }
        else
            return new RespBean("error","添加失败");
    }

    @GetMapping("/selectByUserid")
    public RespBean selectByUserid(String userid)
    {
        List<DailyReport> dailyReports = dailyReportService.selectByUserid(userid);
        RespBean respBean = new RespBean(dailyReports);
        for (DailyReport dailyReport: dailyReports) {
            if(dailyReport.getError() == 1)
                respBean.setStatus("error");
            else
                respBean.setStatus("success");
        }

        return respBean;
    }

    @GetMapping("/print")
    public RespBean print(String userid)
    {
        System.out.println(userid);
        User userByUserId = userService.getUserByUserId(userid);
        System.out.println(userByUserId);
        List<DailyReport> dailyReports = dailyReportService.selectByUserid(userid);
        DailyReport dailyReport = dailyReports.get(0);
        System.out.println(dailyReport);
        List<Object> objectList = new ArrayList<>();
        objectList.add(userByUserId);
        objectList.add(dailyReport);
        return new RespBean(objectList);
    }

}
