WebCreditGrantingInfoController.java 13.3 KB
package com.lhcredit.project.webbusiness.controller;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.lhcredit.common.utils.DateUtils;
import com.lhcredit.common.utils.JsonPathUtils;
import com.lhcredit.common.utils.StringUtils;
import com.lhcredit.common.utils.http.HttpTemplate;
import com.lhcredit.common.utils.security.ShiroUtils;
import com.lhcredit.framework.aspectj.lang.enums.OperatorType;
import com.lhcredit.framework.web.page.TableDataInfo;
import com.lhcredit.framework.web.service.FastDFSClient;
import com.lhcredit.project.business.BasicInformation.service.BasicInformationService;
import com.lhcredit.project.business.TianYC.entity.param.RequestParams;
import com.lhcredit.project.business.creditGrantingInfo.domain.CreditGrantingInfoExcel;
import com.lhcredit.project.business.creditGrantingInfo.domain.HistoricalTransactions;
import com.lhcredit.project.business.creditGrantingRisk.domain.CreditGrantingRisk;
import com.lhcredit.project.business.creditGrantingRisk.service.ICreditGrantingRiskService;
import com.lhcredit.project.business.financeInfo.domain.FinanceInfo;
import com.lhcredit.project.business.financeInfo.service.IFinanceInfoService;
import com.lhcredit.project.business.frontUser.domain.FrontUserMon;
import com.lhcredit.project.business.reportDirectoryData.service.QxbHttpUtils;
import com.lhcredit.project.business.universalEvaluationMode.domain.UniversalEvaluationMode;
import com.lhcredit.project.business.universalEvaluationMode.service.IUniversalEvaluationModeService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.lhcredit.framework.aspectj.lang.annotation.Log;
import com.lhcredit.framework.aspectj.lang.enums.BusinessType;
import com.lhcredit.project.business.creditGrantingInfo.domain.CreditGrantingInfo;
import com.lhcredit.project.business.creditGrantingInfo.service.ICreditGrantingInfoService;
import com.lhcredit.framework.web.controller.BaseController;
import com.lhcredit.framework.web.domain.AjaxResult;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

import static com.lhcredit.common.utils.JsonPathUtils.*;


/**
 * 信用评级申请信息对外接口
 *
 * @author lhcredit
 * @date 2024-06-11
 */
@RestController
@RequestMapping("/web/creditGrantingInfo")
@Validated
public class WebCreditGrantingInfoController extends BaseController {
    @Autowired
    private ICreditGrantingInfoService creditGrantingInfoService;
    @Autowired
    private HttpTemplate httpTemplate;
    @Value("${dataCenters.url}")
    private String apiUrl;
    @Autowired
    private BasicInformationService basicInformationService;
    @Autowired
    private IFinanceInfoService financeInfoService;

    @Autowired
    private IUniversalEvaluationModeService evaluationModeService;
    @Autowired
    private ICreditGrantingRiskService creditGrantingRiskService;





    @RequestMapping("test")
    public void test(){
        List<CreditGrantingInfo> list = creditGrantingInfoService.selectCreditGrantingInfoList(null);
        list.stream().forEach(i->{creditGrantingRiskService.insert(i.getId(), i.getEname());});

    }


    @RequestMapping("getCreditResult")
    public AjaxResult getCreditResult(@NotNull(message = "id不能为空")Long cgId){
        CreditGrantingInfo creditGrantingInfo = creditGrantingInfoService.selectCreditGrantingInfoById(cgId);
        List<FinanceInfo> list = financeInfoService.selectFinanceInfoList(FinanceInfo.builder().cmId(cgId).build());
        creditGrantingInfo.setFinanceInfoList(list);
        //todo 根据id 查询通用指标
        List<UniversalEvaluationMode> evaluationModeList = evaluationModeService.getEvaluationModeListByCgid(cgId);
        creditGrantingInfo.setEvaluationModeList(evaluationModeList);
        return AjaxResult.success(creditGrantingInfo);
    }

