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

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
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.business.notify.domain.JyNotifyLog;
import com.jeeplus.business.notify.domain.PluginNotifyComments;
import com.jeeplus.business.notify.service.JyNotifyService;
import com.jeeplus.business.notify.service.dto.JyNotifyDTO;
import com.jeeplus.business.notify.service.dto.PluginNotifyCommentsDTO;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.utils.StringUtils;
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("/jy/notify")
public class JyNotifyController {

    @Autowired
    private JyNotifyService notifyService;


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

    /**
     * 通告列表数据(通知栏)
     */
    @GetMapping("myNotifyList")
    public ResponseEntity myNotifyData(JyNotifyDTO notifyDTO, boolean isSelf, Page <JyNotifyDTO> page) throws Exception {
        QueryWrapper <JyNotifyDTO> queryWrapper = QueryWrapperGenerator.buildQueryCondition ( notifyDTO, JyNotifyDTO.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_time", notifyDTO.getCreateDates()[0], notifyDTO.getCreateDates()[1]);
            }
        }
        UserDTO userDTO = SpringUtil.getBean(IUserApi.class).getByToken(TokenProvider.getCurrentToken());
        IPage <JyNotifyDTO> result = notifyService.myNotifyDataFindPage ( page, userDTO.getId ( ), isSelf, null, queryWrapper );
        return ResponseEntity.ok ( result );
    }

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

        }
        JyNotifyDTO notifyDTO = notifyService.getDetail (id);
        //获取日志信息
        int pageSize = Integer.parseInt(size);
        List<JyNotifyLog> byNotifyId = notifyService.findByNotifyId(pageSize, id);
        Integer count=notifyService.findLogCount(id);
        notifyDTO.setLogCount(count);
        notifyDTO.setNotifyLogList(byNotifyId);
        return ResponseEntity.ok ( notifyDTO);
    }

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


    @PostMapping("save")
    public ResponseEntity save(@Valid @RequestBody JyNotifyDTO notifyDTO) {
        notifyService.saveForm ( notifyDTO );
        return ResponseEntity.ok ( "保存通知'" + notifyDTO.getTitle ( ) + "'成功" );
    }


    /**
     * 公告通知保存
     * @param notifyDTO
     * @return
     */
    @ApiOperation("公告通知保存")
    @PostMapping("saveForm")
    public ResponseEntity saveForm(@Valid @RequestBody JyNotifyDTO notifyDTO) {
        String id = notifyService.saveForm(notifyDTO);
        return ResponseUtil.newInstance().add("businessTable", "jy_plugin_notify").add("businessId", id).ok ("操作成功");
    }

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


    /**
     * 根据id删除
     * @param ids
     * @return
     */
    @DeleteMapping("delete")
    @ApiOperation("根据id删除")
    public ResponseEntity<String> delete(String ids) {
        String idArray[] = ids.split ( "," );
        notifyService.removeByIds ( Lists.newArrayList ( idArray ) );
        return ResponseEntity.ok ( "删除通知成功" );
    }

    /**
     * 根据id关闭通知
     * @param id
     * @return
     */
    @GetMapping("close")
    @ApiOperation("根据id关闭通知")
    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
     */
    @ApiOperation("部分标记为已读")
    @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();
    }


    /**
     * 添加或修改通知日志
     */
    @ApiOperation("添加或修改通知日志")
    @PostMapping("saveLog")
    public String saveLog(@Valid @RequestBody JyNotifyDTO notifyDTO){
        return notifyService.saveLog(notifyDTO);
    }




}
