package com.ruoyi.web.controller.loan;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SendSmsUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.loan.LoanApplication;
import com.ruoyi.system.domain.remind.WaitRemind;
import com.ruoyi.system.domain.review.ReviewComments;
import com.ruoyi.system.service.loan.ILoanApplicationService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.common.constant.CommonConstants.RZDB;

/**
 * 贷款申请主Controller
 *
 * @author boman
 * @date 2024-04-24
 */
@RestController
@RequestMapping("/application")
public class LoanApplicationController extends BaseController {
    @Autowired
    private ILoanApplicationService loanApplicationService;

    /**
     * 查询贷款申请主列表
     */
    //@PreAuthorize("@ss.hasPermi('system:application:list')")
    @GetMapping("/list")
    public TableDataInfo list(LoanApplication loanApplication) {
        startPage();
        List<LoanApplication> list = loanApplicationService.selectLoanApplicationList(loanApplication);
        return getDataTable(list);
    }


    /**
     * 查询需要保后的数据
     *
     * @param loanApplication
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:application:loanAfterList')")
    @GetMapping("/loanAfterList")
    public TableDataInfo loanAfterList(LoanApplication loanApplication) {
        startPage();
        List<LoanApplication> list = loanApplicationService.selectLoanAfterList(loanApplication);
        return getDataTable(list);
    }

    /**
     * 参会人员查看列表
     */
    @PreAuthorize("@ss.hasPermi('system:application:conference:list')")
    @GetMapping("/conference/list")
    public TableDataInfo conferenceList(LoanApplication loanApplication) {
        startPage();
        List<LoanApplication> list = loanApplicationService.conferenceList(loanApplication);
        return getDataTable(list);
    }

    /**
     * 导出贷款申请主列表
     */
    @PreAuthorize("@ss.hasPermi('system:application:export')")
    @Log(title = "贷款申请主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LoanApplication loanApplication) {
        List<LoanApplication> list = loanApplicationService.selectLoanApplicationList(loanApplication);
        ExcelUtil<LoanApplication> util = new ExcelUtil<LoanApplication>(LoanApplication.class);
        util.exportExcel(response, list, "贷款申请主数据");
    }

    /**
     * 获取贷款申请主详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:application:query')")
    @GetMapping(value = "/{loanApplicationId}")
    public AjaxResult getInfo(@PathVariable("loanApplicationId") Long loanApplicationId) {
        return loanApplicationService.selectLoanApplicationByLoanApplicationId(loanApplicationId);
    }

    /**
     * 获取贷款申请编号
     */
    @PostMapping("/getLoanApplicationNumber")
    public AjaxResult getLoanApplicationNumber() {
        //自定义项目编号 = RZDB + 时间戳 + 随机数
        String loanApplicationNumber = RZDB + DateUtils.getTodayChar() + SendSmsUtils.getCode(4);
        return AjaxResult.success("操作成功",loanApplicationNumber);
    }
    /**
     * 新增贷款申请主
     */
    @RepeatSubmit(interval = 1000, message = "请求过于频繁")
    @PreAuthorize("@ss.hasPermi('system:application:add')")
    @Log(title = "贷款申请主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LoanApplication loanApplication) {
        return toAjax(loanApplicationService.insertLoanApplication(loanApplication));
    }

    /**
     * 修改贷款申请主
     */
    @RepeatSubmit(interval = 1000, message = "请求过于频繁")
    @PreAuthorize("@ss.hasPermi('system:application:edit')")
    @Log(title = "贷款申请主", businessType = BusinessType.UPDATE)
    @PostMapping("/put")
    public AjaxResult edit(@RequestBody LoanApplication loanApplication) {
        return loanApplicationService.updateLoanApplication(loanApplication);
    }


    /**
     * 修改贷款申请主新只新增实际放款金额(万元)实际放款时间
     */
    @RepeatSubmit(interval = 1000, message = "请求过于频繁")
    @PreAuthorize("@ss.hasPermi('system:application:editActually')")
    @Log(title = "贷款申请主", businessType = BusinessType.UPDATE)
    @PostMapping("/putActually")
    public AjaxResult editActually(@RequestBody LoanApplication loanApplication) {
        return loanApplicationService.updateLoanApplicationActually(loanApplication);
    }

