package com.steerinfo.dil.controller;

import com.alibaba.fastjson.JSON;
import com.steerinfo.dil.feign.ESFeign;
import com.steerinfo.dil.model.RmsGatepost;
import com.steerinfo.dil.service.impl.RmsGatepostServiceImpl;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

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

    @Autowired
    RmsGatepostServiceImpl rmsGatepostService;

    @Autowired
    RmsGatepostServiceImpl rmsGatepostRulesService;

    @Autowired
    ColumnDataUtil columnDataUtil;
    @Autowired
    ESFeign esFeign;

    /**
     * 获得门岗列表
     * @param mapValue
     * @param pageNum
     * @param pageSize
     * @param apiId
     * @return
     */
    @ApiOperation(value="获取列表", notes="分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer"),
         @ApiImplicitParam(name = "apiId", value = "347", required = false, dataType = "BigDecimal"),
    })
    //@RequiresPermissions("rmsgatepost:view")
    @PostMapping(value = "/getGatepostList")
    public RESTfulResult getGatepostList(@RequestBody(required = false) Map<String,Object> mapValue,
                                         Integer apiId,
                                         Integer pageNum,
                                         Integer pageSize,
                                         String con) throws ParseException {
        //框计算
        if (con != null) {
            if (!"undefined".equals(con)) {
                //设置要查询的索引名称
                String index = "get_gatepost_list";
                //获取查询结果
                return success(esFeign.getConResult(mapValue, index, apiId, pageNum, pageSize, con));
            }
        }
        //初始化过滤
        List<Map<String, Object>> listTotal = null;
        //如果有条件查询则跳过初始化,和创建索引
        if (mapValue.size() == 0) {
            //将查询结果存入索引中
            listTotal = rmsGatepostService.getGatepostList(null);
            Map<String, Object> map = new HashMap<>();
            //添加索引
            map.put("index", "get_gatepost_list");
            //添加id
            map.put("indexId", "gatepostId");
            listTotal.add(map);
            //新建索引
            String s = JSON.toJSONString(listTotal);
            esFeign.insertIndex(listTotal);
            //删除
            listTotal.remove(listTotal.size() - 1);
        }
        if (listTotal == null) {
            listTotal = rmsGatepostService.getGatepostList(mapValue);
        }

        PageHelper.startPage(pageNum, pageSize);
        //分页查询数据
        List<Map<String, Object>> columnList = rmsGatepostService.getGatepostList(mapValue);
        PageListAdd data = columnDataUtil.tableColumnData(apiId, listTotal, columnList);
        return success(data);
    }

    /**
     * 添加门岗信息
     * @param rmsGatepost
     * @return
     */
    @ApiOperation(value="创建", notes="根据RmsGatepost对象创建")
    @ApiImplicitParam(name = "rmsGatepost", value = "详细实体rmsGatepost", required = true, dataType = "RmsGatepost")
    //@RequiresPermissions("rmsgatepost:create")
    @PostMapping(value = "/insertGatepost")
    public RESTfulResult insertGatepost(@RequestBody RmsGatepost rmsGatepost)  {
        int result= rmsGatepostService.insertGatepost(rmsGatepost);
        if (result==-1){
            return failed();
        }
        return success(result);
    }

    /**
     * 根据id值更新门岗信息
     * @param mapValue
     * @return
     */
    @ApiOperation(value="更新详细信息", notes="根据url的id来指定更新对象,并根据传过来的rmsGatepost信息来更新详细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "BigDecimal"),
        @ApiImplicitParam(name = "rmsGatepost", value = "详细实体rmsGatepost", required = true, dataType = "RmsGatepost")
    })
    //@RequiresPermissions("rmsgatepost:update")
    @PostMapping(value = "updateGatepost", produces  = "application/json;charset=UTF-8")
    public RESTfulResult updateGatepost(LinkedHashMap<String, Object> mapValue) throws Exception{
        int result= rmsGatepostService.updateGatepost(mapValue);
        return success(result);
    }

    /**
     * 根据id删除对应的门岗信息
     * @param gatepostId
     * @return
     */
    @ApiOperation(value="删除", notes="根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "Short")
    //@RequiresPermissions("rmsgatepost:delete")
    @PostMapping(value = "/deleteGatepost/{gatepostId}")
    public RESTfulResult deleteGatepost(@PathVariable("gatepostId") BigDecimal gatepostId){
        int result=rmsGatepostService.deleteGatepost(gatepostId);
      return success(result);
    }
    /**
     * 根据id获取门岗规则详细信息
     */
    @ApiOperation(value="获取列表", notes="分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "apiId", value = "349", required = false, dataType = "BigDecimal"),
    })
    //@RequiresPermissions("rmsgatepost:view")
    @PostMapping(value = "/getGatepostRulesById/{gatepostId}")
    public RESTfulResult getGatepostRulesById(@PathVariable("gatepostId") BigDecimal gatepostId,
                                             Integer apiId) {
        List<Map<String, Object>> list= rmsGatepostService.getGatepostRulesById(gatepostId);
        //        将数据库中的进出类型进行格式转换输出
        for (Map<String, Object> map1:list){
            //        将数据库中的进出类型从整型转换为字符串
            if (map1.get("rulesGatepostEntityOutType")!=null){
                BigDecimal rule= (BigDecimal) map1.get("rulesGatepostEntityOutType");

                if (rule.intValue()==0){
                    map1.put("rulesGatepostEntityOutTypeStr","进");
                }
                if (rule.intValue()==1){
                    map1.put("rulesGatepostEntityOutTypeStr","出");
                }
            }
        }


        List<Map<String, Object>> columnList = list;
        System.out.println(columnList);
        PageListAdd data = columnDataUtil.tableColumnData(apiId, list, columnList);

        System.out.println(data);
        return success(data);
    }


    /**
     * 获取车辆类型Id
     */
    @GetMapping("/getVehicleTypeId")
    public RESTfulResult getVehicleTypeId(){
        return success(rmsGatepostService.getVehicleTypeId());
    }


    /**
     * 根据规则id删除对应的规则信息
     * @param rulesId
     * @return
     */
    @ApiOperation(value="删除", notes="根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "Short")
    //@RequiresPermissions("rmsgatepost:delete")
    @PostMapping(value = "/deleteGatepostRules/{rulesId}")
    public RESTfulResult deleteGatepostRules(@PathVariable("rulesId") BigDecimal rulesId){
        int result=rmsGatepostRulesService.deleteGatepostRules(rulesId);
        return success(result);
    }

    /**
     * 添加相对应的规则信息
     */
    @ApiOperation(value="创建", notes="根据mapValue对象创建")
    @ApiImplicitParam(name = "rmsGatepostRules", value = "详细mapValue", required = true, dataType = "mapValue")
    @PostMapping(value = "/insertGatepostRule")
    public RESTfulResult insertGatepostRule(@RequestBody(required = false) Map<String,Object> mapValue)  {
        int result= rmsGatepostRulesService.insertGatepostRule(mapValue);
        return success(result);
    }

    /**
     * 根据门岗id获取门岗名
     */
    @ApiOperation(value="查询", notes="根据url的id来指定查询")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "Short")
    //@RequiresPermissions("rmsgatepost:delete")
    @PostMapping(value = "/getGatepostName/{gatepostId}")
    public RESTfulResult getGatepostName(@PathVariable("gatepostId") BigDecimal gatepostId){
        return success(rmsGatepostService.getGatepostName(gatepostId));
    }
}