/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package com.unis.interactiveCommunication.modular.icLeaderMailbox.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.unis.common.util.SecurityCode;
import com.unis.common.util.SecurityImage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.binary.Base64;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.unis.common.annotation.CommonLog;
import com.unis.common.pojo.CommonResult;
import com.unis.common.pojo.CommonValidList;
import com.unis.interactiveCommunication.modular.icLeaderMailbox.entity.IcLeaderMailbox;
import com.unis.interactiveCommunication.modular.icLeaderMailbox.param.IcLeaderMailboxAddParam;
import com.unis.interactiveCommunication.modular.icLeaderMailbox.param.IcLeaderMailboxEditParam;
import com.unis.interactiveCommunication.modular.icLeaderMailbox.param.IcLeaderMailboxIdParam;
import com.unis.interactiveCommunication.modular.icLeaderMailbox.param.IcLeaderMailboxPageParam;
import com.unis.interactiveCommunication.modular.icLeaderMailbox.service.IcLeaderMailboxService;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 互动交流-领导信箱表控制器
 *
 * @author hjc
 * @date 2024/05/23 09:26
 */
@Api(tags = "互动交流-领导信箱表控制器")
@ApiSupport(author = "UNIS_TEAM", order = 1)
@RestController
@Validated
public class IcLeaderMailboxController {

    @Resource
    private IcLeaderMailboxService icLeaderMailboxService;

    /**
     * 获取互动交流-领导信箱表分页
     *
     * @author hjc
     * @date 2024/05/23 09:26
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("获取互动交流-领导信箱表分页")
    //@SaCheckPermission("/interactiveCommunication/icLeaderMailbox/page")
    @GetMapping("/interactiveCommunication/icLeaderMailbox/page")
    public CommonResult<Page<IcLeaderMailbox>> page(IcLeaderMailboxPageParam icLeaderMailboxPageParam) {
        return CommonResult.data(icLeaderMailboxService.page(icLeaderMailboxPageParam));
    }

    /**
     * 网站-添加领导信箱
     *
     * @author hjc
     * @date 2024/05/23 09:26
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("网站-添加领导信箱")
    @CommonLog("网站-添加领导信箱")
    //@SaCheckPermission("/interactiveCommunication/icLeaderMailbox/add")
    @PostMapping("/interactiveCommunication/icLeaderMailbox/add")
    public CommonResult<String> add(@RequestBody @Valid IcLeaderMailboxAddParam icLeaderMailboxAddParam) {
        icLeaderMailboxService.add(icLeaderMailboxAddParam);
        return CommonResult.ok();
    }

    /**
     * 回复互动交流-领导信箱表
     *
     * @author hjc
     * @date 2024/05/23 09:26
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation("回复互动交流-领导信箱表")
    @CommonLog("回复互动交流-领导信箱表")
    //@SaCheckPermission("/interactiveCommunication/icLeaderMailbox/edit")
    @PostMapping("/interactiveCommunication/icLeaderMailbox/edit")
    public CommonResult<String> edit(@RequestBody @Valid IcLeaderMailboxEditParam icLeaderMailboxEditParam) {
        icLeaderMailboxService.edit(icLeaderMailboxEditParam);
        return CommonResult.ok();
    }

    /*  *//**
     * 删除互动交流-领导信箱表
     *
     * @author hjc
     * @date 2024/05/23 09:26
     *//*
    @ApiOperationSupport(order = 4)
    @ApiOperation("删除互动交流-领导信箱表")
    @CommonLog("删除互动交流-领导信箱表")
    //@SaCheckPermission("/interactiveCommunication/icLeaderMailbox/delete")
    @PostMapping("/interactiveCommunication/icLeaderMailbox/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   CommonValidList<IcLeaderMailboxIdParam> icLeaderMailboxIdParamList) {
        icLeaderMailboxService.delete(icLeaderMailboxIdParamList);
        return CommonResult.ok();
    }*/

    /**
     * 获取互动交流-领导信箱表详情
     *
     * @author hjc
     * @date 2024/05/23 09:26
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation("获取互动交流-领导信箱表详情")
    //@SaCheckPermission("/interactiveCommunication/icLeaderMailbox/detail")
    @GetMapping("/interactiveCommunication/icLeaderMailbox/detail")
    public CommonResult<IcLeaderMailbox> detail(@Valid IcLeaderMailboxIdParam icLeaderMailboxIdParam) {
        return CommonResult.data(icLeaderMailboxService.detail(icLeaderMailboxIdParam));
    }


    /**
     * 网站-一页展示5条数据
     *
     * @author hjc
     * @date 2024/05/23 09:26
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation("网站-一页展示5条数据+更多详情页")
    //@SaCheckPermission("/interactiveCommunication/icLeaderMailbox/detail")
    @GetMapping("/interactiveCommunication/icLeaderMailbox/getSitePage")
    public CommonResult<Page<IcLeaderMailbox>> getSitePage(IcLeaderMailboxPageParam icLeaderMailboxPageParam) {
        return CommonResult.data(icLeaderMailboxService.getSitePage(icLeaderMailboxPageParam));
    }


    /**
     * 生成验证码图片, 并将验证码图片上传到服务器，然后将生成的验证码串值保存到session 中
     *
     * @param request
     **/
    @GetMapping(value = "/interactiveCommunication/icLeaderMailbox/getCode")
    @ApiOperation(value = "生成验证码图片", notes = "生成验证码图片")
    public CommonResult<Map<String, Object>> getCode(HttpServletRequest request, Integer fontSize, Integer width, Integer heigth) {

        // 图片流
        BufferedImage image;

        // 获取验证码数字字符串
        String yzm_code = SecurityCode.getSecurityCode(4, SecurityCode.SecurityCodeLevel.Hard, false).toLowerCase();
        request.getSession().setAttribute("Service_Code", yzm_code);
        // 生成验证码图片
        image = SecurityImage.createImage(yzm_code, fontSize, width, heigth);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "jpg", baos);
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        //将图片转换为base64位二进制编码返回
        String imgBase64 = new Base64().encodeToString(baos.toByteArray());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("file", imgBase64);
        map.put("code", yzm_code);
        return CommonResult.data(map);
    }

}
