package com.uas.finance.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.uas.finance.model.AccountApply;
import com.uas.finance.model.FinancingApply;
import com.uas.finance.model.RepaymentPlan;
import com.uas.finance.model.SellerQuota;
import com.uas.finance.service.AccountApplyService;
import com.uas.finance.service.RepaymentPlanService;
import com.uas.finance.service.SellerQuotaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 买方用户额度控制层
 * created by shicr on 2018/1/16
 **/
@RestController
@RequestMapping("/sellerquota")
public class SellerQuotaController {

    @Autowired
    private SellerQuotaService sellerQuotaService;

    @Autowired
    private AccountApplyService accountApplyService;

    @Autowired
    private RepaymentPlanService repaymentPlanService;

    private final static Logger logger = LoggerFactory.getLogger(SellerQuotaController.class);

    /**
     * 接收ERP传来的买方用户额度
     *
     * @param list
     * @return
     */
    @RequestMapping(value = "/erp/getfromuas", method = RequestMethod.POST)
    public List<SellerQuota> sellerQuotas(String list, Long faid, Integer status, Long access_id) throws UnsupportedEncodingException {

        if (list != null) {
            list = URLDecoder.decode(list, "utf-8");
        }
        List<SellerQuota> list1 = JSONObject.parseArray(list, SellerQuota.class);
        logger.info("接收ERP传来的买方用户额度");
        return sellerQuotaService.getSeller(list1, faid, status, access_id);
    }

    /**
     * 还款更新买方客户可用额度以及修改还款状态
     *
     * @param sellerQuota
     * @return
     */
    @RequestMapping(value = "/erp/updateQuota", method = RequestMethod.POST)
    public SellerQuota updateQuota(String sellerQuota,Long aaid,String iscloseoff,String repaymentPlans) throws UnsupportedEncodingException {
        if (sellerQuota != null) {
            sellerQuota = URLDecoder.decode(sellerQuota, "utf-8");
        }
        iscloseoff = URLDecoder.decode(iscloseoff);
        SellerQuota sellerQuota1 = JSON.parseObject(sellerQuota, SellerQuota.class);
        //更新买方用户可用额度
        logger.info("还款更新买方客户可用额度以及修改还款状态");
        SellerQuota sellerQuota2 = sellerQuotaService.updateQuota(sellerQuota1,aaid,iscloseoff);
        return sellerQuota2;
    }

    /**
     * 找到某客户下面的所有买方
     *
     * @param uu
     * @returns
     */
    @RequestMapping(value = "/findAll")
    public List<SellerQuota> findAll(@RequestParam(value = "enuu") Long uu) {
        logger.info("找到某客户下面的所有买方");
        return sellerQuotaService.findAll(uu);
    }

}
