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

import cn.hutool.core.bean.BeanUtil;
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.RRobotRunningInfo;
import com.ssyf.module.robot.domain.entity.RRobotRunningInfo;
import com.ssyf.module.robot.domain.query.RRobotRunningInfoQuery;
import com.ssyf.module.robot.domain.query.RRobotRunningInfoQuery;
import com.ssyf.module.robot.domain.vo.RRobotRunningInfoVo;
import com.ssyf.module.robot.domain.vo.RRobotRunningInfoVo;
import com.ssyf.module.robot.service.RRobotRunningInfoService;
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 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.RRobotRunningInfo模型开放接口
*
* @author: auth
* @date: 2022-12-12
*/
@Api(tags="基础-机器人运行信息")
@RestController
@RequestMapping("/auth/RobotRunningInfo")
public class RRobotRunningInfoAuthController {

    @Autowired
    private RRobotRunningInfoService baseService;

    @Log(title = "机器人运行信息-新增数据")
    @RequestSynchronization
    @ApiOperation(value = "新增数据", notes = "新增数据")
    @PostMapping(value = "/addOne")
    public Result<RRobotRunningInfoVo> addOne(@Validated({ValidA.class}) @RequestBody RRobotRunningInfoQuery query) {
        if(BeanUtil.isEmpty(query)){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR);
        }
        RRobotRunningInfo menuRRobotRunningInfo = this.baseService.addOne(query);
        return Result.success(BeanUtil.copyProperties(menuRRobotRunningInfo , RRobotRunningInfoVo.class));
    }

    @Log(title = "机器人运行信息-更新数据")
    @RequestSynchronization
    @ApiOperation(value = "更新数据", notes = "uid更新")
    @PostMapping(value = "/updateByUid/{uid}")
    public Result updateByUid(@PathVariable String uid , @Validated @RequestBody RRobotRunningInfoQuery 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 RRobotRunningInfoQuery query){
        this.baseService.removeBatchByUids(query.getUids());
        return Result.success();
    }


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

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

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

    private Map<String, String> excelTemplate() {
        Map<String, String> fieldMap = MapUtil.builder()
                .put("robotAccount", "机器人账号")
                .put("robotNickName", "机器人昵称")
                .put("runningStatusStr", "运行状态")
                .put("currentBehaviorStr", "当前行为")
                .put("liveRoomId", "直播间ID")
                .put("anchorId", "主播ID")
                .put("liveRoomName", "直播间名称")
                .put("anchorName", "主播名称")
                .put("anchorAvatar", "主播头像")
                .put("loginTime", "登录时间")
                .put("logoutTime", "退出时间")
                .put("lastUpdateTime", "最后更新时间")
                .put("enterLiveRoomTime", "进入直播间时间")
                .put("exitLiveRoomTime", "退出直播间时间")
                .build();
        return fieldMap;
    }



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

        // 创建Workbook对象
        Workbook workbook = ExcelUtil.createWorkbook(RRobotRunningInfoVoList, 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();
        }
    }
}