package com.jeeplus.business.monthly.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.business.finance.util.EasyPoiUtil;
import com.jeeplus.business.inscription.service.dto.InscriptionDTO;
import com.jeeplus.business.monthly.service.MonthlyService;
import com.jeeplus.business.monthly.service.dto.MonthlyDTO;
import com.jeeplus.common.excel.ExcelOptions;
import com.jeeplus.common.excel.annotation.ExportMode;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.logging.constant.enums.LogTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Api(tags ="月报管理")
@RestController
@RequestMapping(value = "/monthly")
public class MonthlyController {

    @Resource
    private MonthlyService monthlyService;


    /**
     * 列表
     */
    @ApiOperation(value = "列表")
    @GetMapping(value = "/findList")
    public ResponseEntity<IPage<MonthlyDTO>> applyList(MonthlyDTO monthlyDTO, Page<MonthlyDTO> page) throws Exception {
        IPage<MonthlyDTO> pageList=monthlyService.findPageList(monthlyDTO,page);
        return ResponseEntity.ok(pageList);
    }

    /**
     * 用于feigh调用的列表
     */
    @GetMapping(value = "/getList")
    public String getList() throws Exception {
        List<MonthlyDTO> pageList=monthlyService.getList();
        return JSON.toJSONString(pageList);
    }

    /**
     * 根据id查找信息
     */
    @ApiOperation(value = "根据id查找根据id查找信息文印信息")
    @GetMapping(value = "findById")
    public ResponseEntity<MonthlyDTO> findById(@RequestParam String id){
        MonthlyDTO monthlyDTO=monthlyService.findById(id);
        return ResponseEntity.ok(monthlyDTO);
    }

    /**
     * 根据id进行删除
     */
    @ApiOperation(value = "根据id进行删除")
    @DeleteMapping(value = "delete")
    public String delete(@RequestParam String id){
        monthlyService.delete(id);
        return "操作成功";
    }


    /**
     * 修改或保存
     */
    @ApiOperation(value = "修改或保存")
    @PostMapping(value = "saveForm")
    public void saveForm(@RequestBody MonthlyDTO monthlyDTO){
        monthlyService.saveForm(monthlyDTO);
    }

    @ApiLog(value = "导出月报数据", type = LogTypeEnum.EXPORT)
    @GetMapping("export")
    @ApiOperation(value = "导出月报数据")
    public void exportFile(MonthlyDTO monthlyDTO, Page <MonthlyDTO> page, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename();
        String sheetName = options.getSheetName();
        List<MonthlyDTO> result = new ArrayList<>();
        if ( ExportMode.current.equals ( options.getMode() ) ) {
            result = monthlyService.findPageList (monthlyDTO,page).getRecords();
        } else if (ExportMode.selected.equals ( options.getMode() )) {
            result = monthlyService.findPageList (monthlyDTO,page).getRecords().stream ( ).filter ( item ->
                    options.getSelectIds ( ).contains ( item.getId ( ) )
            ).collect ( Collectors.toList ( ) );
        } else {
            page.setSize (-1);
            page.setCurrent (0);
            result = monthlyService.findPageList (monthlyDTO,page).getRecords();
        }
        EasyPoiUtil.exportExcel ( result, sheetName,  sheetName, MonthlyDTO.class, fileName, response );

    }

}
