package com.sunxung.factoring.web.cashflowmanage;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Joiner;
import com.sunxung.factoring.component.annotation.OperationLog;
import com.sunxung.factoring.component.enums.OperationTypeEnum;
import com.sunxung.factoring.entity.GridPage;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.cashflowmanage.request.AddCashFlowRequest;
import com.sunxung.factoring.entity.cashflowmanage.vo.CashFlowDealOutInfoVo;
import com.sunxung.factoring.entity.cashflowmanage.vo.CashFlowEntryInfoVo;
import com.sunxung.factoring.entity.cashflowmanage.vo.CashFlowManageBaseVo;
import com.sunxung.factoring.entity.cashflowmanage.vo.SearchCashFlowManageVo;
import com.sunxung.factoring.entity.entprise.Enterprise;
import com.sunxung.factoring.service.businessmanagement.BusinessBasicInfoService;
import com.sunxung.factoring.service.cashflowmanage.BankCardInfoSetService;
import com.sunxung.factoring.service.cashflowmanage.CashFlowManageBasicInfoService;
import com.sunxung.factoring.service.entprise.IEnterpriseService;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @Description : 现金流管理控制层
 * @Author : JiaYf
 * @Date: 2021-11-02 19:08
 */
@Controller
@RequestMapping("/cashFlow")
public class CashFlowManageController {

    @Autowired
    private CashFlowManageBasicInfoService cashFlowManageBasicInfoService;

    @Autowired
    private BusinessBasicInfoService businessBasicInfoService;

    @Autowired
    private BankCardInfoSetService bankCardInfoSetService;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private IEnterpriseService enterpriseService;

    @Value("${sop.url}")
    private String sopIp;

    /**
     * 分页查询 现金流管理信息
     *
     * @param searchCashFlowManageVo
     * @return
     */
    @GetMapping("/select")
    @ResponseBody
    public GridPage<CashFlowManageBaseVo> queryPage(SearchCashFlowManageVo searchCashFlowManageVo) {
        return cashFlowManageBasicInfoService.queryPage(searchCashFlowManageVo);
    }


    /**
     * 业务名称 列表查询
     *
     * @param customerId 买家名称ID
     * @return
     */
    @RequestMapping("getBusinessList")
    @ResponseBody
    public ResponseJson getBusinessList(Long customerId) {
        return new ResponseJson(businessBasicInfoService.getBusinessListByPurchaserId(customerId));
    }

    /**
     * 查询归属公司列表
     *
     * @param
     * @return
     */
    @RequestMapping("getCompanyBelong")
    @ResponseBody
    public ResponseJson getCompanyBelong() {
        return new ResponseJson(bankCardInfoSetService.getCompanyBelong());
    }

    /**
     * 根据所属公司查询银行名称列表
     *
     * @param companyBelong
     * @return
     */
    @RequestMapping("getBankName")
    @ResponseBody
    public ResponseJson getBankName(Long companyBelong) {
        return new ResponseJson(bankCardInfoSetService.getBankName(companyBelong));
    }

    /**
     * 查询银行卡号
     *
     * @param companyBelong 公司归属
     * @param bankName      银行名称
     * @return
     */
    @RequestMapping("getBankAccount")
    @ResponseBody
    public ResponseJson getBankAccount(Long companyBelong, String bankName) {
        return new ResponseJson(bankCardInfoSetService.getBankAccount(companyBelong, bankName));
    }

    /**
     * 添加入账信息
     *
     * @param addCashFlowRequest 入账信息
     * @return
     */
    @PostMapping("/doAdd")
    @ResponseBody
    @OperationLog(operationModule = "现金流管理", operationType = OperationTypeEnum.INSERT, description = "添加入账信息")
    public Object doAdd(@RequestBody AddCashFlowRequest addCashFlowRequest) {
        cashFlowManageBasicInfoService.add(addCashFlowRequest);
        return new ResponseJson("入账信息添加成功！");
    }

