package com.steerinfo.dil.controller;

import com.steerinfo.dil.feign.ColumnDataFeign;
import com.steerinfo.dil.feign.ESFeign;
import com.steerinfo.dil.service.impl.AmsDispatchSaleOrderServiceImpl;
import com.steerinfo.dil.util.BaseRESTfulController;
import com.steerinfo.dil.util.ColumnDataUtil;
import com.steerinfo.dil.util.PageListAdd;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import oracle.jdbc.proxy.annotation.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * AmsSaleOrder RESTful接口:
 * @author generator
 * @version 1.0-SNAPSHORT 2021-09-06 07:10
 * 类描述
 * 修订历史:
 * 日期:2021-09-06
 * 作者:generator
 * 参考:
 * 描述:AmsSaleOrder RESTful接口
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@RestController
@RequestMapping("/${api.version}/amsdispatchsaleorders")
public class AmsDispatchSaleOrderController extends BaseRESTfulController {

    @Autowired
    AmsDispatchSaleOrderServiceImpl amsDispatchSaleOrderService;
    @Autowired
    ColumnDataFeign columnDataFeign;
    @Autowired
    ColumnDataUtil columnDataUtil;
    @Autowired
    ESFeign esFeign;

    @ApiOperation(value="查询app端公开抢单")
    @PostMapping("/getDispatchSaleOrderList")
    public RESTfulResult getDispatchSaleOrderList(){
        List<Map<String, Object>> list = amsDispatchSaleOrderService.getDispatchSaleOrderList();
        return success(list);
    }

    @ApiOperation(value="查询app端已抢的销售订单")
    @PostMapping("/getLockDispatchSaleOrderList")
    public RESTfulResult getLockDispatchSaleOrderList(Integer carrierId){
        List<Map<String, Object>> list = amsDispatchSaleOrderService.getLockDispatchSaleOrderList(carrierId);
        return success(list);
    }

    @ApiOperation(value="抢单/接收订单锁定")
    @PostMapping("/lockDispatchSaleOrder")
    public RESTfulResult lockDispatchSaleOrder(@RequestParam Integer dispatchId,
                                               @RequestParam Integer carrierId) {
        int result = amsDispatchSaleOrderService.lockDispatchSaleOrder(new BigDecimal(dispatchId),new BigDecimal(carrierId));
        return success(result);
    }

    @ApiOperation(value="展示pc端司机已经抢到的订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "apiId(233)", value = "表头", required = false, dataType = "Interger")
    })
    @PostMapping("/selectDispatchSaleOrderList")
    public RESTfulResult selectDispatchSaleOrderList(@RequestBody(required=false) Map<String,Object> mapValue,
                                              Integer apiId,
                                              Integer pageNum,
                                              Integer pageSize,
                                              Integer carrierId){
        if (mapValue == null) {
            mapValue = new HashMap<>();
        }
        mapValue.put("carrierId",new BigDecimal(carrierId));
        List<Map<String, Object>> amsSaleOrder = amsDispatchSaleOrderService.getLockDispatchSaleOrderListForPc(mapValue);
        PageHelper.startPage(pageNum,pageSize);
        List<Map<String, Object>> amsSaleOrder1 = amsDispatchSaleOrderService.getLockDispatchSaleOrderListForPc(mapValue);
        PageListAdd pageList = columnDataUtil.tableColumnData(apiId, amsSaleOrder,amsSaleOrder1);
        return success(pageList);
    }

    @ApiOperation(value="查询PC端公开抢单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "apiId(242)", value = "表头", required = false, dataType = "Interger")
    })
    @PostMapping("/getPCDispatchSaleOrderList")
    public RESTfulResult getPCDispatchSaleOrderList(@RequestBody(required=false) Map<String,Object> mapValue,
                                                    Integer apiId,
                                                    Integer pageNum,
                                                    Integer pageSize,
                                                    Integer dispatchType,
                                                    String con){
        if (con != null) {
            if(!"undefined".equals(con)){
                String index="get_pc_dispatch";//设置要查询的索引名称
                return success(esFeign.getConResult(mapValue,index,apiId,pageNum,pageSize,con));//获取查询结果
            }
        }
        mapValue.put("dispatchType",dispatchType);
        List<Map<String, Object>> dispatchSaleOrderList = null;
        if (mapValue.size() == 1) {
            //将查询结果存入索引中
            dispatchSaleOrderList =  amsDispatchSaleOrderService.getPCDispatchSaleOrderList(mapValue);
            Map<String, Object> map1 = new HashMap<>();
            //添加索引
            map1.put("index","get_pc_dispatch");
            //添加id
            map1.put("indexId","dispatchId");
            dispatchSaleOrderList.add(map1);
            //新建索引
            esFeign.insertIndex(dispatchSaleOrderList);
            //删除
            dispatchSaleOrderList.remove(dispatchSaleOrderList.size()-1);
        }
        if (dispatchSaleOrderList == null) {
            dispatchSaleOrderList = amsDispatchSaleOrderService.getPCDispatchSaleOrderList(mapValue);
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Map<String, Object>> columnList = amsDispatchSaleOrderService.getPCDispatchSaleOrderList(mapValue);
        PageListAdd pageList = columnDataUtil.tableColumnData(apiId, dispatchSaleOrderList,columnList);
        return success(pageList);
    }

    @ApiOperation(value="销售新增定向派单且直接下发")
    @PostMapping("/addDispatchSaleOrder")
    public RESTfulResult addDispatchSaleOrder(@RequestBody Map<String,Object> mapValue) {
        int result = amsDispatchSaleOrderService.addDispatchSaleOrder(mapValue);
        return success(result);
    }

    /**
     * 展示销售派单物资信息
     * @param mapValue
     * @return
     */
    @ApiOperation(value="展示销售派单物资信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "map", value = "销售派单物资信息", required = false, dataType = "java.util.Map"),
            @ApiImplicitParam(name = "apiId", value = "419", required = false, dataType = "java.util.Map")
    })
    @PostMapping(value = "/getDispatchMaterialList")
    public RESTfulResult getDispatchMaterialList(@RequestBody(required = false) Map<String,Object> mapValue,
                                                Integer apiId,
                                                Integer pageNum,
                                                Integer pageSize,
                                                Integer dispatchId){
        if (mapValue == null) {
            mapValue.put("dispatchId",dispatchId);
        }
        List<Map<String, Object>> dispatchMaterialList = amsDispatchSaleOrderService.getDispatchMaterialList(mapValue);
        PageHelper.startPage(pageNum,pageSize);
        //分页数据
        List<Map<String, Object>> columnDataList = amsDispatchSaleOrderService.getDispatchMaterialList(mapValue);
        PageListAdd pageList = columnDataUtil.tableColumnData(apiId, dispatchMaterialList,columnDataList);
        return success(pageList);
    }


}