package com.gic.haoban.manage.web.controller.open;

import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.haoban.base.api.common.pojo.dto.WebLoginDTO;
import com.gic.haoban.common.utils.AuthWebRequestUtil;
import com.gic.haoban.common.utils.EntityUtil;
import com.gic.haoban.manage.web.qo.open.AlarmWordsLogPageQO;
import com.gic.haoban.manage.web.qo.open.AlarmWordsPageQO;
import com.gic.haoban.manage.web.qo.open.AlarmWordsQO;
import com.gic.haoban.task.manage.api.dto.open.*;
import com.gic.haoban.task.manage.api.qdto.open.AlarmWordsLogQDTO;
import com.gic.haoban.task.manage.api.qdto.open.QwOpenAlarmWordsQDTO;
import com.gic.haoban.task.manage.api.service.OpenQwApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 企微托管-敏感词
 */
@RestController
@RequestMapping("/qw-open")
public class QywxAlarmWordsController {

    @Autowired
    private OpenQwApiService openQwApiService ;

    /**
     * 敏感词列表
     */
    @RequestMapping("page-alarm-words")
    public RestResponse<Page<OpenQwAlarmWordsDTO>> pageAlarmWords(@RequestBody AlarmWordsPageQO qo) {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = loginUser.getWxEnterpriseId() ;
        String enterpriseId = loginUser.getEnterpriseId() ;
        BasePageInfo basePageInfo = new BasePageInfo() ;
        basePageInfo.setPageNum(qo.getPageNum());
        basePageInfo.setPageSize(qo.getPageSize());
        QwOpenAlarmWordsQDTO qdto = EntityUtil.changeEntityByJSON(QwOpenAlarmWordsQDTO.class,qo) ;
        qdto.setWxEnterpriseId(wxEnterpriseId);
        qdto.setEnterpriseId(enterpriseId);
        ServiceResponse<Page<OpenQwAlarmWordsDTO>> resp = this.openQwApiService.alarmWordsPage(qdto,basePageInfo) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult(resp.getResult()) ;
    }

    /**
     * 保存敏感词
     * @param qo
     * @return
     */
    @RequestMapping("save-alarm-words")
    public RestResponse<Void> saveAlarmWords(@RequestBody AlarmWordsQO qo) {
        OpenQwAlarmWordsDTO dto = EntityUtil.changeEntityByJSON(OpenQwAlarmWordsDTO.class,qo) ;
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = loginUser.getWxEnterpriseId() ;
        String enterpriseId = loginUser.getEnterpriseId() ;
        dto.setWxEnterpriseId(wxEnterpriseId);
        dto.setEnterpriseId(enterpriseId);
        dto.setCreatorName(loginUser.getClerkId());
        dto.setCreatorName(loginUser.getClerkName());
        ServiceResponse<Void> resp = this.openQwApiService.saveAlarmWords(dto) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult() ;
    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @RequestMapping("del-alarm-words")
    public RestResponse<Void> delAlarmWords(Long id) {
        ServiceResponse<Void> resp = this.openQwApiService.delAlarmWords(id) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult() ;
    }

    /**
     * 敏感词监控列表
     */
    @RequestMapping("page-alarm-words-log")
    public RestResponse<Page<OpenQwAlarmLogDTO>> pageAlarmLog(@RequestBody AlarmWordsLogPageQO qo) {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = loginUser.getWxEnterpriseId() ;
        String enterpriseId = loginUser.getEnterpriseId() ;
        BasePageInfo basePageInfo = new BasePageInfo() ;
        basePageInfo.setPageNum(qo.getPageNum());
        basePageInfo.setPageSize(qo.getPageSize());
        AlarmWordsLogQDTO qdto = EntityUtil.changeEntityByJSON(AlarmWordsLogQDTO.class,qo) ;
        qdto.setWxEnterpriseId(wxEnterpriseId);
        qdto.setEnterpriseId(enterpriseId);
        ServiceResponse<Page<OpenQwAlarmLogDTO>> resp = this.openQwApiService.alarmWordsLogPage(qdto,basePageInfo) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult(resp.getResult()) ;
    }


}
