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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeequan.jeepay.agent.ctrl.CommonCtrl;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.MchInfo;
import com.jeequan.jeepay.db.entity.MchModifyApplymentEntity;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

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

    @Autowired private MchModifyApplymentService modifyApplymentService;


    @GetMapping
    public ApiRes list() {
        MchModifyApplymentEntity modifyApplyment = getObject(MchModifyApplymentEntity.class);
        JSONObject paramJSON = getReqParamJSON();
        //查询该服务商下的所有用户数据（包括自己和下级服务商）
        String currentAgentNo = getCurrentAgentNo();
        List<String> subAgentNoList = agentInfoService.queryAllSubAgentNo(currentAgentNo);
        List<String> mchInfoList = mchInfoService.list(MchInfo.gw()
                        .in(MchInfo::getAgentNo, subAgentNoList)
                .eq(MchInfo::getState,1))
                .stream()
                .map(MchInfo::getMchNo).collect(Collectors.toList());
        modifyApplyment.setMchInfoList(mchInfoList);
        Page<MchModifyApplymentEntity> pages = modifyApplymentService.pageList(getIPage(), modifyApplyment,paramJSON);
        return ApiRes.page(pages);
    }

    @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);
    }
}
