package com.boco.nbd.wios.manage.controller;

import cn.hutool.core.lang.Assert;
import com.boco.nbd.cams.core.constant.MessageConstant;
import com.boco.nbd.wios.manage.contants.WiosConstant;
import com.boco.nbd.wios.manage.entity.bo.Dictionary;
import com.boco.nbd.wios.manage.entity.bo.InfoConfig;
import com.boco.nbd.wios.manage.service.impl.CommonService;
import com.boco.nbd.wios.manage.service.impl.DictionaryService;
import com.boco.nbd.wios.manage.service.impl.InfoConfigService;
import com.boco.nbd.wios.manage.util.FileUtil;
import com.ihidea.component.api.v2.BaseResponse;
import com.ihidea.core.support.exception.ServiceException;
import io.swagger.annotations.Api;
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.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 通用接口
 *
 * @author xgl
 * @version [版本号, 2020年9月17日]
 */
@RestController
@RequestMapping("api")
@Api(tags = "通用接口")
@ApiIgnore
public class CommonController {

    @Autowired
    private CommonService commonService;

    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private InfoConfigService infoConfigService;

    /**
     * 获取枚举内容
     *
     * @param enumNames
     * @return
     */
    @GetMapping(value = "enum/get")
    @ApiOperation(value = "获取枚举内容")
    @ApiImplicitParams({@ApiImplicitParam(name = "enumNames", value = "枚举类名", dataType = "String", paramType = "query", required = true),})
    public BaseResponse<Object> getEnums(String enumNames) {
        if (StringUtils.isEmpty(enumNames)) {
            throw new ServiceException(MessageConstant.MISSING_PARAM);
        }
        return new BaseResponse<>(commonService.getEnums(enumNames));
    }

