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

import com.uas.platform.b2b.erp.support.ErpBufferedLogger;
import com.uas.platform.b2b.model.NotExistOrders;
import com.uas.platform.b2b.service.NotExistOrderService;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.b2b.temporary.model.OrderType;
import com.uas.platform.core.logging.BufferedLoggerManager;
import com.uas.platform.core.model.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.util.List;

/**
 * 查询平台不存在的单据信息
 *
 * Created by hejq on 2018-06-19.
 */
@RequestMapping("/erp/notExistOrders")
@RestController
public class NotExistOrderController {

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

    @Autowired
    private NotExistOrderService orderService;

    /**
     * 获取平台不存在的单据信息,采购单明细
     */
    @RequestMapping(value = "/saleItem", method = RequestMethod.GET)
    public List<NotExistOrders> findNotExistSaleItem() {
        List<NotExistOrders> orders = orderService.findByEnUUAndStatusAndType(SystemSession.getUser().getEnterprise().getUu(),
                Status.NOT_UPLOAD.value(), OrderType.saleItem.name());
        logger.log("查询未上传订单", "查询未上传采购单明细", orders.size());
        return orders;
    }

    /**
     * 根据返回id更新明细下载状态
     *
     * @throws Exception
     */
    @RequestMapping(value = "/back", method = RequestMethod.POST)
    @ResponseBody
    public void updateNotExistSaleItemStatus(@RequestParam("data") String data) throws Exception {
        orderService.updateDownloadStatus(URLDecoder.decode(data, "UTF-8").split(","));
        logger.log("采购订单", "ERP获取平台未上传单据返回更新下载状态", URLDecoder.decode(data, "UTF-8").split(",").length);
    }

    /**
     * 获取平台不存在的单据信息,采购单明细
     */
    @RequestMapping(value = "/saleMain", method = RequestMethod.GET)
    public List<NotExistOrders> findNotExistSaleMain() {
        List<NotExistOrders> orders = orderService.findByEnUUAndStatusAndType(SystemSession.getUser().getEnterprise().getUu(),
                Status.NOT_UPLOAD.value(), OrderType.saleItem.name());
        logger.log("查询未上传订单", "查询未上传采购单", orders.size());
        return orders;
    }

    /**
     * 获取平台不存在的单据信息,委外单明细
     */
    @RequestMapping(value = "/makeItem", method = RequestMethod.GET)
    public List<NotExistOrders> findNotExistMakeItem() {
        List<NotExistOrders> orders = orderService.findByEnUUAndStatusAndType(SystemSession.getUser().getEnterprise().getUu(),
                Status.NOT_UPLOAD.value(), OrderType.makeItem.name());
        logger.log("查询未上传订单", "查询未上传委外单明细", orders.size());
        return orders;
    }

    /**
     * 获取平台不存在的单据信息,采购单明细
     */
    @RequestMapping(value = "/makeMain", method = RequestMethod.GET)
    public List<NotExistOrders> findNotExistMakeMain() {
        List<NotExistOrders> orders = orderService.findByEnUUAndStatusAndType(SystemSession.getUser().getEnterprise().getUu(),
                Status.NOT_UPLOAD.value(), OrderType.saleItem.name());
        logger.log("查询未上传订单", "查询未上传委外单", orders.size());
        return orders;
    }
}
