package com.fs.qw.controller;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.bean.BeanUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import com.fs.qw.domain.QwForbiddenMessage;
import com.fs.qw.domain.QwForbiddenMessageList;
import com.fs.qw.param.QwForbiddenMessageParam;
import com.fs.qw.service.IQwForbiddenMessageService;
import com.fs.qw.vo.QwForbiddenMessageListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 企微违禁消息Controller
 *
 * @author fs
 * @date 2023-11-22
 */
@RestController
@RequestMapping("/qw/forbiddenMessage")
public class QwForbiddenMessageController extends BaseController
{
    @Autowired
    private IQwForbiddenMessageService qwForbiddenMessageService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询企微违禁消息列表
     */
    @PreAuthorize("@ss.hasPermi('qw:forbiddenMessage:list')")
    @GetMapping("/list")
    public TableDataInfo list(QwForbiddenMessageParam param)
    {
        startPage();
        if(!StringUtils.isEmpty(param.getCreateTimeRange())){
            param.setCreateTimeList(param.getCreateTimeRange().split("--"));
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        List<QwForbiddenMessageList> messageLists = qwForbiddenMessageService.selectQwForbiddenMessageListVO(param);
        for (QwForbiddenMessageList messageList : messageLists){
            messageList.setForbiddenMessage("员工在企微账号"+messageList.getAccount()+"聊天中存在违禁词["+messageList.getWord()+"]"+",聊天内容为:  \'"+messageList.getContent()+"\' ,请及时查看处理");        }
        return getDataTable(messageLists);
    }

    /**
     * 导出企微违禁消息列表
     */
    @PreAuthorize("@ss.hasPermi('qw:forbiddenMessage:export')")
    @Log(title = "企微违禁消息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QwForbiddenMessageParam param)
    {
        if(!StringUtils.isEmpty(param.getCreateTimeRange())){
            param.setCreateTimeList(param.getCreateTimeRange().split("--"));
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        List<QwForbiddenMessageList> messageLists = qwForbiddenMessageService.selectQwForbiddenMessageListVO(param);
        List<QwForbiddenMessageListVO> list = new ArrayList<>();
        for (QwForbiddenMessageList messageList : messageLists){
            QwForbiddenMessageListVO vo = new QwForbiddenMessageListVO();
            BeanUtils.copyProperties(messageList,vo);
            vo.setForbiddenMessage("员工在企微账号"+messageList.getAccount()+"聊天中存在违禁词"+",聊天内容为:  \'"+messageList.getContent()+"\' ,请及时查看处理");
            Instant instant = Instant.ofEpochSecond(vo.getSendTime());
            LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            vo.setSendedTime(localDateTime.format(formatter));
            list.add(vo);
        }
        ExcelUtil<QwForbiddenMessageListVO> util = new ExcelUtil<QwForbiddenMessageListVO>(QwForbiddenMessageListVO.class);
        return util.exportExcel(list, "违禁消息");
    }

    /**
     * 获取企微违禁消息详细信息
     */
    @PreAuthorize("@ss.hasPermi('qw:forbiddenMessage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qwForbiddenMessageService.selectQwForbiddenMessageById(id));
    }

    /**
     * 新增企微违禁消息
     */
    @PreAuthorize("@ss.hasPermi('qw:forbiddenMessage:add')")
    @Log(title = "企微违禁消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QwForbiddenMessage qwForbiddenMessage)
    {
        qwForbiddenMessageService.insertQwForbiddenMessage(qwForbiddenMessage);
        return AjaxResult.success();
    }

    /**
     * 修改企微违禁消息
     */
    @PreAuthorize("@ss.hasPermi('qw:forbiddenMessage:edit')")
    @Log(title = "企微违禁消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QwForbiddenMessage qwForbiddenMessage)
    {
        return toAjax(qwForbiddenMessageService.updateQwForbiddenMessage(qwForbiddenMessage));
    }

    /**
     * 删除企微违禁消息
     */
    @PreAuthorize("@ss.hasPermi('qw:forbiddenMessage:remove')")
    @Log(title = "企微违禁消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(qwForbiddenMessageService.deleteQwForbiddenMessageByIds(ids));
    }
}
