package com.sunda.spmsweb.ordercontroller;


import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsorder.entity.OrderKeyValueConfig;
import com.sunda.spmsorder.service.IOrderKeyValueConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单请购码值配置信息表 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2020-12-22
 */
@RestController
@RequestMapping("/orderKVConfig")
@Api(tags="获取订单键值对配置",description = "获取订单键值对配置")
public class OrderKeyValueConfigController {

    @Autowired
    IOrderKeyValueConfigService iOrderKeyValueConfigService;

    @RequestMapping("/getAllConfig")
    @ApiOperation(value = "获取订单键值对配置", notes = "获取订单键值对配置", httpMethod = "GET")
//    @RequiresPermissions("orderKVConfig-getAllConfig")
    public ResponseResult getAllConfig(){
        try{
            return ResponseResult.success().add("kvList",iOrderKeyValueConfigService.getAllKVConfig());
        }catch(Exception e){
            return ResponseResult.error("获取数据失败");
        }
    }

    @RequestMapping("/getByFieldKey")
    @ApiOperation(value = "根据字段值获取键值对配置信息", notes = "根据字段值获取键值对配置信息\nfieldKey = projectCategory", httpMethod = "POST")
    public ResponseResult getByFieldKey(String fieldKey){
        try{
            return ResponseResult.success().add("kvList",iOrderKeyValueConfigService.getByFieldKey(fieldKey));
        }catch(Exception e){
            return ResponseResult.error("获取数据失败");
        }
    }

    @RequestMapping("/getKeyValueByKeyCode")
    @ApiOperation(value = "根据工厂代码获取项目合同号工厂编码", notes = "根据工厂代码获取项目合同号工厂编码\nkeyCode = BJ31" +
            "\nfieldKey = factoryProjectCode", httpMethod = "POST")
    public ResponseResult getKeyValueByKeyCode(@RequestParam String keyCode, @RequestParam String fieldKey){
        try{
            List<OrderKeyValueConfig> keyValueConfigs = iOrderKeyValueConfigService.getKeyValueByKeyCode(keyCode,fieldKey);
            for(int i = 0; i < keyValueConfigs.size(); i++) {
                String keyCodeStr = keyValueConfigs.get(i).getKeyCode();
                keyCodeStr = keyCodeStr.substring(keyCodeStr.indexOf("_")+1,keyCodeStr.length());
                keyValueConfigs.get(i).setKeyCode(keyCodeStr);
            }
            return ResponseResult.success().add("kvList",keyValueConfigs);
        }catch(Exception e){
            return ResponseResult.error("获取数据失败");
        }
    }

    @RequestMapping("/createKVConfig")
    @ApiOperation(value = "更新/新增键值对配置", notes = "更新/新增键值对配置\n" +
            "{\n" +
            "\t\"uuid\": \"06a6373233dd4f3b861af961f747524f\",\n" +
            "\t\"fieldKey\": \"orderWerksSPMSStatus\",\n" +
            "\t\"fieldName\": \"test工厂请购单SPMS状态\",\n" +
            "\t\"keyCode\": \"4\",\n" +
            "\t\"keyCodeDesc\": \"OA审批不通过\",\n" +
            "\t\"keyCodeEnDesc\": \"OA approval failed\",\n" +
            "\t\"enabled\": \"1\",\n" +
            "\t\"sortNo\": 1000,\n" +
            "\t\"remark\": null\n" +
            "}", httpMethod = "POST")
    public ResponseResult createKVConfig(@RequestBody JSONObject jsObject){
        try{
            return iOrderKeyValueConfigService.createKVConfig(jsObject);
        }catch(Exception e){
            return ResponseResult.error();
        }

    }

    @RequestMapping("/deleteKCConfig")
    @ApiOperation(value = "删除键值对配置", notes = "删除键值对配置", httpMethod = "POST")
    public ResponseResult deleteKCConfig(String uuid){
        try{
            return iOrderKeyValueConfigService.deleteKCConfig(uuid);
        }catch(Exception e){
            return ResponseResult.error();
        }

    }
}
