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

import cn.hutool.core.util.ObjUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeequan.jeepay.agent.ctrl.CommonCtrl;
import com.jeequan.jeepay.core.aop.MethodLog;
import com.jeequan.jeepay.core.constants.ApiCodeEnum;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.MchConfig;
import com.jeequan.jeepay.service.impl.MchConfigService;
import com.jeequan.jeepay.service.impl.MchInfoService;
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 zx
 * @date 2021-06-16 09:15
 */
@RestController
@RequestMapping("/api/mchConfig")
public class MchConfigController extends CommonCtrl {

    @Autowired
    private MchConfigService mchConfigService;
    @Autowired
    private MchInfoService mchInfoService;

    /**
     * @Author: zx
     * @Description: 查询商户配置
     * @Date: 9:59 2021/6/16
     */
    @PreAuthorize("hasAuthority('ENT_MCH_CONFIG_PAGE')")
    @GetMapping
    public ApiRes list() {

        String groupKey = getValString("groupKey");

        String mchNo = getValStringRequired("mchNo");
        String mchAppId = getValString("mchAppId");
        String mchApplyId = getValString("mchApplyId");

        LambdaQueryWrapper<MchConfig> queryWrapper = MchConfig.gw()
                .eq(MchConfig::getMchNo, mchNo)
                .eq(!ObjUtil.isEmpty(mchAppId), MchConfig::getMchAppId, mchAppId)
                .eq(!ObjUtil.isEmpty(mchApplyId), MchConfig::getMchApplyId, mchApplyId);
        if (StringUtils.isNotBlank(groupKey)) {
            queryWrapper.eq(MchConfig::getGroupKey, groupKey);
        }

        List<MchConfig> list = mchConfigService.list(queryWrapper);
        return ApiRes.ok(list);
    }

    /**
     * @Author: zx
     * @Description: 根据configKey查询商户配置
     * @Date: 9:59 2021/6/16
     */
    @PreAuthorize("hasAuthority('ENT_MCH_CONFIG_EDIT')")
    @GetMapping("/{configKey}")
    public ApiRes get(@PathVariable("configKey") String configKey) {

        MchConfig mchConfig = mchConfigService.getOne(MchConfig.gw().eq(MchConfig::getMchNo, getValStringRequired("mchNo")).eq(MchConfig::getConfigKey, configKey));
        return ApiRes.ok(mchConfig);
    }

    /**
     * @Author: zx
     * @Description: 根据configKey更新商户配置信息
     * @Date: 10:11 2021/6/16
     */
    /*@PreAuthorize("hasAuthority('ENT_MCH_CONFIG_EDIT')")
    @MethodLog(remark = "更新商户配置")
    @PutMapping("/{configKey}")
    public ApiRes updateByConfigKey(@PathVariable("configKey") String configKey) {
        MchConfig mchConfig = getObject(MchConfig.class);
        mchConfig.setConfigKey(configKey);

        int count = mchConfigService.saveOrUpdateConfig(mchConfig, getCurrentMchNo());
        if(count <= 0) {
            return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_UPDATE);
        }
        return ApiRes.ok();
    }*/

    /**
     * @author: zx
     * @Description: 批量更新商户配置信息
     * @Date: 10:11 2021/6/16
     */
    @PreAuthorize("hasAuthority('ENT_MCH_CONFIG_PAGE')")
    @MethodLog(remark = "批量更新商户配置信息")
    @PutMapping("/{groupKey}")
    public ApiRes updateBatch(@PathVariable("groupKey") String groupKey) {

        String params = getValStringRequired("configData");

        List<MchConfig> mchConfigList = JSONArray.parseArray(params, MchConfig.class);
        String mchNo = getValStringRequired("mchNo");
        String mchAppId = getValString("mchAppId");
        String mchApplyId = getValString("mchApplyId");

        int update = mchConfigService.updateBatch(mchConfigList, mchNo, mchAppId, mchApplyId, groupKey);
        if (update <= 0) {
            return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_UPDATE);
        }

        return ApiRes.ok();
    }


}