    /**
     * 入账信息页面 -获取企业列表
     *
     * @return
     */
    @GetMapping("/enterprises")
    @ResponseBody
    public Object getEnterprises() {
        List<Enterprise> list = enterpriseService.list();
        return new ResponseJson(list);
    }

    /**
     * 入账信息修改
     *
     * @param cashFlowEntryInfoVo
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/doUpdate")
    @ResponseBody
    @OperationLog(operationModule = "现金流管理", operationType = OperationTypeEnum.UPDATE, description = "入账信息修改")
    public Object doUpdate(CashFlowEntryInfoVo cashFlowEntryInfoVo, HttpServletRequest request) {
        cashFlowManageBasicInfoService.doUpdate(cashFlowEntryInfoVo, request);
        return new ResponseJson("入账信息更新完成!");
    }

    /**
     * 入账信息删除
     *
     * @param id
     * @return
     */
    @GetMapping("/doDelete")
    @ResponseBody
    @OperationLog(operationModule = "现金流管理", operationType = OperationTypeEnum.DELETE, description = "入账信息修改")
    public Object doDelete(Long id) {
        cashFlowManageBasicInfoService.delete(id);
        return new ResponseJson("删除成功!");
    }

    /**
     * 处理出账信息
     *
     * @param cashFlowDealOutInfoVo
     * @return
     */
    @RequestMapping("/doDeal")
    @ResponseBody
    @OperationLog(operationModule = "现金流管理", operationType = OperationTypeEnum.OTHER, description = "处理出账信息")
    public Object doDeal(CashFlowDealOutInfoVo cashFlowDealOutInfoVo) {
        cashFlowManageBasicInfoService.doDeal(cashFlowDealOutInfoVo);
        return new ResponseJson("出账处理成功!");
    }

    /**************************************************调用sop接口  start **********************************************************************************/
    /**
     * 查询  长银系 / 非长银系  资方选项
     *
     * @param code
     * @return
     */
    @RequestMapping("/dict/getByCode")
    @ResponseBody
    public Object do_getOption(String code) {

        String forObject = restTemplate.getForObject(sopIp + "/api/dict/getByCode?code=" + code,
                String.class);
        JSONArray array = JSONArray.parseArray(forObject);
        // List<Dictionary> byParentCode = dictionaryService.findByParentCode(code);
        return array;
    }


    /**
     * 查询授信状态为待生效和生效中的资方信息
     *
     * @return
     */
    @GetMapping(value = "/billingRecord/doFindByCreditStatus")
    @ResponseBody
    public Object do_findByCreditStatus() {

        String forObject = restTemplate.getForObject(sopIp + "/api/billingRecord/doFindByCreditStatus", String.class);
        JSONObject jsonObject = JSONObject.parseObject(forObject);
        return jsonObject;
    }


    /**
     * 根据所选公司归属查询银行名称
     *
     * @param companyOwnershipId
     * @return
     */
    @RequestMapping("/billingRecord/do_findBankName")
    @ResponseBody
    public Object do_findBankName(Long companyOwnershipId) {

        String forObject = restTemplate.getForObject(
                sopIp + "/api/billingRecord/do_findBankName?companyOwnershipId=" + companyOwnershipId, String.class);
        JSONArray jsonObject = JSONArray.parseArray(forObject);
        return jsonObject;
    }


    /**
     * 根据所选公司归属和银行名称查询银行卡号. <br/>
     *
     * @param companyOwnershipId
     * @param bankName
     * @return
     */
    @RequestMapping("/billingRecord/do_findBankCardNo")
    @ResponseBody
    public Object do_findBankCardNo(Long companyOwnershipId, String bankName,
                                    Long refinancingManagementId, Long businessType) {
        StringBuffer sb = new StringBuffer();
        sb.append(sopIp + "/api/billingRecord/do_findBankCardNo?");
        List<String> params = Lists.newArrayList();
        if (companyOwnershipId != null) {
            params.add("companyOwnershipId=" + companyOwnershipId);
        }
        if (StringUtils.isNotBlank(bankName)) {
            params.add("bankName=" + bankName);
        }
        if (refinancingManagementId != null) {
            params.add("refinancingManagementId=" + refinancingManagementId);
        }
        if (businessType != null) {
            params.add("businessType=" + businessType);
        }
        String join = Joiner.on("&").join(params);
        sb.append(join);
        String forObject = restTemplate.getForObject(sb.toString(), String.class);
        JSONArray jsonObject = JSONArray.parseArray(forObject);
        return jsonObject;
    }


