package com.keyware.shandan.bianmu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.keyware.shandan.bianmu.entity.DirectoryVo;
import com.keyware.shandan.bianmu.entity.MetadataBasicVo;
import com.keyware.shandan.bianmu.entity.ReviewRecordVo;
import com.keyware.shandan.bianmu.enums.ReviewStatus;
import com.keyware.shandan.bianmu.service.DirPermissionService;
import com.keyware.shandan.bianmu.service.DirectoryService;
import com.keyware.shandan.bianmu.service.MetadataService;
import com.keyware.shandan.bianmu.service.ReviewRecordService;
import com.keyware.shandan.common.constants.DirConstant;
import com.keyware.shandan.common.controller.BaseController;
import com.keyware.shandan.common.entity.Result;
import com.keyware.shandan.common.util.StringUtils;
import com.keyware.shandan.frame.config.security.SecurityUtil;
import com.keyware.shandan.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 审核记录表 前端控制器
 * </p>
 *
 * @author GuoXin
 * @since 2021-06-03
 */
@RestController
@RequestMapping("/business/review")
public class ReviewRecordController extends BaseController<ReviewRecordService, ReviewRecordVo, String> {

    @Autowired
    private ReviewRecordService reviewRecordService;

    @Autowired
    private MetadataService metadataService;

    @Autowired
    private DirectoryService directoryService;

    @Autowired
    private DirPermissionService dirPermissionService;
    /**
     * 目录审核页面
     *
     * @return
     */
    @GetMapping("/directory")
    public ModelAndView directoryReview(ModelAndView modelAndView) {
        modelAndView.setViewName("review/directoryReview");
        SysUser user = SecurityUtil.getLoginSysUser();
        dirPermissionService.refreshCache();
        modelAndView.addObject("readDirIds", dirPermissionService.getByReadPermis(user));
        modelAndView.addObject("writeDirIds", dirPermissionService.getByWritePermis(user));
        return modelAndView;
        //return new ModelAndView("review/directoryReview");

    }

    /**
     * 数据资源审核页面
     *
     * @return
     */
    @GetMapping("/metadata")
    public ModelAndView metadataReview() {
        return new ModelAndView("review/metadataReview");
    }

    /**
     * 数据资源待审核数据列表
     *
     * @param page
     * @param vo
     * @return
     */
    @GetMapping("/list/metadata")
    public Result<Page<MetadataBasicVo>> metadataReviewPageList(Page<MetadataBasicVo> page, MetadataBasicVo vo) {
        return Result.of(metadataService.page(page, new QueryWrapper<>(vo)));
    }

    /**
     * 目录待审核数据列表
     *
     * @param page
     * @param vo
     * @return
     */
    @GetMapping("/list/directory")
    public Result<Page<DirectoryVo>> directoryReviewPageList(Page<DirectoryVo> page, DirectoryVo vo) {
        QueryWrapper<DirectoryVo> query = new QueryWrapper<>(vo);
        query.ne("id", DirConstant.DIR_ROOT_ID);
        return Result.of(directoryService.page(page, query));
    }

    /**
     * 审核相关操作
     *
     * @param entityType   数据对象类型
     * @param entityId     数据对象ID
     * @param status       审核状态
     * @param opinion      审核意见
     * @param metadataName 数据名称
     * @return
     */
    @PostMapping("/operate")
    public Result<Boolean> operate(String entityType, String entityId,
                                   String status, String opinion, String metadataName) throws Exception {
        if (StringUtils.isBlankAny(entityType, entityId, status)) {
            return Result.of(null, false, "参数错误");
        }

        return Result.of(reviewRecordService.review(entityId, entityType, status, opinion, metadataName, "PASS"));
    }

    /**
     * 数据资源提交审核
     *
     * @param id         主键
     * @param status     要变更的审核状态
     * @param opinion    审核意见
     * @param sendNotice 是否发送通知
     * @return 请求结果
     */
    @PostMapping("/metadata/change")
    public Result<Object> metadataSubmit(String id, String status, String opinion, boolean sendNotice) {
        if (StringUtils.isBlankAny(id, status)) {
            return Result.of(null, false, "参数错误");
        }
        ReviewStatus reviewStatus = ReviewStatus.valueOf(status);
        Boolean result = reviewRecordService.metadataReview(id, reviewStatus, opinion, sendNotice);
        return Result.of(result, result);
    }

    /**
     * 资源目录提交审核
     *
     * @param id         主键
     * @param status     要变更的审核状态
     * @param opinion    审核意见
     * @param sendNotice 是否发送通知
     * @return 请求结果
     */
    @PostMapping("/directory/change")
    public Result<Object> directorySubmit(String id, String status, String opinion, boolean sendNotice) {
        if (StringUtils.isBlankAny(id, status)) {
            return Result.of(null, false, "参数错误");
        }
        try {
            ReviewStatus reviewStatus = ReviewStatus.valueOf(status);
            Boolean result = reviewRecordService.directoryReview(id, reviewStatus, opinion, sendNotice);
            return Result.of(result, result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.of(false, false, e.getMessage());
        }
    }

    /**
     * 资源目录文件提交审核
     *
     * @param ids         主键
     * @param status     要变更的审核状态
     * @param opinion    审核意见
     * @param sendNotice 是否发送通知
     * @return 请求结果
     *
     * xiongcl 20240412
     */
    @PostMapping("/directory/file/change")
    public Result<Object> directoryFileSubmit(String ids, String status, String opinion, boolean sendNotice) {
        if (StringUtils.isBlankAny(ids, status)) {
            return Result.of(null, false, "参数错误");
        }
        try {
            ReviewStatus reviewStatus = ReviewStatus.valueOf(status);
            Arrays.stream(ids.split(",")).forEach(id -> {
                try {
                    reviewRecordService.directoryFileReview(id, reviewStatus, opinion, sendNotice);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });
            return Result.of(true, true);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return Result.of(false, false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.of(false, false, e.getMessage());
        }

    }

    /**
     * 根据数据实体ID获取最后一次审核记录
     *
     * @param entityId
     * @return
     */
    @GetMapping("/get/entity")
    public Result<ReviewRecordVo> getLastByEntityId(String entityId) {
        if (StringUtils.isBlank(entityId)) {
            return Result.of(null, false, "参数错误");
        }
        QueryWrapper<ReviewRecordVo> query = new QueryWrapper<>();
        query.eq("ENTITY_ID", entityId);
        query.orderByDesc("CREATE_TIME");
        List<ReviewRecordVo> list = reviewRecordService.list(query);
        if (list.size() > 0) {
            return Result.of(list.get(0));
        }
        return Result.of(null, false);
    }
}
