package com.fs.company.controller.qw;

import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.company.domain.CompanyUser;
import com.fs.framework.security.SecurityUtils;
import com.fs.qw.param.QwGroupChatParam;
import com.fs.qw.param.TransferChatParam;
import com.fs.qw.result.ResultMessage;
import com.fs.qw.service.IQwGroupChatService;
import com.fs.qw.vo.QwGroupChatTransferVO;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/qw/groupChatTransfer")
@AllArgsConstructor
public class QwGroupChatTransferController extends BaseController {

    private final IQwGroupChatService qwGroupChatService;

    @PreAuthorize("@ss.hasPermi('qw:groupChatTransfer:listOnJob')")
    @GetMapping("/listOnJob")
    public TableDataInfo listOnJob(QwGroupChatParam qwGroupChat) {
        startPage();
        qwGroupChat.setStatusList(Arrays.asList("0", "3"));
        List<QwGroupChatTransferVO> list = qwGroupChatService.selectQwGroupChatTransferList(qwGroupChat);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('qw:groupChatTransfer:transferOnJob')")
    @PostMapping("/transferOnJob")
    public AjaxResult transferOnJob(@RequestBody @Valid TransferChatParam param) {
        if (param.getChatIds().size() > 100) {
            return AjaxResult.error("单批次最大转移数为100个");
        }
        CompanyUser user = SecurityUtils.getLoginUser().getUser();
        ResultMessage message = qwGroupChatService.processTransfer(param, user, false);
        return AjaxResult.success(message.buildResultMessage());
    }

    @PreAuthorize("@ss.hasPermi('qw:groupChatTransfer:list')")
    @GetMapping("/list")
    public TableDataInfo list(QwGroupChatParam qwGroupChat) {
        startPage();
        qwGroupChat.setStatusList(Collections.singletonList("1"));
        List<QwGroupChatTransferVO> list = qwGroupChatService.selectQwGroupChatTransferList(qwGroupChat);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('qw:groupChatTransfer:transfer')")
    @PostMapping("/transfer")
    public AjaxResult transfer(@RequestBody @Valid TransferChatParam param) {
        if (param.getChatIds().size() > 100) {
            return AjaxResult.error("单批次最大转移数为100个");
        }
        CompanyUser user = SecurityUtils.getLoginUser().getUser();
        ResultMessage message = qwGroupChatService.processTransfer(param, user, true);
        return AjaxResult.success(message.buildResultMessage());
    }
}