    /**
     * 增加授信额度申请
     * @return
     */
    @RequestMapping("/creditLimitAddApplication")
    public AjaxResult creditLimitAddApplication(@RequestBody Object params){
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(params));
        CreditGrantingInfo creditGrantingInfo = creditGrantingInfoService.selectCreditGrantingInfoById(jsonObject.getLong("cgId"));
        if (jsonObject.getLong("creditLimit")<=creditGrantingInfo.getCreditLimitLast()){
            return AjaxResult.error("申请必须大于授信金额");
        }
        int i = creditGrantingInfoService.updateCreditGrantingInfo(CreditGrantingInfo.builder().id(jsonObject.getLong("cgId")).creditLimitAdd(jsonObject.getLong("creditLimit"))
                .creditLimitAddInfo(jsonObject.getString("info")).creditLimitAddAnnex(jsonObject.getString("annex")).build());
        return toAjax(i);
    }


    /**
     * 向下调整授信额度
     * @param cgId
     * @param limitSub
     * @return
     */
    @RequestMapping("/creditLimitSub")
    public AjaxResult creditLimitSub(@NotNull(message = "id不能为空") Long cgId,@NotNull(message = "分值不能为空") Double limitSub){
        CreditGrantingInfo creditGrantingInfo = creditGrantingInfoService.selectCreditGrantingInfoById(cgId);
        Long creditLimitLast = creditGrantingInfo.getCreditLimitLast();
        if (limitSub>=creditLimitLast)return AjaxResult.error("授信额度修改失败!");
        int i = creditGrantingInfoService.updateCreditGrantingInfo(CreditGrantingInfo.builder().id(cgId).creditLimitLast(new BigDecimal(limitSub).setScale(2,BigDecimal.ROUND_HALF_UP).longValue()).build());
        return toAjax(i);
    }

    /**
     * 查询信用评级申请列表接口
     */
    @ApiOperation("查询信用评级申请列表")
    @Log(title = "信用评级申请", businessType = BusinessType.LIST, operatorType = OperatorType.WEB)
    @RequestMapping("list")
    public Object list(String ename, Long creditLimitSt, Long creditLimitEd, String creditStatus,
                       String creditLv, Date creditTimeSt, Date creditTimeEd, Integer sort, Boolean outPut, HttpServletResponse response) {
        FrontUserMon userInfo = getUserInfo();

        if (outPut==false){
            startPage();
        }
        List<CreditGrantingInfo> list=creditGrantingInfoService.selectPageList(ename,creditLimitSt,creditLimitEd,creditStatus,creditLv,creditTimeSt,creditTimeEd,sort,userInfo.getOrgId());
        if (outPut){
            List<CreditGrantingInfoExcel> excelList=JSONArray.parseArray(JSON.toJSONString(list), CreditGrantingInfoExcel.class);
            com.lhcredit.common.utils.poi.ExcelUtil<CreditGrantingInfoExcel> util = new com.lhcredit.common.utils.poi.ExcelUtil<CreditGrantingInfoExcel>(CreditGrantingInfoExcel.class);
             util.exportExcel(excelList, "creditGrantingInfo",response);
                return "";
        }
        return toAjax(list);
    }


    @RequestMapping("genPdf")
    public AjaxResult genPdf(String name,Long cgId){
        String pdf = creditGrantingInfoService.genPdf(cgId);
        System.out.println(pdf);
        return AjaxResult.success(new HashMap<String,Object>(){{put("url",pdf);}});
    }


    @RequestMapping("getBasic")
    public AjaxResult getBasic(String name,Long id){
        RequestParams requestParams = new RequestParams();
        requestParams.setName(name);
        JSONObject jsonObject = basicInformationService.baseinfo2(requestParams);
        if (!jsonObject.getString("code").equals("2000")){
            return AjaxResult.error(jsonObject.getString("msg"));
        }
        CreditGrantingInfo creditGrantingInfo = new CreditGrantingInfo();
        FrontUserMon userInfo = getUserInfo();
         userInfo = new FrontUserMon();
        //--------初始化bean信息------------------------------------
        creditGrantingInfo.setEname(jsonPathString(jsonObject,"data.name"));
        JSONObject basicInfo=new JSONObject();
        basicInfo.put("establishDate",jsonPathString(jsonObject,"data.termStart"));
        basicInfo.put("legalPersonName", jsonPathString(jsonObject,"data.operName"));
        basicInfo.put("regCapital", jsonPathString(jsonObject,"data.registCapiNew"));
        basicInfo.put("base", jsonPathString(jsonObject,"data.base"));
        basicInfo.put("regLocation", jsonPathString(jsonObject,"data.address"));
        basicInfo.put("businessScope", jsonPathString(jsonObject,"data.scope"));
        basicInfo.put("regNumber", jsonPathString(jsonObject,"data.regNo"));
        basicInfo.put("industry", jsonPathString(jsonObject,"data.industryCode"));
        basicInfo.put("bondNum", jsonPathString(jsonObject,"data.bondNum"));
        creditGrantingInfo.setBasicInfo(basicInfo.toJSONString());
        //-------初始化财务信息-------------------------
//        List<FinanceInfo> list=financeInfoService.getReportSystemApiData(name,jsonPathString(jsonObject,"data.regLocation"),userInfo.getId(),userInfo.getOrgId());
//        creditGrantingInfo.setFinanceInfoList(list);
        return AjaxResult.success(creditGrantingInfo);
    }


    public static void main(String[] args) {

        Object object = QxbHttpUtils.sendHttpRequest("http://lhapi.lhdna.com/api/V4.0/baseinfo2?", new JSONObject() {{
            put("name", "义乌市大进百货有限公司");
        }});
        System.out.println(JSONObject.parseObject(object.toString()).getJSONObject("data").toJSONString());

    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody  CreditGrantingInfo creditGrantingInfo){
        System.out.println(JSON.toJSONString(creditGrantingInfo));
        if (null!=creditGrantingInfo.getId()){
            creditGrantingInfoService.deleteCreditGrantingInfoByIds(creditGrantingInfo.getId().toString());
        }
//        Object object = QxbHttpUtils.sendHttpRequest("http://lhapi.lhdna.com/api/V4.0/baseinfo2?", new JSONObject() {{
//            put("name", "义乌市大进百货有限公司");
//        }});
//        creditGrantingInfo.setBasicInfo(JSONObject.parseObject(object.toString()).getJSONObject("data").toJSONString());
        if (StringUtils.isNotEmpty(creditGrantingInfo.getCollateralList())&&creditGrantingInfo.getCollateralList().equals("null"))creditGrantingInfo.setCollateralList(null);
        if (StringUtils.isNotEmpty(creditGrantingInfo.getGuaranteeContract())&&creditGrantingInfo.getGuaranteeContract().equals("null"))creditGrantingInfo.setGuaranteeContract(null);
        FrontUserMon userInfo = getUserInfo();
//        FrontUserMon userInfo= new FrontUserMon();
//        userInfo.setId(27L);
//        userInfo.setOrgId(25L);
        creditGrantingInfo.setUserId(userInfo.getId());
        creditGrantingInfo.setOrgId(userInfo.getOrgId());

        //判断财务数据为空的情况不计算
        if (StringUtils.isEmpty(creditGrantingInfo.getFinanceInfoList())||creditGrantingInfo.getFinanceInfoList().size()==0){
            return AjaxResult.error("财务数据异常,平台不支持授信!");
        }
        //如果历史交易数据不为空,则解析
        if (StringUtils.isNotEmpty(creditGrantingInfo.getHistoricalTransactionsAnnex())){
            HistoricalTransactions historicalTransactions = changeHistoricalTransactionsAnnex(creditGrantingInfo.getHistoricalTransactionsAnnex());
            creditGrantingInfo.setHistoricalTransactions(null!=historicalTransactions?JSON.toJSONString(historicalTransactions):null);
        }
        creditGrantingInfo=creditGrantingInfoService.calculateCreditIndicators(creditGrantingInfo);
        creditGrantingInfo.setCreditLimitLast(creditGrantingInfo.getCreditLimit());
        List<FinanceInfo> financeInfoList = creditGrantingInfo.getFinanceInfoList();
        creditGrantingInfoService.updateCreditGrantingInfo(creditGrantingInfo);
        CreditGrantingRisk creditGrantingRisk=creditGrantingRiskService.insert(creditGrantingInfo.getId(), creditGrantingInfo.getEname());
        return toAjax(creditGrantingInfo.getId());
    }




    public HistoricalTransactions changeHistoricalTransactionsAnnex(String annex){
        try{
            String string = JSONObject.parseObject(annex).getString("url");
            URL url=new URL(string);
            InputStream inputStream = url.openStream();
            com.lhcredit.common.utils.poi.ExcelUtil<HistoricalTransactions> utils=new com.lhcredit.common.utils.poi.ExcelUtil<HistoricalTransactions>(HistoricalTransactions.class);
            List<HistoricalTransactions> historicalTransactions = utils.importExcel(inputStream);
            return historicalTransactions.stream().sorted(Comparator.comparing(HistoricalTransactions::getYear).reversed()).collect(Collectors.toList()).get(0);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}