package com.jeequan.jeepay.mgr.ctrl.transfer;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.TransferInterfaceConfigEntity;
import com.jeequan.jeepay.db.entity.TransferInterfaceDefineEntity;
import com.jeequan.jeepay.mgr.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.TransferInterfaceConfigService;
import com.jeequan.jeepay.service.impl.TransferInterfaceDefineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * TODO
 *
 * @author deng
 * @since 2024/4/25
 */
@RestController
@RequestMapping("api/transferDefine")
public class TransferDefineController extends CommonCtrl {

    @Autowired
    private TransferInterfaceDefineService transferInterfaceDefineService;

    @Autowired
    private TransferInterfaceConfigService transferInterfaceConfigService;

    @GetMapping("/definePage")
    public ApiRes transferDefinePage() {
        Page<TransferInterfaceDefineEntity> iPage = getIPage();
        Page<TransferInterfaceDefineEntity> page = transferInterfaceDefineService.page(iPage);

        return ApiRes.ok(page);
    }

    @PostMapping("/define")
    public ApiRes saveDefine() {
        TransferInterfaceDefineEntity defineEntity = getObject(TransferInterfaceDefineEntity.class);

        String[] wayCodeStrs = defineEntity.getExt().getString("wayCodeStrs").split(",");

        JSONArray wayCodeArr = new JSONArray();
        defineEntity.setWayCodes(wayCodeArr);
        for (String wayCodeStr : wayCodeStrs) {
            JSONObject param = new JSONObject();
            wayCodeArr.add(param);
            param.put("wayCode", wayCodeStr);
        }

        // 不能修改transIfCode值
        if (defineEntity.getId() != null) {
            defineEntity.setTransIfCode(null);
        }

        transferInterfaceDefineService.saveDefine(defineEntity);

        return ApiRes.ok();
    }

    @GetMapping("/define/{transferIfCode}")
    public ApiRes detail(@PathVariable("transferIfCode") String transferIfCode) {
        TransferInterfaceDefineEntity result = transferInterfaceDefineService.lambdaQuery()
                .eq(TransferInterfaceDefineEntity::getTransIfCode, transferIfCode)
                .one();
        return ApiRes.ok(result);
    }

    @DeleteMapping("/define/{id}")
    public ApiRes saveDefine(@PathVariable("id") Long id) {
        transferInterfaceDefineService.removeDefine(id);

        return ApiRes.ok();
    }

    @GetMapping("/instancePage")
    public ApiRes transferInstancePage() {
        Page<TransferInterfaceConfigEntity> iPage = getIPage();
        Page<TransferInterfaceConfigEntity> page = transferInterfaceConfigService.page(iPage);

        return ApiRes.ok(page);
    }

    @GetMapping("/instance/code/{code}")
    public ApiRes transferInstanceByCode(@PathVariable("code") String code) {

        String isvNo = getValStringRequired("isvNo");

        TransferInterfaceConfigEntity existData = transferInterfaceConfigService.getTransferConfig(code, isvNo, CS.SYS_ROLE_TYPE.ISV);

        if (existData == null) {
            existData = new TransferInterfaceConfigEntity();
            existData.setTransIfCode(code);
        }

        TransferInterfaceDefineEntity defineEntity = transferInterfaceDefineService.lambdaQuery()
                .eq(TransferInterfaceDefineEntity::getTransIfCode, code)
                .one();

        JSONArray ifDefineArray;
        try {
            ifDefineArray = JSONArray.parseArray(defineEntity.getIsvParams());
        } catch (Exception e) {
            ifDefineArray = new JSONArray();
        }

        existData.addExt("ifDefineArray", ifDefineArray);

        return ApiRes.ok(existData);
    }

    @PostMapping("/instance")
    public ApiRes saveInstance() {
        TransferInterfaceConfigEntity configEntity = getObject(TransferInterfaceConfigEntity.class);
        transferInterfaceConfigService.saveConfig(configEntity);
        return ApiRes.ok();
    }

    @DeleteMapping("/instance/{id}")
    public ApiRes saveInstance(@PathVariable("id") Long id) {
        transferInterfaceConfigService.removeConfig(id);

        return ApiRes.ok();
    }
}
