package com.ruoyi.loanpricing.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.loanpricing.domain.dto.CorporateLoanPricingCreateDTO;
import com.ruoyi.loanpricing.domain.dto.PersonalLoanPricingCreateDTO;
import com.ruoyi.loanpricing.domain.entity.LoanPricingWorkflow;
import com.ruoyi.loanpricing.domain.vo.LoanPricingWorkflowListVO;
import com.ruoyi.loanpricing.domain.vo.LoanPricingWorkflowVO;
import com.ruoyi.loanpricing.service.ILoanPricingWorkflowService;
import com.ruoyi.loanpricing.service.LoanPricingCustomerMapService;
import com.ruoyi.loanpricing.service.LoanRateHistoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 利率定价流程Controller
 *
 * @author ruoyi
 * @date 2025-01-19
 */
@Tag(name = "利率定价流程管理")
@RestController
@RequestMapping("/loanPricing/workflow")
public class LoanPricingWorkflowController extends BaseController
{
    @Autowired
    private ILoanPricingWorkflowService loanPricingWorkflowService;

    @Autowired
    private LoanPricingCustomerMapService customerMapService;

    @Autowired
    private LoanRateHistoryService loanRateHistoryService;

    /**
     * 发起个人客户利率定价流程
     */
    @Operation(summary = "发起个人客户利率定价流程", description = "用于个人客户的利率定价流程发起")
    @Log(title = "个人客户利率定价流程", businessType = BusinessType.INSERT)
    @PostMapping("/create/personal")
    public AjaxResult createPersonal(@Validated @RequestBody PersonalLoanPricingCreateDTO dto) {
        LoanPricingWorkflow result = loanPricingWorkflowService.createPersonalLoanPricing(dto);
        return success(result);
    }

    /**
     * 发起企业客户利率定价流程
     */
    @Operation(summary = "发起企业客户利率定价流程", description = "用于企业客户的利率定价流程发起")
    @Log(title = "企业客户利率定价流程", businessType = BusinessType.INSERT)
    @PostMapping("/create/corporate")
    public AjaxResult createCorporate(@Validated @RequestBody CorporateLoanPricingCreateDTO dto)
    {
        LoanPricingWorkflow result = loanPricingWorkflowService.createCorporateLoanPricing(dto);
        return success(result);
    }

    /**
     * 查询利率定价流程编辑数据
     */
    @Operation(summary = "查询利率定价流程编辑数据")
    @GetMapping("/{serialNum}/edit")
    public AjaxResult getEditInfo(
            @Parameter(description = "业务方流水号")
            @PathVariable("serialNum") String serialNum)
    {
        return success(loanPricingWorkflowService.selectEditableLoanPricingBySerialNum(serialNum));
    }

    /**
     * 编辑个人客户利率定价流程
     */
    @Operation(summary = "编辑个人客户利率定价流程")
    @Log(title = "个人客户利率定价流程", businessType = BusinessType.UPDATE)
    @PutMapping("/{serialNum}/personal")
    public AjaxResult updatePersonal(
            @Parameter(description = "业务方流水号")
            @PathVariable("serialNum") String serialNum,
            @Validated @RequestBody PersonalLoanPricingCreateDTO dto)
    {
        return success(loanPricingWorkflowService.updatePersonalLoanPricing(serialNum, dto));
    }

    /**
     * 编辑企业客户利率定价流程
     */
    @Operation(summary = "编辑企业客户利率定价流程")
    @Log(title = "企业客户利率定价流程", businessType = BusinessType.UPDATE)
    @PutMapping("/{serialNum}/corporate")
    public AjaxResult updateCorporate(
            @Parameter(description = "业务方流水号")
            @PathVariable("serialNum") String serialNum,
            @Validated @RequestBody CorporateLoanPricingCreateDTO dto)
    {
        return success(loanPricingWorkflowService.updateCorporateLoanPricing(serialNum, dto));
    }

    /**
     * 查询个人客户号映射
     */
    @Operation(summary = "查询个人客户号映射")
    @GetMapping("/customer-map/personal")
    public AjaxResult queryPersonalCustomerMap(@RequestParam("custId") String custId)
    {
        return success(customerMapService.queryPersonal(custId));
    }

    /**
     * 查询企业客户号映射
     */
    @Operation(summary = "查询企业客户号映射")
    @GetMapping("/customer-map/corporate")
    public AjaxResult queryCorporateCustomerMap(@RequestParam("custId") String custId)
    {
        return success(customerMapService.queryCorporate(custId));
    }

    /**
     * 查询历史贷款合同
     */
    @Operation(summary = "查询历史贷款合同")
    @GetMapping("/history-contract")
    public AjaxResult queryHistoryContract(@RequestParam("custIsn") String custIsn)
    {
        return success(loanRateHistoryService.query(custIsn));
    }

    /**
     * 解密历史流程客户名称和证件号码密文字段
     */
    @Anonymous
    @Operation(summary = "解密历史流程客户敏感字段")
    @PostMapping("/anonymous/decrypt-sensitive-fields")
    public AjaxResult decryptSensitiveFields()
    {
        return success(loanPricingWorkflowService.decryptWorkflowSensitiveFields());
    }

    /**
     * 查询利率定价流程列表
     */
    @Operation(summary = "查询利率定价流程列表")
    @GetMapping("/list")
    public TableDataInfo list(LoanPricingWorkflow loanPricingWorkflow)
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Page<LoanPricingWorkflowListVO> page = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        IPage<LoanPricingWorkflowListVO> result = loanPricingWorkflowService.selectLoanPricingPage(page, loanPricingWorkflow);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(result.getRecords());
        rspData.setTotal(result.getTotal());
        return rspData;
    }

    /**
     * 查询利率定价流程详情
     */
    @Operation(summary = "查询利率定价流程详情")
    @GetMapping("/{serialNum}")
    public AjaxResult getInfo(
            @Parameter(description = "业务方流水号")
            @PathVariable("serialNum") String serialNum)
    {
        LoanPricingWorkflowVO workflow = loanPricingWorkflowService.selectLoanPricingBySerialNum(serialNum);
        if (workflow == null)
        {
            return error("记录不存在");
        }
        return success(workflow);
    }

    /**
     * 设定执行利率
     */
    @Operation(summary = "设定执行利率")
    @Log(title = "利率定价流程", businessType = BusinessType.UPDATE)
    @PutMapping("/{serialNum}/executeRate")
    public AjaxResult setExecuteRate(
            @Parameter(description = "业务方流水号")
            @PathVariable("serialNum") String serialNum,
            @RequestBody Map<String, String> request) {
        String executeRate = request.get("executeRate");
        boolean success = loanPricingWorkflowService.setExecuteRate(serialNum, executeRate);
        return success ? success() : error("设定失败");
    }
}
