package com.gree.mall.manager.logic.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminCompanyPayConfigVO;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.commonmapper.CommonMapper;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.AdminCompanyWechat;
import com.gree.mall.manager.plus.entity.AdminCompanyWechatPayConfig;
import com.gree.mall.manager.plus.service.AdminCompanyWechatPayConfigService;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Slf4j
@Service
@RequiredArgsConstructor
public class AdminCompanyPayConfigLogic {

    private final CommonMapper commonMapper;
    private final CommonLogic commonLogic;
    private final AdminCompanyWechatPayConfigService adminCompanyWechatPayConfigService;


    public IPage<AdminCompanyPayConfigVO> list(ZfireParamBean zfireParamBean) {
        FieldUtils.supplyParam(zfireParamBean, AdminCompanyPayConfigVO.class, commonLogic.getAdminUser());
        IPage<AdminCompanyPayConfigVO> page = commonMapper.adminCompanyPayConfigList(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);

        return page;
    }

    public AdminCompanyWechatPayConfig detail(String id) {
        return adminCompanyWechatPayConfigService.getById(id);
    }

    @Transactional
    public void save(AdminCompanyWechatPayConfig bean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        AdminCompanyWechat companyWechat = adminUser.getAdminCompanyWechat();

        if (Objects.nonNull(companyWechat)) {
            bean.setCompanyWechatId(companyWechat.getCompanyWechatId());
            bean.setCompanyName(companyWechat.getCompanyName());
        }

        if (StringUtils.isBlank(bean.getPayMerchantName())) {
            throw new RemoteServiceException("电子商户名称不能为空");
        }

        if (StringUtils.isBlank(bean.getAppId())) {
            throw new RemoteServiceException("appid不能为空");
        }

        if (StringUtils.isBlank(bean.getMchId())) {
            throw new RemoteServiceException("mchid不能为空");
        }

        if (StringUtils.isBlank(bean.getSubMchId())) {
            throw new RemoteServiceException("子商户mchid不能为空");
        }

        if (StringUtils.isBlank(bean.getSubAppId())) {
            throw new RemoteServiceException("子商户appid不能为空");
        }

        if (StringUtils.isBlank(bean.getMchKey())) {
            throw new RemoteServiceException("key不能为空");
        }

        if (Objects.isNull(bean.getStatus())) {
            throw new RemoteServiceException("请选择状态");
        }

        if (StringUtils.isBlank(bean.getAppletAppid())) {
            throw new RemoteServiceException("小程序appid为空");
        }

        if (StringUtils.isBlank(bean.getAppletSecret())) {
            throw new RemoteServiceException("小程序密钥不能为空");
        }

        if (StringUtils.isBlank(bean.getId())) {
            bean.setId(IdWorker.getIdStr());
        }

        bean.insertOrUpdate();

    }
}
