package com.supplier.controller;

import com.supplier.model.Ignore;
import com.supplier.model.PrivateOrder;
import com.supplier.service.PrivateOrderService;
import com.supplier.util.ParsingToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 私人订制
 */
@RestController
@RequestMapping("privateOrder")
public class PrivateOrderController {

    @Autowired
    private PrivateOrderService privateOrderService;

    /**
     * 查询私人订制
     * @param privateOrder 参数
     * @return 返回结果
     */
    @GetMapping("getPrivateOrder")
    public Map<String, Object> getPrivateOrder(PrivateOrder privateOrder, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        try {
            //获取供应商code
            Map<String, Object> userMap = ParsingToken.tokenParsing(request);
            privateOrder.setCode((String) userMap.get("code"));

            int count = 0;
            List<PrivateOrder> list = new ArrayList<PrivateOrder>();
            if(privateOrder.getOrderType() == 0){
                list = privateOrderService.getPrivateOrderZeor(privateOrder);
            } else {
                list = privateOrderService.getPrivateOrder(privateOrder);
            }

            map.put("count", count);
            map.put("data", list);
            map.put("msg", "200");
        }catch (Exception e){
            map.put("msg", "500");
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 忽略
     * @param ignore 参数
     * @return 返回结果
     */
    @PostMapping("addIgnore")
    public Map<String, Object> addIgnore(Ignore ignore, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        try{
            //生成uuid
            String id = UUID.randomUUID().toString().replace("-", "");
            ignore.setUuid(id);
            //获取供应商code
            Map<String, Object> userMap = ParsingToken.tokenParsing(request);
            ignore.setCode((String) userMap.get("code"));

            privateOrderService.addIgnore(ignore);
            map.put("msg", "200");
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg", "500");
        }
        return map;
    }

    /**
     * 查询指定定制单内容
     * @param uuid 参数
     * @return 返回结果
     */
    @GetMapping("getPrivateOrderById")
    public Map<String, Object> getPrivateOrderById(String uuid){
        Map<String, Object> map = new HashMap<>();
        try{
            PrivateOrder privateOrder = privateOrderService.getPrivateOrderById(uuid);
            map.put("data", privateOrder);
            map.put("msg", "200");
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg", "500");
        }
        return map;
    }
}
