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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminCompanyPayConfigBean;
import com.gree.mall.manager.bean.admin.AdminCompanyPayConfigVO;
import com.gree.mall.manager.bean.admin.AdminCompanyVO;
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.service.AdminCompanyWechatService;
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.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class AdminCompanyPayConfigLogic {


    private final CommonMapper commonMapper;
    private final CommonLogic commonLogic;
    private final AdminCompanyWechatService adminCompanyWechatService;


    public IPage<AdminCompanyPayConfigVO> page(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        FieldUtils.supplyParam(zfireParamBean, AdminCompanyPayConfigVO.class, adminUser);
        return commonMapper.adminCompanyPayConfigPage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
    }

    public AdminCompanyPayConfigBean detail(String id) {
        final AdminCompanyWechat companyWechat = adminCompanyWechatService.getById(id);
        AdminCompanyPayConfigBean bean = new AdminCompanyPayConfigBean();
        BeanUtils.copyProperties(companyWechat, bean);
        return bean;
    }

    public void add(AdminCompanyPayConfigBean bean) {
        this.checkParams(bean);
        bean.insert();
    }

    public void checkParams(AdminCompanyPayConfigBean bean) {
        if (StringUtils.isBlank(bean.getCompanyName())) {
            throw new RemoteServiceException("电子商户名称不能为空");
        }
        if (StringUtils.isBlank(bean.getAppId())) {
            throw new RemoteServiceException("appid不能为空");
        }
        if (StringUtils.isBlank(bean.getSubAppId())) {
            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.getMchKey())) {
            throw new RemoteServiceException("key不能为空");
        }
        if (StringUtils.isBlank(bean.getSubSecret())) {
            throw new RemoteServiceException("子商户密钥不能为空");
        }
    }

    public void edit(AdminCompanyPayConfigBean bean) {
        this.checkParams(bean);
        bean.updateById();
    }

    public IPage<AdminCompanyVO> adminCompanyPage(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        FieldUtils.supplyParam(zfireParamBean, AdminCompanyPayConfigVO.class, adminUser);
        return commonMapper.adminCompanyPage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
    }
}
