package com.uas.finance.controller;

import com.alibaba.fastjson.JSONObject;
import com.uas.finance.model.RepaymentPlan;
import com.uas.finance.service.RepaymentPlanService;
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.RestController;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * 还款计划控制器
 * created by shicr on 2018/2/24
 **/
@RestController
@RequestMapping(value = "/repayment")
public class RepaymentPlanController {

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

    @Autowired
    private RepaymentPlanService service;

    /**
     * 获得UAS传来的某出账单下所有的还款计划
     *
     * @return
     */
    @RequestMapping(value = "/erp/getfromuas", method = RequestMethod.POST)
    public List<Map<String, Long>> getfromuas(String list) throws UnsupportedEncodingException {
        if (list != null) {
            list = URLDecoder.decode(list, "utf-8");
        }
        List<RepaymentPlan> list1 = JSONObject.parseArray(list, RepaymentPlan.class);
        logger.info("获得UAS传来的某出账单下所有的还款计划","success");
        return service.save(list1);
    }

    /**
     * 下载某出账单下的还款计划
     */
    @RequestMapping(value = "/downExcel")
    public ModelAndView downExcel(Long aaid) {
        Map<String, Object> model = new HashMap<>();
        logger.info("下载某出账单下的还款计划","success");
        model.put("repaylist", findByAaid(aaid));
        return new ModelAndView(new ViewExcel(), model);
    }

    /**
     * 查找还款计划
     * @param aaid
     * @return
     */
    private List<RepaymentPlan> findByAaid(Long aaid) {
        logger.info("查找还款计划");
        return service.findByAaid(aaid);
    }
}
