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.RRobotAccount;
import com.ssyf.module.robot.domain.entity.RRobotRunRecord;
import com.ssyf.module.robot.domain.entity.RRobotRunRecord;
import com.ssyf.module.robot.domain.entity.RRobotRunRecord;
import com.ssyf.module.robot.domain.query.RRobotRunRecordQuery;
import com.ssyf.module.robot.domain.query.RRobotRunRecordQuery;
import com.ssyf.module.robot.domain.vo.RRobotAccountVo;
import com.ssyf.module.robot.domain.vo.RRobotRunRecordVo;
import com.ssyf.module.robot.domain.vo.RRobotRunRecordVo;
import com.ssyf.module.robot.domain.vo.RRobotRunRecordVo;
import com.ssyf.module.robot.service.RRobotRunRecordService;
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 io.swagger.models.auth.In;
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.RRobotRunRecord模型开放接口
*
* @author: auth
* @date: 2022-12-12
*/
@Api(tags="基础-机器人运行记录")
@RestController
@RequestMapping("/auth/RobotRunRecord")
public class RRobotRunRecordAuthController {

    @Autowired
    private RRobotRunRecordService baseService;

    @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 = "/removeBatchByUids")
    public Result removeBatchByUids(@RequestBody List<String> uuids) {
        this.baseService.removeBatchByUids(uuids);
        return Result.success();
    }

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

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

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

    @ApiOperation(value = "/查询所有数据")
    @PostMapping(value = "/queryList")
    public Result<List<RRobotRunRecordVo>> queryList(@Validated @RequestBody RRobotRunRecordQuery query) {
        PageInfo<RRobotRunRecord> temp = this.baseService.getPage(query);
        return Result.success(PageEnhanceHelper.switchPageInfo(temp , RRobotRunRecordVo.class));
    }

    @ApiOperation(value = "/查询总数")
    @PostMapping(value = "/count")
    public Result<Long> count(@Validated @RequestBody RRobotRunRecordQuery 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("configType", "配置类型")
                .put("notes", "备注")
                .put("executeStatusStr", "执行状态")
                .put("executeStartTime", "执行时间")
                .put("liveRoomId", "直播间ID")
                .put("anchorId", "主播ID")
                .put("liveRoomName", "直播间名称")
                .put("anchorName", "主播名称")
                .build();
        return fieldMap;
    }


    @ApiOperation(value = "导出数据", notes = "根据查询条件导出数据为Excel文件")
    @PostMapping(value = "/export", produces = "application/vnd.ms-excel")
    public void export(@Validated @RequestBody RRobotRunRecordQuery query, HttpServletResponse response) throws IOException, IOException {
        PageInfo<RRobotRunRecord> RRobotRunRecordPageInfo= this.baseService.getPage(query);
        List<RRobotRunRecord> RRobotRunRecordList = new ArrayList<>();
        if(RRobotRunRecordPageInfo != null && RRobotRunRecordPageInfo.getList() != null){
            RRobotRunRecordList = RRobotRunRecordPageInfo.getList();
        }
        List<RRobotRunRecordVo> RRobotRunRecordVoList = EntityUtil.copyProperties(RRobotRunRecordList, RRobotRunRecordVo.class);
        for (RRobotRunRecordVo RRobotRunRecordVo : RRobotRunRecordVoList){
            RRobotRunRecordVo.setExecuteStatusStr();
        }

        // 字段名和中文名的映射
        Map<String, String> fieldMap = excelTemplate();

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