package com.jeequan.jeepay.mch.ctrl.merchant;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.MchModifyApplymentEntity;
import com.jeequan.jeepay.mch.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.MchModifyApplymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * TODO
 *
 * @author crystal
 * @date 2023/12/27 15:36
 */
@RestController
@RequestMapping("/api/mchModifyApplyment")
public class MchModifyApplymentController extends CommonCtrl {

    @Autowired
    private MchModifyApplymentService modifyApplymentService;


    @PreAuthorize("hasAuthority('ENT_MCH_MODIFY_LIST')")
    @GetMapping
    public ApiRes list() {
        MchModifyApplymentEntity modifyApplyment = getObject(MchModifyApplymentEntity.class);
        modifyApplyment.setMchNo(getCurrentMchNo());
        Page<MchModifyApplymentEntity> pages = modifyApplymentService.pageList(getIPage(), modifyApplyment,null);
        return ApiRes.page(pages);
    }


    @PostMapping(value = "/audit")
    public ApiRes audit() {
        String modifyApplymentId = getValStringRequired("modifyApplymentId");
        Boolean auditFlag = getValRequired("state", Boolean.class);
        String remark = getValString("remark");

        modifyApplymentService.audit(modifyApplymentId, auditFlag, remark, true);
        return ApiRes.ok();
    }

    @GetMapping("/result/{modifyApplyId}")
    public ApiRes getResult(@PathVariable("modifyApplyId") String modifyApplyId) {
        modifyApplymentService.getResult(modifyApplyId);

        return ApiRes.ok();
    }

    @PreAuthorize("hasAuthority('ENT_MCH_APPLYMENT_VIEW')")
    @GetMapping("/{recordId}")
    public ApiRes detail(@PathVariable("recordId") String recordId) {
        Byte originData = getValByteDefault("originData", CS.NO);
        MchModifyApplymentEntity record;
        if (CS.NO == originData) {
            record = modifyApplymentService.getInfoById(recordId);
        } else {
            record = modifyApplymentService.getById(recordId);
        }

        return ApiRes.ok(record);
    }
}
