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

import com.gic.haoban.common.utils.StringUtil;
import com.gic.haoban.manage.service.dao.mapper.WxApplicationMapper;
import com.gic.haoban.manage.service.dao.mapper.WxEnterpriseMapper;
import com.gic.haoban.manage.service.entity.TabHaobanWxApplication;
import com.gic.haoban.manage.service.entity.TabHaobanWxEnterprise;
import com.gic.haoban.manage.service.service.WxApplicationService;
import com.gic.haoban.manage.service.service.WxEnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class WxApplicationServiceImpl implements WxApplicationService {
	
	@Autowired
	private WxApplicationMapper mapper;
	@Autowired
	private WxEnterpriseService wxEnterpriseService ;
	@Autowired
	private WxEnterpriseMapper wxEnterpriseMapper;

	@Override
	public TabHaobanWxApplication selectBySiteIdAndWxEnterpriseId(String siteId, String wxEnterpriseId) {
		
		return mapper.selectBySiteIdAndWxEnterpriseId(siteId,wxEnterpriseId);
	}

	@Override
	public void insert(TabHaobanWxApplication tab) {
		Date now = new Date();
		tab.setCreateTime(now);
		tab.setUpdateTime(now);
		tab.setStatusFlag(1);
		tab.setWxApplicationId(StringUtil.randomUUID());
		mapper.insertSelective(tab);
		this.wxEnterpriseService.deleteQwInfoCache(tab.getWxEnterpriseId());
	}

	@Override
	public void update(TabHaobanWxApplication tab) {
		tab.setUpdateTime(new Date());
		mapper.updateByPrimaryKeySelective(tab);
		this.wxEnterpriseService.deleteQwInfoCache(tab.getWxEnterpriseId());
	}

    @Override
    public TabHaobanWxApplication selectByWxEnterpriseIdAndApplicationType(String wxEnterpriseId, int applicationType) {
        return mapper.selectByWxEnterpriseIdAndApplicationType(wxEnterpriseId, applicationType);
    }

    @Override
    public int cancalWxApplication(String corpId, String suiteId) {
        int i=  this.mapper.cancelWxApplication(corpId, suiteId);
		TabHaobanWxEnterprise tab = this.wxEnterpriseMapper.getEnterpriseBycorpId(corpId,1) ;
		if(null != tab) {
			this.wxEnterpriseService.deleteQwInfoCache(tab.getWxEnterpriseId());
		}
		return i;
    }

    @Override
    public TabHaobanWxApplication selectByCorpId(String corpid) {
        return mapper.selectByCorpId(corpid,null);
    }

	@Override
	public TabHaobanWxApplication selectByCorpIdAndApplicationType(String corpId, int applicationType) {
		return mapper.selectByCorpId(corpId,applicationType);
	}

}
