package com.dgtly.order.controller;





import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dgtly.common.core.domain.AjaxResult;
import com.dgtly.common.core.page.TableDataInfo;
import com.dgtly.common.utils.DateUtils;
import com.dgtly.common.utils.MapDataUtil;
import com.dgtly.common.utils.poi.ExcelUtil;
import com.dgtly.order.domain.CustomerAuthModel;
import com.dgtly.order.domain.OrderCustomers;
import com.dgtly.order.service.ICustomersStartService;
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;
import java.util.List;

/**
 * 经销商Controller
 * 
 * @author qxp
 * @date 2020-07-09
 */
@Controller
@RequestMapping("/order/customers")
public class OrderCustomersController extends BaseController
{
    private String prefix = "order/customers";

    @Autowired
    private ICustomersStartService customersStartService;


    @Autowired
    private RestTemplate restTemplate;

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

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


    @RequiresPermissions("order:customers:list")
    @PostMapping("/list")
    @ResponseBody
    public Object list(HttpServletRequest request)
    {
        String str="";
        String url = cloudPath + "order" + "/order/customers/list";
        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("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        String str = "";
        OrderCustomers orderCustomers = null;
        String url = cloudPath+"/order/order/customers/info/"+id;
        try{

            str = restTemplate.getForObject(url,String.class);
            orderCustomers =JSONObject.toJavaObject(JSON.parseObject(str),OrderCustomers.class);

        }catch (Exception e){
            logger.error("查找经销商详情报错"+url,e);
            throw e;
        }
        mmap.put("orderCustomers", orderCustomers);
        return prefix + "/edit";
    }

    @GetMapping("/dealHistoryData")
    @ResponseBody
    public Object dealHistoryData(String customerCode){
        String str="";
        String url = "";
        if (customerCode!=null&&!customerCode.equals("")){
             url = cloudPath +"order/order/meta-hana-sales-order/dealHistoryData?customerCode="+customerCode;
        }else {
             url = cloudPath +"order/order/meta-hana-sales-order/dealHistoryData";
        }
        try {
            //设置为form方式
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//            MultiValueMap<String, Object> map = MapDataUtil.convertMultiValueMap(null);
//            HttpEntity<Object> requestEntity
//                    = new HttpEntity<>(map, headers);
            str = restTemplate.getForObject(url, String.class);
        }catch (Exception e){
            logger.error("经销商历史数据处理接口报错:"+url,e);
            throw e;
        }
        JSONObject json = JSONObject.parseObject(str);
        return json;
    }


    /**
     * 返回经销商列表，根据进销商编号去重
     * @param customers
     * @return
     */
    @PostMapping("/customersList")
    @ResponseBody
    public TableDataInfo customersList(OrderCustomers customers)
    {
        customers.setIsDelete("0");
        startPage();
        List<OrderCustomers> usercustomersList = customersStartService.selectcustomersList(customers);
        return getDataTable(usercustomersList);
    }

    /**
     * 导出企微人员发送列表
     */
    @RequiresPermissions("order:customers:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export()
    {
        List<CustomerAuthModel> list = customersStartService.selectCustomerAuthList(DateUtils.getYearMonthStr());
        ExcelUtil<CustomerAuthModel> util = new ExcelUtil<CustomerAuthModel>(CustomerAuthModel.class);
        return util.exportExcel(list, "已认证经销商列表");
    }

}