    /**
     * 根据业务类型、业务类型编号查询资方银行名称和账号. <br/>
     *
     * @param refinancingManagementId 资方Id
     * @param businessType            业务类型
     * @param businessNumber          业务类型编号
     * @return
     */
    @RequestMapping(value = "/billingRecord/do_findCapitalBankCard")
    @ResponseBody
    public Object do_findCapitalBankCard(Long refinancingManagementId, Long businessType, Long businessNumber) {
        String forObject = restTemplate.getForObject(
                sopIp + "/api/billingRecord/do_findCapitalBankCard?refinancingManagementId=" + refinancingManagementId + "&businessType=" + businessType
                        + "&businessNumber=" + businessNumber, String.class);
        JSONArray jsonObject = JSONArray.parseArray(forObject);
        return jsonObject;
    }

    /**
     * 根据资方信息获取对应支用到期日
     *
     * @param refinancingManagementId 资方Id
     * @param businessType            业务类型
     * @param businessNumber          业务类型编号
     * @return
     */
    @RequestMapping(value = "/billingRecord/do_findExpirationDateByManagementId")
    @ResponseBody
    public Object do_findExpirationDateByManagementId(Long refinancingManagementId, Long businessType, Long businessNumber) {
        String forObject = restTemplate.getForObject(
                sopIp + "/api/billingRecord/do_findExpirationDateByManagementId?refinancingManagementId=" + refinancingManagementId + "&businessType=" + businessType
                        + "&businessNumber=" + businessNumber, String.class);
        JSONArray jsonObject = JSONArray.parseArray(forObject);
        return jsonObject;
    }


    /**
     * 根据所选公司归属和银行名称查询银行卡号. <br/>
     *
     * @param id
     * @return
     */
    @RequestMapping("/billingRecord/getById/{id}")
    @ResponseBody
    public Object do_getById(@PathVariable("id") Long id) {

        String forObject = restTemplate.getForObject(
                sopIp + "/api/billingRecord/getById/" + id, String.class);
        JSONObject jsonObject = JSONObject.parseObject(forObject);
        return jsonObject;
    }


    /**
     * 根据资方信息获取对应业务类型、业务类型编号
     *
     * @return
     */
    @GetMapping(value = "/billingRecord/doFindBusinessTypeByManagementId")
    @ResponseBody
    public Object do_doFindBusinessTypeByManagementId(Long refinancingManagementId) {
        String forObject = restTemplate.getForObject(
                sopIp + "/api/billingRecord/doFindBusinessTypeByManagementId?refinancingManagementId=" + refinancingManagementId, String.class);
        JSONObject jsonObject = JSONObject.parseObject(forObject);
        return jsonObject;
    }
    /**************************************************调用sop接口  end **********************************************************************************/


    /**
     * 入账详细信息
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/getEntryDetail/{id}")
    @ResponseBody
    public Object do_getEntryDetail(@PathVariable("id") Long id) {
        CashFlowEntryInfoVo entryDetail = cashFlowManageBasicInfoService.getEntryDetail(id);
        return entryDetail;
    }


    /**
     * 出账详细信息
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/getOutDetail/{id}")
    @ResponseBody
    public Object do_getOutDetail(@PathVariable("id") Long id) {
        CashFlowDealOutInfoVo dealOut = cashFlowManageBasicInfoService.getDealOut(id);
        return dealOut;
    }
}
