package com.jeeplus.business.payment.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.business.borrow.domain.BorrowMoney;
import com.jeeplus.business.borrow.service.dto.BorrowDTO;
import com.jeeplus.business.drive.service.dto.DriveApplyDTO;
import com.jeeplus.business.finance.util.EasyPoiUtil;
import com.jeeplus.business.payment.domain.Payment;
import com.jeeplus.business.payment.service.PaymentService;
import com.jeeplus.business.payment.service.dto.PaymentDTO;
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 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;

@Api("付款管理")
@RestController
@RequestMapping(value = "/payment")
public class PaymentController {

    @Resource
    private PaymentService paymentService;


    /**
     * 付款管理列表
     */
    @ApiOperation(value = "付款管理列表")
    @GetMapping(value = "/findList")
    public ResponseEntity<IPage<PaymentDTO>> applyList(PaymentDTO paymentDTO, Page<PaymentDTO> page) throws Exception {
        IPage<PaymentDTO> pageList=paymentService.findPageList(paymentDTO,page);
        return ResponseEntity.ok(pageList);
    }

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

    /**
     * 新增或修改
     */
    @ApiOperation(value = "新增或修改")
    @PostMapping(value = "/saveForm")
    public ResponseEntity saveForm(@RequestBody PaymentDTO paymentDTO) throws Exception {
        String id=paymentService.save(paymentDTO);
        return ResponseUtil.newInstance().add("businessTable","jy_payment").add("businessId",id).ok("操作成功");
    }

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

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

    /**
     * 管理员修改
     */
    @ApiOperation(value = "管理员修改")
    @PostMapping(value = "/adminEditForm")
    public void adminEditForm(@RequestBody PaymentDTO paymentDTO){
        paymentService.adminEditForm(paymentDTO);
    }

    /**
     * 根据当前登录人查找借款信息
     */
    @ApiOperation(value = "根据当前登录人查找信息")
    @GetMapping(value = "listByUserId")
    public ResponseEntity<IPage<Payment>> listByUserId(Payment payment, Page<BorrowDTO> page) throws Exception {
        IPage<Payment> pageList=paymentService.listByUserId(payment,page);
        return ResponseEntity.ok(pageList);
    }

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

    }


}
