package com.ssyf.module.robot.controller.auth;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageInfo;
import com.ssyf.common.core.util.EntityUtil;
import com.ssyf.common.core.util.MapUtil;
import com.ssyf.common.web.aop.RequestSynchronization;
import com.ssyf.common.web.apicode.ApiCodeUser;
import com.ssyf.common.web.result.Result;
import com.ssyf.common.web.valid.ValidA;
import com.ssyf.module.robot.domain.entity.LiveRoomRelation;
import com.ssyf.module.robot.domain.entity.LiveRoomRelation;
import com.ssyf.module.robot.domain.query.LiveRoomRelationQuery;
import com.ssyf.module.robot.domain.query.LiveRoomRelationQuery;
import com.ssyf.module.robot.domain.vo.LiveRoomRelationVo;
import com.ssyf.module.robot.domain.vo.LiveRoomRelationVo;
import com.ssyf.module.robot.service.LiveRoomRelationService;
import com.ssyf.plugs.logger.Log;
import com.ssyf.plugs.mysql.helper.PageEnhanceHelper;
import com.ssyf.plugs.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
* com.ssyf.auth.entity.LiveRoomRelation模型开放接口
*
* @author: auth
* @date: 2022-12-12
*/
@Api(tags="基础-关联")
@RestController
@RequestMapping("/auth/LiveRoomRelation")
public class LiveRoomRelationAuthController {

    @Autowired
    private LiveRoomRelationService baseService;

    @Log(title = "机器人机器人-新增数据")
    @RequestSynchronization
    @ApiOperation(value = "新增数据", notes = "新增数据")
    @PostMapping(value = "/addOne")
    public Result<LiveRoomRelationVo> addOne(@Validated({ValidA.class}) @RequestBody LiveRoomRelationQuery query) {
        if(BeanUtil.isEmpty(query)){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR);
        }
        LiveRoomRelation menuLiveRoomRelation = this.baseService.addOne(query);
        return Result.success(BeanUtil.copyProperties(menuLiveRoomRelation , LiveRoomRelationVo.class));
    }
    
    @Log(title = "机器人机器人-批量新增数据")
    @RequestSynchronization
    @ApiOperation(value = "批量新增数据", notes = "批量新增数据")
    @PostMapping(value = "/addBatch")
    public Result<List<LiveRoomRelationVo>> addBatch(@Validated({ValidA.class}) @RequestBody LiveRoomRelationQuery  query) {
        if(CollectionUtil.isEmpty(query.getQueries())){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode(), "批量新增数据不能为空");
        }
        List<LiveRoomRelation> menuLiveRoomRelations = this.baseService.addBatch(query.getQueries());
        List<LiveRoomRelationVo> result = EntityUtil.copyProperties(menuLiveRoomRelations, LiveRoomRelationVo.class);
        return Result.success(result);
    }

    @Log(title = "机器人机器人-更新数据")
    @RequestSynchronization
    @ApiOperation(value = "更新数据", notes = "uid更新")
    @PostMapping(value = "/updateByUid/{uid}")
    public Result updateByUid(@PathVariable String uid , @Validated @RequestBody LiveRoomRelationQuery query){
        if(BeanUtil.isEmpty(query)){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"更新数据不能为空");
        }
        this.baseService.updateByUid(uid ,query);
        return Result.success();
    }

    @Log(title = "机器人机器人-删除数据")
    @RequestSynchronization
    @ApiOperation(value = "删除数据", notes = "uid删除")
    @PostMapping(value = "/removeByUid/{uid}")
    public Result removeByUid(@PathVariable String uid){
        this.baseService.removeByUid(uid);
        return Result.success();
    }

    @Log(title = "机器人机器人-批量删除")
    @RequestSynchronization
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @PostMapping(value = "/removeBatchByUid")
    public Result removeBatchByUid(@Validated @RequestBody LiveRoomRelationQuery query){
        this.baseService.removeBatchByUids(query.getUids());
        return Result.success();
    }

    @ApiOperation(value = "/查询指定数据" ,notes = "uid查询")
    @PostMapping(value = "/getOneByUid/{uid}")
    public Result<PageInfo<LiveRoomRelationVo>> getOneByUid(@PathVariable String uid) {
        return Result.success(BeanUtil.toBean(this.baseService.getOneByUid(uid) ,LiveRoomRelationVo.class));
    }

    @ApiOperation(value = "/查询分页数据", notes = "分页查询")
    @PostMapping(value = "/queryPage")
    public Result<PageInfo<LiveRoomRelationVo>> queryPage(@Validated @RequestBody LiveRoomRelationQuery query) {
        PageInfo<LiveRoomRelation> temp = this.baseService.getPage(query);
        return Result.success(PageEnhanceHelper.switchPageInfo(temp ,LiveRoomRelationVo.class));
    }

    @ApiOperation(value = "/查询所有数据")
    @PostMapping(value = "/queryList")
    public Result<PageInfo<LiveRoomRelationVo>> queryList(@Validated @RequestBody LiveRoomRelationQuery query) {
        List<LiveRoomRelation> menuLiveRoomRelationList = this.baseService.getList(query);
        List<LiveRoomRelationVo> t = EntityUtil.copyProperties(menuLiveRoomRelationList, LiveRoomRelationVo.class);
        return Result.success(t);
    }
    @ApiOperation(value = "/查询总数")
    @PostMapping(value = "/count")
    public Result<Integer> count(@Validated @RequestBody LiveRoomRelationQuery query) {
        Integer count = this.baseService.countBy(query);
        return Result.success(count);
    }

    private Map<String, String> excelTemplate() {
        Map<String, String> fieldMap = MapUtil.builder()
                .put("liveRoomId", "直播间id")
                .put("liveRoomName", "直播间名称")
                .put("anchorCode", "主播Id")
                .put("anchorName", "主播名称")
                .build();
        return fieldMap;
    }


    @ApiOperation(value = "导出数据", notes = "根据查询条件导出数据为Excel文件")
    @PostMapping(value = "/export", produces = "application/vnd.ms-excel")
    public void export(@Validated @RequestBody LiveRoomRelationQuery query, HttpServletResponse response) throws IOException, IOException {
        PageInfo<LiveRoomRelation> LiveRoomRelationPageInfo= this.baseService.getPage(query);
        List<LiveRoomRelation> LiveRoomRelationList = new ArrayList<>();
        if(LiveRoomRelationPageInfo != null && LiveRoomRelationPageInfo.getList() != null){
            LiveRoomRelationList = LiveRoomRelationPageInfo.getList();
        }
        List<LiveRoomRelationVo> LiveRoomRelationVoList = EntityUtil.copyProperties(LiveRoomRelationList, LiveRoomRelationVo.class);
        // 字段名和中文名的映射
        Map<String, String> fieldMap = excelTemplate();

        // 创建Workbook对象
        Workbook workbook = ExcelUtil.createWorkbook(LiveRoomRelationVoList, fieldMap);

        // 设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=inbound_records.xlsx");

        // 将Workbook写入响应输出流
        try (OutputStream outputStream = response.getOutputStream()) {
            workbook.write(outputStream);
        } finally {
            workbook.close();
        }
    }
}
