package com.uas.platform.b2b.erp.controller;

import com.uas.platform.b2b.erp.model.VendorPerformanceAssessErp;
import com.uas.platform.b2b.erp.service.VendorPerformanceAssessErpService;
import com.uas.platform.b2b.erp.support.ErpBufferedLogger;
import com.uas.platform.b2b.model.VendorPerformanceAssess;
import com.uas.platform.b2b.service.VendorPerformanceAssessService;
import com.uas.platform.core.logging.BufferedLoggerManager;
import com.uas.platform.core.util.serializer.FlexJsonUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.ResponseBody;

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

/**
 * 对ERP的供应商绩效考核接口
 * Created by 黄诚天 on 2017-10-12.
 */
@Controller
@RequestMapping("/erp/vendorPerformanceAssess")
public class VendorPerformanceAssessErpController {
    @Autowired
    private VendorPerformanceAssessErpService vpaErpService;

    @Autowired
    private VendorPerformanceAssessService vpaService;

    private final static ErpBufferedLogger logger = BufferedLoggerManager.getLogger(ErpBufferedLogger.class);

    /**
     * 将ERP的供应商绩效考核信息写到平台
     *
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public void saveVPAs(@RequestParam("data") String data) throws UnsupportedEncodingException {
        String jsonStr = URLDecoder.decode(data, "UTF-8");
        List<VendorPerformanceAssessErp> vpas = FlexJsonUtils.fromJsonArray(jsonStr, VendorPerformanceAssessErp.class);
        vpaService.save(vpaErpService.convertVPA(vpas));
        logger.log("供应商绩效考核信息", "上传供应商绩效考核信息", vpas.size());
    }

    /**
     * 将平台更新的ERP的供应商绩效考核信息回传回ERP
     *
     * @return
     */
    @RequestMapping(value = "/backtouas", method = RequestMethod.GET)
    @ResponseBody
    public List<VendorPerformanceAssessErp> getVPAs() {
        List<VendorPerformanceAssessErp> vpaErps = new ArrayList<VendorPerformanceAssessErp>();
        //暂不管回传
        List<VendorPerformanceAssess> vpas = null;
        if (!CollectionUtils.isEmpty(vpas)) {
            for (VendorPerformanceAssess vpa : vpas) {
                VendorPerformanceAssessErp vpaErp = new VendorPerformanceAssessErp();
                vpaErp.setVpa_gysqrdf(vpa.getVpaGysqrdf());
                vpaErp.setVpa_brkyy(vpa.getVpaBrkyy());
                vpaErp.setVpa_recordman(vpa.getVpaRecordman());
                vpaErp.setVpa_recorddate(vpa.getVpaRecorddate());
                vpaErps.add(vpaErp);
            }
        }
        return vpaErps;
    }

    /**
     * 更新下载成功的状态
     *
     * @param data
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "/refreshDownloadstatus", method = RequestMethod.POST)
    @ResponseBody
    public void refreshDownloadstatus(@RequestParam("data") String data) throws UnsupportedEncodingException {
        vpaService.onVPADownSuccess(URLDecoder.decode(data, "UTF-8").split(","));
    }


}
