package com.jeeplus.pubmodules.holiday.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.common.excel.ExcelOptions;
import com.jeeplus.common.excel.annotation.ExportMode;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.logging.constant.enums.LogTypeEnum;
import com.jeeplus.pubmodules.holiday.domain.Holiday;
import com.jeeplus.pubmodules.holiday.mapper.HolidayMapper;
import com.jeeplus.pubmodules.holiday.service.HolidayService;
import com.jeeplus.pubmodules.holiday.service.dto.HolidayDTO;
import com.jeeplus.pubmodules.holiday.utils.EasyPoiUtil;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
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 = "/holiday")
public class HolidayController {

    @Resource
    private HolidayService holidayService;

    @Resource
    private HolidayMapper holidayMapper;


    /**
     * 列表
     */
    @ApiOperation(value = "请假申请列表")
    @GetMapping(value = "/findList")
    public ResponseEntity<IPage<HolidayDTO>> applyList(HolidayDTO holidayDTO, Page<HolidayDTO> page) throws Exception {
        IPage<HolidayDTO> pageList=holidayService.findPageList(holidayDTO,page);
        return ResponseEntity.ok(pageList);
    }

    /**
     * 根据id查询数据
     */
    @ApiOperation(value = "根据id查询数据")
    @GetMapping(value = "findById")
    public ResponseEntity<HolidayDTO> findById(@RequestParam String id){
        HolidayDTO holidayDTO=holidayService.findById(id);
        return ResponseEntity.ok(holidayDTO);
    }

    /**
     * 当请假类型为年假时进行校验
     */
    @ApiOperation(value = "校验请假类型")
    @PostMapping(value = "checkType")
    public ResponseEntity checkType(@RequestBody HolidayDTO holidayDTO){
       String data= holidayService.checkType(holidayDTO);
       return ResponseEntity.ok(data);
    }


    /*
    新增或修改
     */
    @ApiOperation(value = "新增或修改")
    @PostMapping(value = "/saveForm")
    public ResponseEntity saveForm(@RequestBody HolidayDTO holidayDTO){
        Holiday dto=holidayService.save(holidayDTO);
        return ResponseUtil.newInstance().add("businessTable","jy_holiday").add("businessId",dto.getId()).add("days",dto.getDays()).ok("操作成功");
    }

    /**
     * 根据id修改状态status
     * @param
     */
    @ApiOperation(value = "根据id修改状态status")
    @PostMapping(value = "/updateStatusById")
    public void updateStatusById(@RequestBody HolidayDTO holidayDTO) {
        holidayService.updateStatusById(holidayDTO);
    }

    /**
     * 根据id进行删除
     */
    @ApiOperation(value = "根据id进行删除")
    @DeleteMapping(value = "/delete")
    public ResponseEntity<String> deleteById(@RequestParam String id){
        String s = holidayService.deleteById(id);
        return ResponseEntity.ok(s);
    }

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

    }



}
