package com.jwipc.finance.controller.rest;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.jwipc.finance.dto.PaymentAdvanceRefundTransferApplySaveDto;
import com.jwipc.finance.logic.PaymentAdvanceRefundTransferApplyLogic;
import com.jwipc.finance.model.query.PaymentAdvanceRefundTransferApplyHeadQuery;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.dto.RoleListDTO;
import com.jwipc.sys.logic.RoleLogic;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.jwipc.finance.constant.ModuleConst.MODULE_ROOT;

/**
 * 预付款退款转单记录 业务处理
 * @author qzc
 * @date 2025/07/21
 */
@RestController
@RequestMapping(MODULE_ROOT + "/paymentAdvanceRefundTransferApply/apply/rest")
@RequiredArgsConstructor
public class PaymentAdvanceRefundTransferApplyRestController {

    private final RoleLogic roleLogic;
    private final PaymentAdvanceRefundTransferApplyLogic paymentAdvanceRefundTransferApplyLogic;

    @AuditLogInter(value = "首页查询列表", module = "预付款退款/转单")
    @RequestMapping("/list")
    public PageView indexPageTableData(PaymentAdvanceRefundTransferApplyHeadQuery query) {
        boolean flag = true;
        List<RoleListDTO> userRoles = roleLogic.getUserRoles(LoginUtil.getUserId());
        if(CollUtil.isNotEmpty(userRoles)){
            for (RoleListDTO userRole : userRoles) {
                if("query_all_data".equals(userRole.getRolekey())){
                    flag = false;
                }
            }
        }
        if(flag){
            query.setUserId(LoginUtil.getUserId());
        }
        PageView pageView = paymentAdvanceRefundTransferApplyLogic.selectListByPage(query);
        return pageView;
    }

    @PostMapping("/refundTransferApply/save")
    public ResponseEntity dataSave(@RequestBody PaymentAdvanceRefundTransferApplySaveDto data) throws Exception {
        if (ObjectUtil.isNull(data)) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(this.returnMsg("参数异常, 请重新操作"));
        }
        paymentAdvanceRefundTransferApplyLogic.dataSave(data);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/refundTransferApply/del")
    public ResponseEntity dataDel(String applyIds) {
        if (StringUtils.isEmpty(applyIds)) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(this.returnMsg("参数异常, 请重新操作"));
        }

        try {
            final List<Long> applyIdList = JSONArray.parseArray(applyIds, Long.class);
            paymentAdvanceRefundTransferApplyLogic.delRefundTransferApply(applyIdList);
        } catch (Exception e) {
            e.printStackTrace();
            String message = StringUtils.isNotEmpty(e.getMessage()) ? e.getMessage() : "系统异常: 空指针, 请联系管理";
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(this.returnMsg(message));
        }

        return ResponseEntity.ok("数据删除成功");
    }


    @PostMapping("/refundTransferApply/submit")
    public ResponseEntity submit(Long applyId){
        paymentAdvanceRefundTransferApplyLogic.submit(applyId);
        return ResponseEntity.ok(true);
    }

    @PostMapping("/refundTransferApply/confirm")
    public ResponseEntity confirm(String paymentChangeNo,
                                  String approvalOpinion,
                                  Integer status) throws Exception {
        if (ObjectUtil.isNull(paymentChangeNo)) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(this.returnMsg("参数异常, 请重新操作"));
        }
        paymentAdvanceRefundTransferApplyLogic.confirm(paymentChangeNo,approvalOpinion,status);
        return ResponseEntity.ok().build();
    }

    private JSONObject returnMsg(String msg) {
        JSONObject error = new JSONObject();
        error.put("code", HttpStatus.INTERNAL_SERVER_ERROR);
        error.put("message", msg);
        return error;
    }

}
