package com.lz.modules.app.controller;

import com.lz.common.utils.DateUtils;
import com.lz.common.utils.R;
import com.lz.modules.app.req.ResultRecordReq;
import com.lz.modules.sys.entity.app.ResultRecord;
import com.lz.modules.sys.service.app.ResultRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class GetDateController {


    @Autowired
    private ResultRecordService resultRecordService;

    /**
     * 信息
     */
    @RequestMapping("/get/date")
    public R getDate(ResultRecordReq req) throws Exception{
        ResultRecord resultRecord = resultRecordService.selectResultRecordById(req.getRecordResultId());
        SimpleDateFormat myFmt2 = new SimpleDateFormat("yyyy-MM");
        List<Map<String, String>> list = new ArrayList<>();
        List<String> contains = new ArrayList<>();
        String value = myFmt2.format(resultRecord.getMonthTime());
        Map<String, String> map = new HashMap<>();
        map.put("value", value);
        list.add(map);
        contains.add(value);
        for (int i = 0; i < 3; i++) {
            String val = myFmt2.format(DateUtils.addDateMonths(new Date(), i));
            if (!contains.contains(val)) {
                contains.add(val);
                Map<String, String> mapValue = new HashMap<>();
                mapValue.put("value", val);
                list.add(mapValue);
            }
        }
        for (int i = 0; i < 6; i++) {
            String val = myFmt2.format(DateUtils.addDateMonths(new Date(),-i));
            if(!contains.contains(val)){
                contains.add(val);
                Map<String,String> mapValue = new HashMap<>();
                mapValue.put("value",val);
                list.add(mapValue);
            }
        }
        return R.ok().put("list",list);
    }

    /**
     * 信息
     */
    @RequestMapping("/get/updateDate")
    public R updateDate(ResultRecordReq req) throws Exception {
        ResultRecord resultRecord = resultRecordService.selectResultRecordById(req.getRecordResultId());
        SimpleDateFormat myFmt2 = new SimpleDateFormat("yyyy-MM");
        resultRecord.setMonthTime(myFmt2.parse(req.getMonthTime()));
        resultRecordService.updateResultRecordById(resultRecord);
        return R.ok();
    }
}
