package com.jeeplus.business.humanResources.controller;

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.holiday.domain.Holiday;
import com.jeeplus.business.holiday.service.dto.HolidayDTO;
import com.jeeplus.business.humanResources.service.StaffDimissionService;
import com.jeeplus.business.humanResources.service.dto.StaffDimissionDTO;
import com.jeeplus.business.humanResources.service.dto.StaffHeirDTO;
import com.jeeplus.business.humanResources.service.dto.StaffUserInfoDTO;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Value;
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 = "/employeeDimission")
public class StaffDimissionController {

    @Resource
    private StaffDimissionService staffDimissionService;


    /**
     * 列表展示
     */
    @ApiOperation(value = "列表展示")
    @GetMapping(value = "/findPageList")
    public ResponseEntity<IPage<StaffDimissionDTO>> applyList(StaffDimissionDTO dimissionDTO, Page<StaffDimissionDTO> page) throws Exception {
        IPage<StaffDimissionDTO> pageList=staffDimissionService.findPageList(dimissionDTO,page);
        return ResponseEntity.ok(pageList);
    }


    /*
    根据id进行查询
     */
    @ApiOperation(value = "根据id查询数据")
    @GetMapping(value = "findById")
    public ResponseEntity<StaffDimissionDTO> findById(@RequestParam String id){
        StaffDimissionDTO dimissionDTO=staffDimissionService.findById(id);
        return ResponseEntity.ok(dimissionDTO);
    }

    /**
     * 新增或修改
     */
    @ApiOperation(value = "新增或修改")
    @PostMapping(value = "/saveForm")
    public ResponseEntity saveForm(@RequestBody StaffDimissionDTO dimissionDTO){
        String id=staffDimissionService.save(dimissionDTO);
        return ResponseUtil.newInstance().add("businessTable","staff_dimission").add("businessId",id).ok("操作成功");
    }


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

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

    /*
    根据id查询交接信息
     */
    @ApiOperation(value = "根据id查询数据")
    @GetMapping(value = "findHeirById")
    public ResponseEntity<StaffHeirDTO> findHeirById(@RequestParam String id){
        StaffHeirDTO staffHeirDTO=staffDimissionService.findHeirById(id);
        return ResponseEntity.ok(staffHeirDTO);
    }

    /**
     * 新增或修改交接申请
     */
    @ApiOperation(value = "新增或修改")
    @PostMapping(value = "/saveHeir")
    public ResponseEntity saveHeir(@RequestBody StaffHeirDTO staffHeirDTO){
        String id=staffDimissionService.saveHeir(staffHeirDTO);
        return ResponseUtil.newInstance().add("businessTable","staff_heir").add("businessId",id).ok("操作成功");
    }

    /**
     * 根据id修改状态status
     * @param
     */
    @ApiOperation(value = "根据id修改状态status")
    @PostMapping(value = "/updateHeirStatusById")
    public void updateHeirStatusById(@RequestBody StaffHeirDTO staffHeirDTO) {
        staffDimissionService.updateHeirStatusById(staffHeirDTO);
    }

    @ApiLog(value = "导出离职人员信息", type = LogTypeEnum.EXPORT)
    @GetMapping("export")
    //@PreAuthorize ("hasAuthority('cw_finance:invoice:export')")
    @ApiOperation(value = "导出离职人员信息")
    public void exportFile(StaffDimissionDTO dimissionDTO, Page <StaffDimissionDTO> page, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename();
        String sheetName = options.getSheetName();
        List<StaffDimissionDTO> result = new ArrayList<>();
        if ( ExportMode.current.equals ( options.getMode() ) ) {
            result = staffDimissionService.findPageList(dimissionDTO,page).getRecords();
        } else if (ExportMode.selected.equals ( options.getMode() )) {
            result = staffDimissionService.findPageList(dimissionDTO,page).getRecords().stream ( ).filter ( item ->
                    options.getSelectIds ( ).contains ( item.getId ( ) )
            ).collect ( Collectors.toList ( ) );
        } else {
            page.setSize (-1);
            page.setCurrent (0);
            result = staffDimissionService.findPageList(dimissionDTO,page).getRecords();
        }
        EasyPoiUtil.exportExcel ( result, sheetName,  sheetName, StaffDimissionDTO.class, fileName, response );

    }



}
