package com.jeeplus.centrecareful.approvalInfo.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.centrecareful.approvalInfo.domain.ZSProgramProjectListInfo;
import com.jeeplus.centrecareful.approvalInfo.service.ZsReimbursementInfoService;
import com.jeeplus.centrecareful.approvalInfo.service.dto.*;
import com.jeeplus.centrecareful.approvalInfo.util.ZSEasyPoiUtil;
import com.jeeplus.common.excel.ExcelOptions;
import com.jeeplus.common.excel.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 com.jeeplus.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 王强
 * @create: 2022-11-24 16:44
 **/
@RestController
@Api(tags ="财务-报销申请")
@RequestMapping(value = "/zsReimbursement/info")
public class ZsReimbursementInfoController {

    @Resource
    private ZsReimbursementInfoService service;

    @Resource
    private IFlowableApi flowTaskService;

    /**
     * 报告号查询
     * @param dto
     * @return
     */
    @ApiOperation(value = "报告号查询")
    @GetMapping("/reportNoList")
    public ResponseEntity<IPage<ZsReportNoDto>> reportNoList(Page<ZsReportNoDto> page, ZsReportNoDto dto) throws Exception{
        IPage<ZsReportNoDto> iPage = service.getReportNoList(page, dto);
        return ResponseEntity.ok(iPage);
    }

    /**
     * 列表查询
     * @param dto/reimbursementApproval/type/bxList
     * @return
     */
    @ApiOperation(value = "列表查询")
    @GetMapping("/list")
    public ResponseEntity<IPage<ZsRetureListDto>> list(Page<ZsRetureListDto> page, ZsQueryListDto dto) throws Exception{
        IPage<ZsRetureListDto> iPage = service.list(page, dto);
        iPage.getRecords().stream().forEach(i -> {
            if (StringUtils.isNotBlank(i.getProcInsId()) && StringUtils.isNotBlank(i.getTaskId())) {
                i.setAuditUserIds(flowTaskService.getTaskAuditUsers(i.getTaskId()));  // 获取数据审核人
            }
        });
        return ResponseEntity.ok(iPage);
    }

    /**
     * 项目登记列表
     * @param info
     * @param page
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "项目登记列表")
    @GetMapping(value = "/projectList")
    public ResponseEntity<IPage<ZSProgramProjectListInfo>> projectList(ZSProgramProjectListInfo info, Page<ZSProgramProjectListInfo> page) throws Exception {
        IPage<ZSProgramProjectListInfo> list = service.projectList(page, info, "1");
        return ResponseEntity.ok (list);
    }

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

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

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

    @ApiOperation(value = "发票号判断")
    @GetMapping("/checkNumber")
    public ResponseEntity<Boolean> checkNumber(@RequestParam String number) {
        Boolean is = service.checkNumber(number);
        return ResponseEntity.ok(is);
    }

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

    @ApiOperation(value = "用户树形")
    @GetMapping(value = "/userTree")
    public ResponseEntity<List<ZsTreeUserDto>> userTree(@RequestParam String name) {
        List<ZsTreeUserDto> list = service.userTree(name);
        return ResponseEntity.ok(list);
    }



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

    }



    @ApiOperation(value = "关联报销")
    @GetMapping(value = "/relationReimbursementList")
    public ResponseEntity<IPage<ZsRetureListDto>> relationReimbursementList(Page<ZsRetureListDto> page, @RequestParam("id") String id) {
        IPage<ZsRetureListDto> listDtoIPage = service.relationReimbursementList(page, id);
        return ResponseEntity.ok(listDtoIPage);
    }

}
