package com.uas.finance.erp.controller;

import com.uas.finance.constants.Status;
import com.uas.finance.erp.service.FinancingApplyDownService;
import com.uas.finance.model.FinancingApply;
import com.uas.finance.service.FinancingApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
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 2017/12/26
 **/
@RequestMapping("/erp/finance")
@RestController
public class FinancingApplyDownController {

    @Autowired
    private FinancingApplyService financingApplyService;

    @Autowired
    private FinancingApplyDownService financingApplyDownService;

    /**
     * 获取未下载金融申请数据
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public ModelMap findApplyUnload() {
        List<FinancingApply> financingApplies = financingApplyService.findByFaenuuAndErpstatus(10046945L, Status.NOT_UPLOAD.code());
        return financingApplyDownService.covert(financingApplies);
    }

    /**
     * 下载成功后返回id更新下载状态
     *
     * @param data 封装的idStr数据
     */
    @RequestMapping(value = "/back", method = RequestMethod.POST)
    public void updateAfterCheck(@RequestParam("data") String data) throws UnsupportedEncodingException {
        String idStr = URLDecoder.decode(data, "UTF-8");
        financingApplyDownService.updateDownStatus(idStr);
    }
}