    /**
     * 删除贷款申请主
     */
    @PreAuthorize("@ss.hasPermi('system:application:remove')")
    @Log(title = "贷款申请主", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{loanApplicationIds}")
    public AjaxResult remove(@PathVariable Long[] loanApplicationIds) {
        return toAjax(loanApplicationService.deleteLoanApplicationByLoanApplicationIds(loanApplicationIds));
    }

    /**
     *暂存
     */
    @RepeatSubmit(interval = 1000, message = "请求过于频繁")
    @PreAuthorize("@ss.hasPermi('system:application:temporary')")
    @PostMapping("/temporary")
    public AjaxResult temporary(@RequestBody LoanApplication loanApplication) {
        return loanApplicationService.temporary(loanApplication);
    }

    /**
     * 7：合同签约 8：放款合规风险审核
     */
    @PreAuthorize("@ss.hasPermi('system:application:schedule')")
    @PostMapping("/schedule")
    public AjaxResult schedule(@RequestBody LoanApplication loanApplication) {
        return loanApplicationService.schedule(loanApplication);
    }
    /**
     * 审核
     */
    @PreAuthorize("@ss.hasPermi('system:application:sh')")
    @PostMapping("/sh")
    public AjaxResult sh(@RequestBody ReviewComments reviewComments) {
        return loanApplicationService.sh(reviewComments);
    }

    /**
     * 管理员同意上会审核按钮
     */
    @PreAuthorize("@ss.hasPermi('system:application:review')")
    @PostMapping("/review")
    public AjaxResult review(@RequestBody LoanApplication loanApplication) {
        return loanApplicationService.review(loanApplication);
    }


    /**
     * 申诉
     */
    @PreAuthorize("@ss.hasPermi('system:application:ss')")
    @PostMapping("/ss")
    public AjaxResult ss(@RequestBody LoanApplication loanApplication) {
        return loanApplicationService.ss(loanApplication);
    }
    /**
     * 撤销
     */
    @PreAuthorize("@ss.hasPermi('system:application:cx')")
    @PostMapping("/cx")
    public AjaxResult cx(@RequestBody LoanApplication loanApplication) {
        return loanApplicationService.cx(loanApplication);
    }

    /**
     * 归档
     */
    @PreAuthorize("@ss.hasPermi('system:application:gd')")
    @PostMapping("/gd")
    public AjaxResult gd(@RequestBody LoanApplication loanApplication) {
        return loanApplicationService.gd(loanApplication);
    }

    /**
     * 暂缓出具放款合规表-风险使用
     */
    @PreAuthorize("@ss.hasPermi('system:application:zanHuan')")
    @PostMapping("/zanHuan")
    public AjaxResult zanHuan(@RequestBody LoanApplication loanApplication) {
        return loanApplicationService.zanHuan(loanApplication);
    }

    /**
     * 管理员授权A角色开具放款通知书
     */
    @PreAuthorize("@ss.hasPermi('system:application:authorize')")
    @PostMapping("/authorize")
    public AjaxResult authorize(@RequestBody LoanApplication loanApplication) {
        return loanApplicationService.authorize(loanApplication);
    }

    /**
     * 导出模板附件
     */
    @PreAuthorize("@ss.hasPermi('system:export:mb')")
    @Log(title = "导出模板附件", businessType = BusinessType.DELETE)
    @PostMapping("/export/mb")
    public AjaxResult exportMb(@RequestBody LoanApplication loanApplication) {
        return loanApplicationService.exportMb(loanApplication);
    }

    /**
     * 文件管理列表
     * @param loanApplication
     * @return
     */
    @GetMapping("/listOss")
    @PreAuthorize("@ss.hasPermi('system:application:listOss')")
    public TableDataInfo listOss(LoanApplication loanApplication) {
        startPage();
        List<LoanApplication> list = loanApplicationService.listOss(loanApplication);
        return getDataTable(list);
    }
}
