package com.uas.finance.erp.controller;

import com.uas.finance.service.CustomerInfoService;
import com.uas.finance.constants.Status;
import com.uas.finance.erp.service.CustomerInfoDownService;
import com.uas.finance.model.CustomerInfo;
import com.uas.finance.util.SystemSession;
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 hejq on 2017-12-21.
 */
@RequestMapping("/erp/customerInfo")
@RestController
public class CustomerInfoDownController {

    @Autowired
    private CustomerInfoService customerInfoService;

    @Autowired
    private CustomerInfoDownService customerInfoDownService;

    /**
     * 获取未下载客户资料数据
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public ModelMap findUnload() {
        List<CustomerInfo> infoList = customerInfoService.findByEnuuAndErpsatus(SystemSession.getUserAccount().getSpaceUU(), Status.NOT_UPLOAD.code());
        return customerInfoDownService.covert(infoList);
    }

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