    /**
     * 获取字典内容
     *
     * @param type
     * @return
     */
    @GetMapping(value = "dictionary/getByType")
    @ApiOperation(value = "获取字典内容")
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "类型", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "query", required = false),})
    public BaseResponse<List<Dictionary>> getDictionaryByType(String type, String name) {
        if (StringUtils.isEmpty(type)) {
            throw new ServiceException(MessageConstant.MISSING_PARAM);
        }
        return new BaseResponse<>(dictionaryService.selectValidByType(type, name));
    }

    /**
     * 添加字典内容
     *
     * @param type
     * @param name
     * @param value
     * @return
     */
    @PostMapping(value = "dictionary/add")
    @ApiOperation(value = "添加字典内容")
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "类型", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "value", value = "值", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "isSettle", value = "是否结算:0:否,1:是", dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "deviceLen", value = "设备编号长度(0:不限)", dataType = "int", paramType = "query", required = false),
    })
    public BaseResponse<Object> dicAdd(String type, String name, String value, Integer isSettle, Integer deviceLen) {
        Assert.notNull(type, "缺少type");
        Assert.notNull(name, "缺少name");
        Assert.notNull(value, "缺少value");
        Dictionary dic = new Dictionary();
        dic.setType(type);
        dic.setName(name);
        dic.setValue(value);
        dic.setIsSettle(isSettle);
        dic.setDeviceLen(deviceLen);
        List<Dictionary> list = dictionaryService.selectValidByValue(type, value);
        if (!list.isEmpty()) {
            throw new ServiceException("该字典值已存在");
        }
        dictionaryService.add(dic);
        return new BaseResponse<Object>();
    }

    /**
     * 根据id更新字典内容
     *
     * @param id
     * @param value
     * @return
     */
    @PostMapping(value = "dictionary/update")
    @ApiOperation(value = "更新字典内容")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "value", value = "值", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "isSettle", value = "是否结算:0:否,1:是", dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "deviceLen", value = "设备编号长度(0:不限)", dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "query", required = false),})
    public BaseResponse<Object> dicUpdate(String id, String value, String name, Integer isSettle, Integer deviceLen) {
        Assert.notNull(id, "缺少id");
        Assert.notNull(value, "缺少value");
        Dictionary dic = new Dictionary();
        dic.setId(id);
        dic.setValue(value);
        dic.setIsSettle(isSettle);
        dic.setDeviceLen(deviceLen);
        dic.setName(name);
        dictionaryService.update(dic);
        return new BaseResponse<Object>();
    }

    /**
     * 获取上传地址
     *
     * @return
     */
    @GetMapping(value = "getUploadUrl")
    @ApiOperation(value = "获取上传地址")
    public BaseResponse<String> getUploadUrl() {
        return new BaseResponse<String>(WiosConstant.FILE_UPLOAD_DEFAULT_URL);
    }

    /**
     * 下载模板文件
     *
     * @param response
     * @param fileName
     * @return
     * @throws Exception
     */
    @GetMapping(value = "getTemplateFile")
    @ApiOperation(value = "获取模板文件")
    @ApiImplicitParams({@ApiImplicitParam(name = "fileName", value = "文件名", dataType = "String", paramType = "query", required = true),})
    public void getTemplateFile(HttpServletResponse response, String fileName) throws Exception {
        FileUtil.downloadTemplate(response, fileName);
    }

    /**
     * 批量更新时效配置
     *
     * @param dicJson
     * @return
     */
    @PostMapping(value = "dictionary/timingConfigUpdate")
    @ApiOperation(value = "批量更新时效配置")
    @ApiImplicitParams({@ApiImplicitParam(name = "dicJson", value = "json数据", dataType = "String", paramType = "query", required = true),})
    public BaseResponse<Object> timingConfigUpdate(String dicJson) {
        Assert.notNull(dicJson, MessageConstant.MISSING_PARAM);
        List<Dictionary> list = Dictionary.jsonHandle(dicJson);
        dictionaryService.timingConfigUpdate(list);
        return new BaseResponse<>();
    }

    /**
     * 删除文件
     *
     * @param id
     * @return
     */
    @PostMapping(value = "uploadFile/delete")
    @ApiOperation(value = "删除文件")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "文件id", dataType = "int", paramType = "query", required = true),})
    public BaseResponse<Object> deleteFile(Integer id) {
        Assert.notNull(id, "缺少id");
        commonService.deleteUploadFile(id);
        return new BaseResponse<>();
    }

    /**
     * @return com.ihidea.component.api.v2.BaseResponse<java.lang.Object>
     * @Description 添加基本信息配置
     * @Param [type, name]
     * @author liwenxiang
     * @date 2021/7/24 13:41
     */
    @PostMapping(value = "infoConfig/add")
    @ApiOperation(value = "添加信息配置")
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "类型(0-安装紧急程度,1-首联人,2-邮箱)", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "值", dataType = "String", paramType = "query", required = true)})
    public BaseResponse<Object> infoConfigAdd(Integer type, String name) {
        infoConfigService.add(type, name);
        return new BaseResponse<Object>();
    }

    /**
     * @return com.ihidea.component.api.v2.BaseResponse<java.lang.Object>
     * @Description 编辑基本信息配置
     * @Param [type, name]
     * @author liwenxiang
     * @date 2021/7/24 14:05
     */
    @PostMapping(value = "infoConfig/update")
    @ApiOperation(value = "更新信息配置")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "配置信息id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "值", dataType = "String", paramType = "query", required = true)})
    public BaseResponse<Object> infoConfigUpdate(String id, String name) {
        infoConfigService.update(id, name);
        return new BaseResponse<Object>();
    }

    /**
     * @return com.ihidea.component.api.v2.BaseResponse<java.lang.Object>
     * @Description 删除基本信息配置
     * @Param [id]
     * @author liwenxiang
     * @date 2021/7/26 10:04
     */
    @PostMapping(value = "infoConfig/del")
    @ApiOperation(value = "删除基本信息配置")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "配置信息id", dataType = "String", paramType = "query", required = true)})
    public BaseResponse<Object> infoConfigUpdate(String id) {
        infoConfigService.delete(id);
        return new BaseResponse<Object>();
    }

    /**
     * @return
     * @Description 条件查询信息配置列表
     * @Param [type, name]
     * @author liwenxiang
     * @date 2021/7/26 9:50
     */
    @GetMapping(value = "infoConfig/getListByCondition")
    @ApiOperation(value = "获取配置信息列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "类型(0-安装紧急程度,1-首联人,2-邮箱)", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "值", dataType = "String", paramType = "query", required = false)})
    public BaseResponse<List<InfoConfig>> getInfoConfigListByCondition(Integer type, String name) {
        return new BaseResponse<>(infoConfigService.getInfoConfigListByCondition(type, name));
    }
}