package com.dgtly.order.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dgtly.common.utils.MapDataUtil;
import com.dgtly.order.domain.OrderCustomers;
import com.dgtly.order.domain.OrderSalesDetail;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import com.dgtly.common.core.controller.BaseController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

/**
 * 销售目标与达成Controller
 * 
 * @author dgtly
 * @date 2020-07-16
 */
@Controller
@RequestMapping("/order/orderSalesDetail")
public class OrderSalesDetailController extends BaseController
{
    private String prefix = "order/orderSalesDetail";
    final private static String queryUrl = "http://192.168.0.41:8010";
    @Autowired
    private RestTemplate restTemplate;

    @Value(value = "${ruoyi.cloudPath}")
    private String cloudPath;

    @RequiresPermissions("order:orderSalesDetail:view")
    @GetMapping()
    public String orderSalesDetail() {
        return prefix + "/orderSalesDetail";
    }

    /**
     * 查询销售目标达成列表
     */
    @RequiresPermissions("order:orderSalesDetail:list")
    @PostMapping("/list")
    @ResponseBody
    public Object list(HttpServletRequest request)
    {
        //走网关
        String url = cloudPath+"order"+"/order/orderSalesDetail/list";
        //本地测试 不走网关
        //String url = queryUrl + "/order/orderSalesDetail/list";
        String str = "";
        try {
            //设置为form方式
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            MultiValueMap<String, Object> map = MapDataUtil.convertMultiValueMap(request);

            HttpEntity<Object> requestEntity
                    = new HttpEntity<>(map, headers);
            str = restTemplate.postForObject(url ,requestEntity,String.class);
        } catch (Exception e){
            logger.error("查找目标达成列表接口报错:"+url,e);
            throw e;
        }


        return str;
    }

    /**
     * 查看销售目标达成详情
     */
    @GetMapping("/info/{id}")
    public String info(@PathVariable("id") Long id, ModelMap mmap)
    {
        //走网关
        String url = cloudPath+"order/order/orderSalesDetail/info/"+id;
        //本地测试 不走网关
        //String url = queryUrl + "/order/orderSalesDetail/info/"+id;
        try {
            String str = restTemplate.getForObject(url, String.class);
            OrderSalesDetail orderSalesDetail = JSONObject.toJavaObject(JSON.parseObject(str),OrderSalesDetail.class);
            mmap.put("orderSalesDetail", orderSalesDetail);
        } catch (Exception e){
            logger.error("查找目标达成详情接口报错:"+url,e);
            throw e;
        }
        return prefix + "/detail";
    }

    /**
     * 导出销售目标达成列表
     */
   /* @RequiresPermissions("OrderSalesDetail:detail:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OrderSalesDetail orderSalesDetail)
    {
        List<OrderSalesDetail> list = orderSalesDetailService.selectOrderSalesDetailList(orderSalesDetail);
        ExcelUtil<OrderSalesDetail> util = new ExcelUtil<OrderSalesDetail>(OrderSalesDetail.class);
        return util.exportExcel(list, "detail");
    }*/

    /**
     * 新增销售目标达成
     */
   /* @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }*/


    /**
     * 修改保存销售目标达成
     */
    /*@RequiresPermissions("OrderSalesDetail:detail:edit")
    @Log(title = "销售目标达成", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OrderSalesDetail orderSalesDetail)
    {
        return toAjax(orderSalesDetailService.updateOrderSalesDetail(orderSalesDetail));
    }*/

    /**
     * 删除销售目标达成
     */
    /*@RequiresPermissions("OrderSalesDetail:detail:remove")
    @Log(title = "销售目标达成", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(orderSalesDetailService.deleteOrderSalesDetailByIds(ids));
    }*/
}
