package com.yiboshi.science.rest.v1;

import com.yiboshi.arch.base.ResponseDataModel;
import com.yiboshi.science.base.Pagination;
import com.yiboshi.science.config.security.SecurityUserHolder;
import com.yiboshi.science.entity.ComYear;
import com.yiboshi.science.entity.SelectListItem;
import com.yiboshi.science.param.dto.ComYearDTO;
import com.yiboshi.science.param.query.ComYearQueryVO;
import com.yiboshi.science.rest.BaseController;
import com.yiboshi.science.service.ComYearService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 功能：年度设置表 接口
 *
 * @author lkl
 * @version 2021-08-26
 */
@Api(tags = "com-year", description = "年度设置表")
@RestController
@RequestMapping("/v1/science-admin/com-year")
public class ComYearController extends BaseController<ComYearService, ComYearQueryVO, ComYearDTO, ComYear> {

    @Autowired
    private ComYearService comYearService;

    /**
     * 功能:分页查询
     *
     * @param vo 查询条件
     */
    @ApiOperation(value = "分页查询", httpMethod = "GET", notes = "根据参数获取列表")
    @GetMapping
    @RequestMapping("/getListByPage")
    public ResponseDataModel<Pagination<ComYearDTO>> getListByPage(@Validated ComYearQueryVO vo, BindingResult bindingResult) {
        vo.setUnitCode(SecurityUserHolder.getUnitCode());
        Pagination<ComYearDTO> page = comYearService.getListByPage(vo);
        return ResponseDataModel.ok(page);
    }

    /**
     * 添加/更新年度设置
     *
     * @param vo
     */
    @ApiOperation(value = "添加/更新年度设置", httpMethod = "POST", notes = "添加年度")
    @RequestMapping("/addOrUpdateYearInfo")
    @PostMapping
    public ResponseDataModel<ComYearDTO> addOrUpdateYearInfo(@RequestBody ComYear vo) {
        comYearService.addOrUpdateYearInfo(vo);
        return ResponseDataModel.ok();
    }

    /**
     * 功能:获取最大年度
     */
    @ApiOperation(value = "获取项目申报年度", httpMethod = "GET", notes = "获取项目申报年度")
    @GetMapping
    @RequestMapping("/getReportYear")
    public ResponseDataModel<Map<String, Object>> getReportYear() {
        Map<String, Object> data = new HashMap<String, Object>(1) {
            {
                put("reportYear", comYearService.getReportYear());
            }
        };
        return ResponseDataModel.ok(data);
    }

    /**
     * 功能:获取申报年度下拉列表
     */
    @ApiOperation(value = "获取申报年度下拉列表", httpMethod = "GET", notes = "获取申报年度下拉列表")
    @GetMapping
    @RequestMapping("/getYearSelectItem")
    public ResponseDataModel<List<SelectListItem>> getYearSelectItem() {
        Integer reportYear = comYearService.getReportYear();
        Integer year = reportYear.equals(0) ? Calendar.getInstance().get(Calendar.YEAR) : reportYear;
        List<SelectListItem> list = new ArrayList<>();
        SelectListItem item;
        for (Integer i = year; i > year - 10; i--) {
            item = new SelectListItem();
            item.setTitle(i + "年");
            item.setKey(i.toString());
            list.add(item);
        }
        return ResponseDataModel.ok(list);
    }

    @ApiOperation(value = "获取项目申报时间", httpMethod = "GET", notes = "获取项目申报时间")
    @GetMapping
    @RequestMapping("/getYearByTreeCode")
    public ResponseDataModel<ComYearDTO> getYearByTreeCode(int type) {
        ComYearDTO comYear = new ComYearDTO();
        Integer reportYear = 0;
        switch (type) {
            case 1:
                reportYear = comYearService.getReportYear();
                break;
            case 2:
                reportYear = comYearService.getTestYear();
                break;
            case 3:
                reportYear = comYearService.getCheckYear();
                break;
        }
        comYear = comYearService.getYearByTreeCode(SecurityUserHolder.getUnitCode(), reportYear.equals(0) ? Calendar.getInstance().get(Calendar.YEAR) : reportYear, type);
        return ResponseDataModel.ok(comYear);
    }

    @ApiOperation(value = "获取上级单位申报时间", httpMethod = "GET", notes = "获取上级单位申报时间")
    @GetMapping
    @RequestMapping("/getYearInfo")
    public ResponseDataModel<ComYearDTO> getYearInfo(int type) {
        return ResponseDataModel.ok(comYearService.getYearInfo(type));
    }
}