package com.jiayue.ssi.controller;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiayue.ssi.annotation.OperateLog;
import com.jiayue.ssi.annotation.PreventReplay;
import com.jiayue.ssi.backenum.ApproveOperaterEnum;
import com.jiayue.ssi.backenum.ApproveStatusEnum;
import com.jiayue.ssi.backenum.AuditType;
import com.jiayue.ssi.backenum.BusinessType;
import com.jiayue.ssi.constant.ApproveConstants;
import com.jiayue.ssi.constant.CustomException;
import com.jiayue.ssi.entity.SysApprove;
import com.jiayue.ssi.entity.SysUser;
import com.jiayue.ssi.service.SysApproveService;
import com.jiayue.ssi.util.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 审核接口
 *
 * @author xsl
 * @since 2023/03/13
 */
@RestController
@RequestMapping("/sysApproveController")
@Slf4j
public class SysApproveController {
    @Autowired
    SysApproveService sysApproveService;

    /**
     * 获取用户分页信息
     *
     * @return 用户信息
     */
    @GetMapping(value = "/getAll")
    @PreAuthorize("@ss.hasPermi('approveManager:approve:list')")
    @PreventReplay
    public ResponseVO getAll(String currentPage, String pageSize, String approveStatus, String approveResult) throws CustomException {
        try {
            Integer cp;
            if (StringUtils.isNotEmpty(currentPage)) {
                if (NumberUtil.isInteger(currentPage)){
                    cp = Integer.parseInt(currentPage);
                }
                else{
                    return ResponseVO.fail("currentPage不是整数！");
                }
            }
            else{
                return ResponseVO.fail("currentPage不能是空！");
            }

            Integer ps;
            if (StringUtils.isNotEmpty(pageSize)) {
                if (NumberUtil.isInteger(pageSize)){
                    ps = Integer.parseInt(pageSize);
                }
                else{
                    return ResponseVO.fail("pageSize不是整数！");
                }
            }
            else{
                return ResponseVO.fail("pageSize不能是空！");
            }

            QueryWrapper<SysApprove> wrapper = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(approveStatus)) {
                wrapper.eq("approve_status", approveStatus);
            }
            if (StringUtils.isNotEmpty(approveResult)) {
                wrapper.eq("approve_result", approveResult);
            }
            Page<SysApprove> result = sysApproveService.page(new Page<>(cp, ps), wrapper);
            List<SysApprove> records = result.getRecords();
            //遍历对象数组的方法
            records.forEach(
                    record -> {
                        if (record.getModuleName().equals(ApproveConstants.MODULE_NAME_USER)){
                            SysUser sysUser = JSONUtil.toBean(record.getParameterContent(),SysUser.class);
                            if (record.getOperation().equals(String.valueOf(ApproveOperaterEnum.DELETE.getCode()))){
                                record.setParameterContent("{账号："+sysUser.getUsername()+"}");
                            }
                        }
                    }
            );
            return ResponseVO.success(result);
        } catch (Exception e) {
            throw new CustomException("获取所有审核异常", e);
        }
    }

    /**
     * 审批结果提交
     */
    @PostMapping(value = "/submitApprove")
    @OperateLog(title = "审核管理", businessType = BusinessType.UPDATE, auditType = AuditType.SYS)
    @PreAuthorize("@ss.hasPermi('approveManager:approve:submitApprove')")
    @PreventReplay
    public ResponseVO submitApprove(@RequestBody SysApprove sysApprove) throws CustomException {
        try {
            sysApproveService.submitApprove(sysApprove);
            return ResponseVO.success("审核提交成功");
        } catch (Exception e) {
            throw new CustomException("审核提交异常", e);
        }
    }
}
