package com.jeeplus.business.borrow.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.domain.Refund;
import com.jeeplus.business.borrow.domain.RefundHis;
import com.jeeplus.business.borrow.service.BorrowService;
import com.jeeplus.business.borrow.service.dto.BorrowDTO;
import com.jeeplus.business.borrow.service.dto.RefundDTO;
import com.jeeplus.business.drive.service.dto.DriveApplyDTO;
import com.jeeplus.business.finance.util.EasyPoiUtil;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api("借款管理")
@RestController
@RequestMapping(value = "/borrow")
public class BorrowController {

    @Resource
    private BorrowService borrowService;



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

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

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

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

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

    /**
     * 创建还款数据
     */
    @ApiOperation(value = "创建还款数据")
    @PostMapping(value = "/createRefundData")
    public ResponseEntity<Map<String,String>> createReturnData(@RequestBody BorrowDTO borrowDTO) throws Exception{
        String refundId = borrowService.createReturnData(borrowDTO.getId());
        Map<String,String> res = new HashMap<String,String>();
        res.put("returnId", refundId);
        return ResponseEntity.ok(res);
    }

    /**
     * 根据还款id查询数据
     */
    @ApiOperation(value = "根据还款id查询数据")
    @GetMapping(value = "findByRefundId")
    public ResponseEntity<BorrowDTO> findByRefundId(@RequestParam String id){
        BorrowDTO borrowDTO=borrowService.findByRefundId(id);
        return ResponseEntity.ok(borrowDTO);
    }


    /**
     * 添加/修改借款归还
     */
    @ApiOperation(value = "新增或修改借款归还")
    @PostMapping(value = "/saveRefund")
    public ResponseEntity saveRefund(@RequestBody RefundDTO refundDTO) throws Exception {
        String id=borrowService.saveRefund(refundDTO);
        return ResponseUtil.newInstance().add("businessTable","jy_refund").add("businessId",id).ok("操作成功");
    }

    /**
     * 根据id修改状态status
     * @param
     */
    @ApiOperation(value = "根据id修改状态status")
    @PostMapping(value = "/updateRefundById")
    public void updateRefundById(@RequestBody RefundDTO refundDTO) {
        borrowService.updateRefundById(refundDTO);
    }


    /**
     * 管理员修改
     */
    @ApiOperation(value = "管理员修改")
    @PostMapping(value = "/adminEditForm")
    public void adminEditForm(@RequestBody BorrowDTO borrowDTO){
        borrowService.adminEditForm(borrowDTO);
    }


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


    /**
     * 根据借款id查询还款数据
     */
    @ApiOperation(value = "根据借款id查询还款数据")
    @GetMapping(value = "findByBorId")
    public ResponseEntity<List<Refund>> findByBorId(@RequestParam String id){
        List<Refund> borrowDTO=borrowService.findByBorId(id);
        return ResponseEntity.ok(borrowDTO);
    }


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


    /**
     * 根据还款历史id查询还款历史
     */
    @ApiOperation(value = "根据还款历史id查询还款历史")
    @GetMapping(value = "findHisById")
    public ResponseEntity<RefundHis> findHisById(@RequestParam String id){
        RefundHis refundHis=borrowService.findHisById(id);
        return ResponseEntity.ok(refundHis);
    }

    /**
     * 取消还款申请
     * @param id
     * @return
     */
    @ApiOperation(value = "取消还款申请")
    @GetMapping("/backSourceData")
    public ResponseEntity<Map<String, String>> backSourceData(String id){
        Map<String, String> map = borrowService.backSourceData(id);
        return ResponseEntity.ok(map);
    }

    /**
     * 根据还款id查询数据
     */
    @ApiOperation(value = "根据还款id查询数据")
    @GetMapping(value = "getByReId")
    public ResponseEntity<BorrowDTO> getByReId(@RequestParam String id){
        BorrowDTO borrowDTO=borrowService.getByReId(id);
        return ResponseEntity.ok(borrowDTO);
    }







}
