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.error.ServiceRuntimeException;
import com.ssyf.common.web.result.Result;
import com.ssyf.module.robot.domain.entity.RRobotConsumptionRecord;
import com.ssyf.module.robot.domain.entity.RRobotConsumptionRecord;
import com.ssyf.module.robot.domain.query.RRobotConsumptionRecordQuery;
import com.ssyf.module.robot.domain.query.RRobotConsumptionRecordQuery;
import com.ssyf.module.robot.domain.vo.RRobotConsumptionRecordVo;
import com.ssyf.module.robot.domain.vo.RRobotConsumptionRecordVo;
import com.ssyf.module.robot.service.RRobotConsumptionRecordService;
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.RRobotConsumptionRecord模型开放接口
*
* @author: auth
* @date: 2022-12-12
*/
@Api(tags="基础-机器人消费记录")
@RestController
@RequestMapping("/auth/RobotConsumptionRecord")
public class RRobotConsumptionRecordAuthController {

    @Autowired
    private RRobotConsumptionRecordService baseService;

    @Log(title = "机器人消费记录-新增数据")
    @RequestSynchronization
    @ApiOperation(value = "新增数据", notes = "新增数据")
    @PostMapping(value = "/addOne")
    public Result<RRobotConsumptionRecordVo> addOne(@Validated @RequestBody RRobotConsumptionRecordQuery query) {
        if(BeanUtil.isEmpty(query)){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR);
        }
        RRobotConsumptionRecord consumptionRecord = this.baseService.addOne(query);
        return Result.success(BeanUtil.copyProperties(consumptionRecord , RRobotConsumptionRecordVo.class));
    }

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

    @Log(title = "机器人消费记录-批量删除数据")
    @RequestSynchronization
    @ApiOperation(value = "批量删除数据", notes = "批量删除数据")
    @PostMapping(value = "/removeBatchByUids")
    public Result<Boolean> removeBatchByUids(@RequestBody List<String> uids) {
        this.baseService.removeBatchByUids(uids);
        return Result.success(true);
    }

    @Log(title = "机器人消费记录-更新数据")
    @RequestSynchronization
    @ApiOperation(value = "更新数据", notes = "更新数据")
    @PostMapping(value = "/updateByUid/{uid}")
    public Result<Boolean> updateByUid(@PathVariable String uid, @Validated @RequestBody RRobotConsumptionRecordQuery query) {
        this.baseService.updateByUid(uid, query);
        return Result.success(true);
    }

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

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

    @ApiOperation(value = "/查询所有数据")
    @PostMapping(value = "/queryList")
    public Result<List<RRobotConsumptionRecordVo>> queryList(@Validated @RequestBody RRobotConsumptionRecordQuery query) {
        List<RRobotConsumptionRecord> consumptionRecordList = this.baseService.getList(query);
        List<RRobotConsumptionRecordVo> t = EntityUtil.copyProperties(consumptionRecordList, RRobotConsumptionRecordVo.class);
        return Result.success(t);
    }

    @ApiOperation(value = "/查询总数")
    @PostMapping(value = "/count")
    public Result<Integer> count(@Validated @RequestBody RRobotConsumptionRecordQuery 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("giftName", "礼物名称")
                .put("giftId", "礼物ID")
                .put("giftUnitPrice", "礼物单价")
                .put("giftQuantity", "礼物数量")
                .put("giftTotalPrice", "礼物总价")
                .put("anchorId", "主播ID")
                .put("anchorName", "主播名称")
                .put("roomName", "直播间名称")
                .put("consumptionAmount", "消费金额")
                .put("consumptionTime", "消费时间")
                .put("consumptionMethod", "消费方式")
                .put("consumptionInfo", "消费信息")
                .put("notes", "备注")
                .build();
        return fieldMap;
    }


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

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