package com.zd.system.controller;

import java.util.List;
import java.io.IOException;
import java.util.Optional;
import javax.servlet.http.HttpServletResponse;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.zd.common.core.utils.FileConfigUtils;
import com.zd.common.core.web.domain.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.system.domain.SysLogoConfig;
import com.zd.system.service.ISysLogoConfigService;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.zd.common.swagger.config.Knife4jConfiguration;

import com.zd.common.core.web.page.TableDataInfo;

/**
 * logo配置Controller
 *
 * @author zd
 * @date 2022-05-06
 */
@RestController
@Api(tags = "【logo配置】")
//作者手动加入 ，在接口上加作者@ApiOperationSupport(author = "XXX")
@ApiSupport(author = Knife4jConfiguration.Author.CYL)
@RequestMapping("/logo/config")
public class SysLogoConfigController extends BaseController
{
    @Autowired
    private ISysLogoConfigService sysLogoConfigService;
    @Autowired
    private FileConfigUtils fileConfigUtils;

    /**
     * 查询logo配置列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询logo配置列表")
    public TableDataInfo<SysLogoConfig> list(SysLogoConfig sysLogoConfig)
    {
        startPage();
        List<SysLogoConfig> list = sysLogoConfigService.selectSysLogoConfigList(sysLogoConfig);
        return getDataTable(list);
    }

    /**
     * 导出logo配置列表
     */
    @ApiOperation(value = "导出logo配置列表")
    @Log(title = "logo配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLogoConfig sysLogoConfig) throws IOException
    {
        List<SysLogoConfig> list = sysLogoConfigService.selectSysLogoConfigList(sysLogoConfig);
        ExcelUtil<SysLogoConfig> util = new ExcelUtil<SysLogoConfig>(SysLogoConfig.class);
        util.exportExcel(response, list, "logo配置数据");
    }

    /**
     * 获取logo配置详细信息
     */
    @ApiOperation(value = "获取logo配置详细信息")
    @GetMapping(value = "/{id}")
    public ResultData<SysLogoConfig> getInfo(@PathVariable("id") Long id)
    {
        return ResultData.success(sysLogoConfigService.selectSysLogoConfigById(id));
    }

    /**
     * 新增logo配置
     */
    @ApiOperation(value = "新增logo配置")
    @Log(title = "logo配置", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody SysLogoConfig sysLogoConfig)
    {
        return ResultData.result(sysLogoConfigService.insertSysLogoConfig(sysLogoConfig));
    }

    /**
     * 修改logo配置
     */
    @ApiOperation(value = "修改logo配置")
    @Log(title = "logo配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData edit(@RequestBody SysLogoConfig sysLogoConfig)
    {
        return  ResultData.result(sysLogoConfigService.updateSysLogoConfig(sysLogoConfig));
    }

    /**
     * 删除logo配置
     */
    @ApiOperation(value = "删除logo配置")
    @Log(title = "logo配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable Long[] ids)
    {
        return ResultData.result(sysLogoConfigService.deleteSysLogoConfigByIds(ids));
    }


    /**
     * 查询logo配置基本信息数据（漏过token验证）
     * 小程序 PC 使用
     */
    @GetMapping("/getLogoInfo")
    @ApiOperation(value = "查询logo配置列表")
    public ResultData<SysLogoConfig> getLogoInfo(SysLogoConfig sysLogoConfig)
    {
        List<SysLogoConfig> list = sysLogoConfigService.selectSysLogoConfigList(sysLogoConfig);
        if(list.size()>0){
            SysLogoConfig logoConfig = list.get(0);
            return ResultData.success(logoConfig);
        }
        return ResultData.success(new SysLogoConfig());
    }


    /**
     * 查询logo配置基本信息数据（漏过token验证）
     * 终端使用   完整路径
     */
    @GetMapping("/getConfInfo")
    @ApiOperation(value = "查询logo配置列表")
    public ResultData<SysLogoConfig> getConfInfo(SysLogoConfig sysLogoConfig)
    {
        List<SysLogoConfig> list = sysLogoConfigService.selectSysLogoConfigList(sysLogoConfig);
        if(list.size()>0){
            SysLogoConfig logoConfig = list.get(0);
            logoConfig.setCircularLogo(fileConfigUtils.getLocalUrl() + Optional.ofNullable(logoConfig.getCircularLogo()).orElse(""));
            logoConfig.setRectangleLogo(fileConfigUtils.getLocalUrl() + Optional.ofNullable(logoConfig.getRectangleLogo()).orElse(""));
            return ResultData.success(logoConfig);
        }
        return ResultData.success(new SysLogoConfig());
    }

    /**
     * 统一修改添加logo配置（漏过token验证）
     */
    @ApiOperation(value = "修改logo配置")
    @Log(title = "logo配置", businessType = BusinessType.UPDATE)
    @PutMapping("/updateLogoInfo")
    public ResultData updateLogoInfo(@RequestBody SysLogoConfig sysLogoConfig)
    {
        return  ResultData.result(sysLogoConfigService.updateSysLogoConfig(sysLogoConfig));
    }
}
