package cn.hanghui.outapi.platform.tenant.controller;

import cn.hanghui.outapi.platform.common.config.log.Log;
import cn.hanghui.outapi.platform.common.controller.BaseController;
import cn.hanghui.outapi.platform.common.entity.rest.RestResult;
import cn.hanghui.outapi.platform.common.utils.ExcelDataListener;
import cn.hanghui.outapi.platform.tenant.entity.dto.blacklist.*;
import cn.hanghui.outapi.platform.tenant.entity.enums.BusinessTypeEnum;
import cn.hanghui.outapi.platform.tenant.entity.enums.LogTagEnum;
import cn.hanghui.outapi.platform.tenant.service.UserBlacklistService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * @author shisl
 * @package cn.hanghui.outapi.platform.tenant.controller
 * @class UserBlacklistController
 * @date 2024/8/29 上午9:14
 * @description
 */
@Slf4j
@Api(tags = "黑名单管理")
@RestController
@RequestMapping(value = "/api/v1/user/blacklist")
@Validated
public class UserBlacklistController extends BaseController {

    @Autowired
    private UserBlacklistService userBlacklistService;

    @ApiOperation("批量导入黑名单")
    @PostMapping("/task/import")
    @Log(tag = LogTagEnum.BLACK,title = "批量导入黑名单",businessTypeEnum = BusinessTypeEnum.INSERT,isSaveRequestData=false)
    public RestResult<List<String>> importBlacklist(@RequestParam("file") MultipartFile excelFile,
                                                    @RequestParam("taskName") String taskName,
                                                    @RequestParam("purchaserId") Long purchaserId) {
        ExcelDataListener listener = new ExcelDataListener();
        try {
            EasyExcel.read(excelFile.getInputStream(), UserBlacklistBean.class, listener).sheet(0).doReadSync();
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<Object> datas = listener.getDatas();
        return userBlacklistService.importBlacklist(datas, taskName, purchaserId);
    }

    @ApiOperation(value = "黑名单任务列表（分页）")
    @PostMapping("/task/page")
    public RestResult<IPage<UserBlacklistTaskVo>> page(@RequestBody UserBlacklistTaskSearch dto) {
        return userBlacklistService.page(dto);
    }

    @ApiOperation(value = "黑名单获取（分页）")
    @PostMapping("/page")
    public RestResult<IPage<UserBlacklistBean>> upage(@RequestBody UserBlacklistSearch dto) {
        return userBlacklistService.upage(dto);
    }

    @ApiOperation(value = "黑名单任务删除")
    @PostMapping("/del/{taskId}")
    @Log(tag = LogTagEnum.BLACK,title = "黑名单任务删除",businessTypeEnum = BusinessTypeEnum.DELETE)
    public RestResult<Boolean> del(@PathVariable(value = "taskId") @ApiParam(value = "taskId", required = true) Long taskId) {
        return userBlacklistService.del(taskId);
    }

    @ApiOperation(value = "黑名单任务列表")
    @PostMapping("/task/list")
    public RestResult<List<UserBlacklistTaskLessVo>> list(@RequestBody @Valid UserBlacklistTaskListSearch dto) {
        return userBlacklistService.list(dto);
    }
}
