package com.sunyard.ssp.modules.sysconf.paramconf.controller;


import com.sunyard.chsm.model.entity.ParamConf;
import com.sunyard.ssp.common.Result;
import com.sunyard.ssp.common.annotation.AuditControllerLog;
import com.sunyard.ssp.modules.sysconf.paramconf.service.IParamConfService;
import com.sunyard.ssp.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fyc
 * @since 2020-03-17
 */
@Controller
@Slf4j
@Api(description = "系统配置-业务参数配置",tags ="系统配置-业务参数配置")
@RequestMapping("/sysconf/paramConf")
@Transactional
public class ParamConfController {

    @Autowired
    IParamConfService iParamConfService;




    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    @ResponseBody
    public Result<List<ParamConf>> getAll(){

        List<ParamConf> list = iParamConfService.list();
        return new ResultUtil<List<ParamConf>>().setData(list);
    }

    @RequestMapping(value = "/selectByItem",method = RequestMethod.GET)
    @ApiOperation(value = "根据类目获取参数(通讯参数-1，终端UI参数-2, 白名单参数-0)")
    @ResponseBody
    public Result<Map> selectByItem(@RequestParam Integer item){
        Map data = iParamConfService.selectByItem(item);
        return new ResultUtil<Map>().setData(data);
    }

    @RequestMapping(value = "/selectByKey",method = RequestMethod.GET)
    @ApiOperation(value = "根据key值获取数据")
    @ResponseBody
    public Result<ParamConf> selectByItem(@RequestParam String key){
        ParamConf data = iParamConfService.selectByKey(key);
        return new ResultUtil<ParamConf>().setData(data);
    }



    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "保存数据")
    @AuditControllerLog(description = "更新业务参数",operateType = "更新")
    public Result save(@RequestBody Map map){

        boolean result = iParamConfService.save(map);
        if(result){
            return new ResultUtil<>().setSuccessMsg("保存成功");
        }else {
            return new ResultUtil<>().setErrorMsg("保存失败");
        }


    }


    @RequestMapping(value = "/deleteById",method = RequestMethod.DELETE)
    @ResponseBody
    @ApiOperation(value = "通过id删除")
    public Result<Object> delAllByIds(@RequestParam String id){

        iParamConfService.removeById(id);
        return new ResultUtil<Object>().setSuccessMsg("删除数据成功");
    }


}
