package com.uas.finance.erp.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.uas.finance.constants.Status;
import com.uas.finance.erp.service.AccountApplyDownService;
import com.uas.finance.model.AccountApply;
import com.uas.finance.service.AccountApplyService;
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 2018/1/29
 **/
@RequestMapping("/erp/accountapply")
@RestController
public class AccountApplyDownController {

    @Autowired
    private AccountApplyDownService service;

    @Autowired
    private AccountApplyService accountApplyService;

    /**
     * 找到所有未下载的应收账款转让
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public ModelMap findUnload() {
        //获取英唐保理下的所有未下载应收账款单据
        List<AccountApply> accountApplies = accountApplyService.findByFauuAndErpstatus(10046945L, Status.NOT_UPLOAD.code());
        return service.covert(accountApplies);
    }

    /**
     * 更新应收账款的下载状态
     *
     * @param data
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "/back", method = RequestMethod.POST)
    public void updateErpstatus(@RequestParam("data") String data) throws UnsupportedEncodingException {
        String idStr = URLDecoder.decode(data, "UTF-8");
        JSONObject jsonObject = JSON.parseObject(idStr);
        Long id = jsonObject.getLong("id");
        service.updateErpStatus(id);
    }

}
