/**
 * Copyright &copy; 2021-2026 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.notify.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.notify.domain.PluginNotifyComments;
import com.jeeplus.notify.service.NotifyService;
import com.jeeplus.notify.service.dto.NotifyDTO;
import com.jeeplus.notify.service.dto.NotifyRecordDTO;
import com.jeeplus.sys.constant.CommonConstants;
import com.jeeplus.sys.service.UserService;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.sys.utils.UserUtils;
import com.jeeplus.test.cw.workClientInfo.service.dto.CwWorkClientBaseDTO;
import com.jeeplus.test.knowledge.domain.KnowledgeShareComment;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 通知通告Controller
 *
 * @author jeeplus
 * @version 2021-05-16
 */
@RestController
@RequestMapping("/notify")
public class NotifyController {

    @Autowired
    private NotifyService notifyService;

    @Autowired
    private UserService userService;

    /**
     * 通告列表数据
     * 若对该方法进行调整则需要同步调整下边的 myNotifyData 方法
     */
    @GetMapping("list")
    public ResponseEntity data(NotifyDTO notifyDTO, boolean isSelf, Page <NotifyDTO> page) throws Exception {
        QueryWrapper <NotifyDTO> queryWrapper = QueryWrapperGenerator.buildQueryCondition ( notifyDTO, NotifyDTO.class );
        if (ObjectUtil.isNotEmpty(notifyDTO)) {
            if (StringUtils.isNotBlank(notifyDTO.getCreateName())) {
                queryWrapper.like("u.name",notifyDTO.getCreateName());
            }
            if (ArrayUtil.isNotEmpty(notifyDTO.getCreateDates())) {
                queryWrapper.between("a.create_date", notifyDTO.getCreateDates()[0], notifyDTO.getCreateDates()[1]);
            }
        }
        IPage <NotifyDTO> result = notifyService.findPage ( page, UserUtils.getCurrentUserDTO ( ).getId ( ), isSelf, null, queryWrapper );
        return ResponseEntity.ok ( result );
    }

    /**
     * 通告列表数据(通知栏)
     */
    @GetMapping("myNotifyList")
    public ResponseEntity myNotifyData(NotifyDTO notifyDTO, boolean isSelf, Page <NotifyDTO> page) throws Exception {
        QueryWrapper <NotifyDTO> queryWrapper = QueryWrapperGenerator.buildQueryCondition ( notifyDTO, NotifyDTO.class );
        if (ObjectUtil.isNotEmpty(notifyDTO)) {
            if (StringUtils.isNotBlank(notifyDTO.getCreateName())) {
                queryWrapper.like("u.name",notifyDTO.getCreateName());
            }
            if (ArrayUtil.isNotEmpty(notifyDTO.getCreateDates())) {
                queryWrapper.between("a.create_date", notifyDTO.getCreateDates()[0], notifyDTO.getCreateDates()[1]);
            }
        }
        IPage <NotifyDTO> result = notifyService.myNotifyDataFindPage ( page, UserUtils.getCurrentUserDTO ( ).getId ( ), isSelf, null, queryWrapper );
        return ResponseEntity.ok ( result );
    }

    /**
     * 查询数据
     */
    @GetMapping("queryById")
    public ResponseEntity queryById(String id, boolean isSelf) {
        if ( isSelf ) {
            notifyService.updateReadFlag ( id );

        }
        NotifyDTO notifyDTO = notifyService.getDetail ( id );
        return ResponseEntity.ok ( notifyDTO);
    }

    @PostMapping("pushNotifyRecord")
    public ResponseEntity pushNotifyRecord(String id) {
        String s = notifyService.pushNotifyRecord(id);
        return ResponseEntity.ok ( s );
    }

    @PreAuthorize ("hasAnyAuthority('notify:add','notify:edit')")
    @PostMapping("save")
    public ResponseEntity save(@Valid @RequestBody NotifyDTO notifyDTO) {
        notifyService.saveForm ( notifyDTO );
        return ResponseEntity.ok ( "保存通知'" + notifyDTO.getTitle ( ) + "'成功" );
    }

    @PreAuthorize ("hasAnyAuthority('notify:add','notify:edit')")
    @PostMapping("saveForm")
    public ResponseEntity saveForm(@Valid @RequestBody NotifyDTO notifyDTO) {
        String id = notifyService.saveForm(notifyDTO);
        return ResponseUtil.newInstance().add("businessTable", "plugin_notify").add("businessId", id).ok ("操作成功");
    }

    /**
     * 根据id修改状态值status
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据id修改状态值status")
    @PostMapping(value = "/updateStatusById")
    public ResponseEntity<String> updateStatusById(@RequestBody NotifyDTO dto) {
        String s = notifyService.updateStatusById(dto);
        return ResponseEntity.ok(s);
    }

    @PreAuthorize("hasAuthority('notify:del')")
    @DeleteMapping("delete")
    public ResponseEntity <String> delete(String ids) {
        String idArray[] = ids.split ( "," );
        notifyService.removeByIds ( Lists.newArrayList ( idArray ) );
        return ResponseEntity.ok ( "删除通知成功" );
    }

    @DeleteMapping("close")
    public ResponseEntity <String> close(String id) {
        notifyService.closeById (id);
        return ResponseEntity.ok ( "关闭通知成功" );
    }

    /**
     * 评论
     */
    @PostMapping("/addComment")
    @ApiOperation(value = "添加评论")
    public ResponseEntity<String> addComment(@RequestBody PluginNotifyComments comment) {
        String s = notifyService.addComment(comment);
        return ResponseEntity.ok(s);
    }

    /**
     * 删除评论
     */
    @GetMapping("/delComment")
    @ApiOperation(value = "删除评论")
    public ResponseEntity<String> delComment(String id) {
        String s = notifyService.delComment(id);
        return ResponseEntity.ok(s);
    }

    /**
     * 部分标记为已读
     * @param ids
     * @return
     */
    @ApiLog("部分标记为已读")
    @PostMapping("portionRead")
    public ResponseEntity portionRead(String ids) {
        String idArray[] =ids.split(",");
        notifyService.portionRead (Lists.newArrayList (idArray));
        return ResponseEntity.ok ("部分标记为已读成功");
    }

    @ApiOperation(value = "全部标记已读")
    @PostMapping("/readAll")
    public String readAll() {
        return notifyService.readAll();
    }

}
