package com.jeeplus.business.reimbursement.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.business.drive.service.dto.DriveApplyDTO;
import com.jeeplus.business.finance.util.EasyPoiUtil;
import com.jeeplus.business.project.domain.JyProject;
import com.jeeplus.business.project.service.dto.JyProjectDTO;
import com.jeeplus.business.reimbursement.service.JyReimbursementInfoService;
import com.jeeplus.business.reimbursement.service.dto.JyReimbursementInfoDTO;
import com.jeeplus.common.excel.ExcelOptions;
import com.jeeplus.common.excel.annotation.ExportMode;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.flowable.feign.IFlowableApi;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.logging.constant.enums.LogTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
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;

@RestController
@Api(tags ="报销管理")
@RequestMapping(value = "/reimbursement")
public class JyReimbursementInfoController {

    @Resource
    private JyReimbursementInfoService jyReimbursementInfoService;

    @Resource
    private IFlowableApi flowTaskService;


    /**
     * 列表
     */
    @ApiOperation(value = "列表查询")
    @GetMapping("/list")
    public ResponseEntity<IPage<JyReimbursementInfoDTO>> list(Page<JyReimbursementInfoDTO> page, JyReimbursementInfoDTO dto) throws Exception{
        IPage<JyReimbursementInfoDTO> iPage = jyReimbursementInfoService.list(page, dto);
        iPage.getRecords().stream().forEach(i -> {
            // 报销审批
            if (StringUtils.isNotBlank(i.getTaskId()) && StringUtils.isNotBlank(i.getStatus())) {
                if ("2".equals(i.getStatus())) { // “审核中”的数据要获取数据审核人
                    i.setAuditUserIds(flowTaskService.getTaskAuditUsers(i.getTaskId()));  // 获取数据审核人
                }
            }
        });
        return ResponseEntity.ok(iPage);
    }


    /**
     * 新增/修改
     * @param dto
     * @return
     */
    @ApiOperation(value = "新增、修改")
    @PostMapping("/save")
    public ResponseEntity<String> save(@RequestBody JyReimbursementInfoDTO dto) throws Exception{
        String s = jyReimbursementInfoService.save(dto);
        return ResponseUtil.newInstance().add("businessTable", "jy_reimbursement_info")
                .add("businessId", s)
                .ok ("操作成功");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/remove")
    public ResponseEntity<String> remove(@RequestParam String id){
        String s = jyReimbursementInfoService.remove(id);
        return ResponseEntity.ok(s);
    }

    /**
     * 查询
     * @param id
     * @return
     */
    @ApiOperation(value = "查询")
    @GetMapping("/findById")
    public ResponseEntity<JyReimbursementInfoDTO> findById(@RequestParam String id) throws Exception{
        JyReimbursementInfoDTO dto = jyReimbursementInfoService.findById(id);
        return ResponseEntity.ok(dto);
    }

    /**
     * 根据id修改状态status
     */
    @ApiOperation(value = "根据id修改状态status")
    @PostMapping(value = "updateStatusById")
    public void updateStatusById(@RequestBody JyReimbursementInfoDTO dto) {
        jyReimbursementInfoService.updateStatusById(dto);
    }

    /**
     * 项目列表查询
     * @param ids
     * @return
     */
    @ApiOperation(value = "列表查询")
    @GetMapping("/queryByProIds")
    public ResponseEntity<List<JyProjectDTO>> queryByProIds(String ids){
        List<JyProjectDTO> jyProjects = jyReimbursementInfoService.queryByProIds(ids);
        return ResponseEntity.ok(jyProjects);
    }

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

    }








}
