package com.gic.haoban.manage.service.service.out.impl;

import com.gic.commons.util.EntityUtil;
import com.gic.haoban.manage.api.dto.WxApplicationDTO;
import com.gic.haoban.manage.api.service.WxApplicationApiService;
import com.gic.haoban.manage.service.entity.TabHaobanWxApplication;
import com.gic.haoban.manage.service.service.WxApplicationService;
import com.gic.haoban.manage.service.service.WxEnterpriseService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by tgs on 2020/2/22.
 */
@Service("wxApplicationApiService")
public class WxApplicationApiServiceImpl implements WxApplicationApiService {
    private static final Logger log = LogManager.getLogger(WxApplicationApiServiceImpl.class);

    @Autowired
    private WxApplicationService wxApplicationService;
    @Autowired
    private WxEnterpriseService wxEnterpriseService;

    @Override
    public void addSuite(WxApplicationDTO dto) {
        TabHaobanWxApplication tab = EntityUtil.changeEntityByJSON(TabHaobanWxApplication.class, dto);
        this.wxApplicationService.insert(tab);
    }

    @Override
    public void cancelSuite(String corpId, String suiteId) {
        this.wxApplicationService.cancalWxApplication(corpId, suiteId);
        log.info("取消授权，corpId：{}，suiteId：{}", corpId, suiteId);
        TabHaobanWxApplication wxApplication = this.wxApplicationService.selectByCorpIdAndApplicationType(corpId, 2);
        if (wxApplication != null) {
            //如果取消好办小程序应用授权，临时解除绑定
            this.wxEnterpriseService.unbind(wxApplication.getWxEnterpriseId());
            log.info("取消好办小程序应用授权，corpId:{},suiteId:{}", corpId, suiteId);
        }
    }

    @Override
    public WxApplicationDTO selectByWxEnterpriseIdAndApplicationType(String wxEnterpriseId, int type) {
        return EntityUtil.changeEntityByJSON(WxApplicationDTO.class, wxApplicationService.selectByWxEnterpriseIdAndApplicationType(wxEnterpriseId, type));
    }

    @Override
    public WxApplicationDTO selectByCorpid(String corpId) {
        return EntityUtil.changeEntityByJSON(WxApplicationDTO.class, wxApplicationService.selectByCorpId(corpId));
    }
}
