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

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gic.commons.util.EntityUtil;
import com.gic.commons.util.ToolUtil;
import com.gic.haoban.manage.api.dto.SecretSettingDTO;
import com.gic.haoban.manage.api.dto.WxEnterpriseQwDTO;
import com.gic.haoban.manage.api.enums.SecretTypeEnum;
import com.gic.haoban.manage.service.dao.mapper.SecretSettingMapper;
import com.gic.haoban.manage.service.entity.TabSecretSetting;
import com.gic.haoban.manage.service.service.SecretSettingService;
import com.gic.haoban.manage.service.service.WxEnterpriseService;

/**
 * Created 2020/6/3.
 *
 * @author hua
 */
@Service
public class SecretSettingServiceImpl implements SecretSettingService {

    private static final Logger logger = LoggerFactory.getLogger(SecretSettingServiceImpl.class);

    @Autowired
    private SecretSettingMapper secretSettingMapper;
    @Autowired
    private WxEnterpriseService wxEnterpriseService ;

    @Override
    public boolean saveSecretSetting(SecretSettingDTO secretSetting) {
        if (secretSetting.getSecretId() != null) {
            secretSetting.setUpdateTime(new Date());
            secretSettingMapper.updateByPrimaryKeySelective(EntityUtil.changeEntityByJSON(TabSecretSetting.class, secretSetting));
            this.wxEnterpriseService.deleteQwInfoCache(secretSetting.getWxEnterpriseId());
            return true;
        } else {
            secretSetting.setSecretId(ToolUtil.randomUUID());
            secretSetting.setCreateTime(new Date());
            secretSetting.setUpdateTime(new Date());
            secretSetting.setStatusFlag(1);
            secretSettingMapper.insert(EntityUtil.changeEntityByJSON(TabSecretSetting.class, secretSetting));
            this.wxEnterpriseService.deleteQwInfoCache(secretSetting.getWxEnterpriseId());
            return true;
        }
    }

    @Override
    public boolean delSecretSetting(String secretId) {
        TabSecretSetting tabSecretSetting = new TabSecretSetting();
        tabSecretSetting.setSecretId(secretId);
        tabSecretSetting.setStatusFlag(0);
        tabSecretSetting.setUpdateTime(new Date());
        int i = secretSettingMapper.updateByPrimaryKeySelective(tabSecretSetting);

        TabSecretSetting s = this.secretSettingMapper.selectByPrimaryKey(secretId) ;
        if(null != s && null !=s.getWxEnterpriseId()) {
            this.wxEnterpriseService.deleteQwInfoCache(s.getWxEnterpriseId());
        }
        return i > 0;
    }

    @Override
    public List<SecretSettingDTO> listSecretSetting(String wxEnterpriseId) {
        List<TabSecretSetting> list = secretSettingMapper.listSecretSetting(wxEnterpriseId);
        return EntityUtil.changeEntityListByJSON(SecretSettingDTO.class, list);
    }

    @Override
    public SecretSettingDTO getSecretSetting(String wxEnterpriseId, int secretType) {
        if (secretType == SecretTypeEnum.MEMBER_WAPP.getVal()) {
            logger.info("会员小程序配置使用getMemberSecretSetting接口");
            return null;
        }
        WxEnterpriseQwDTO qwDTO = this.wxEnterpriseService.getQwInfo(wxEnterpriseId) ;
        if (qwDTO == null) {
            return null;
        }
        // 如果是纯自建,使用自建的token
        if(qwDTO.isSelf() && StringUtils.isNotBlank(qwDTO.getUrlHost())) {
            secretType = SecretTypeEnum.SELF_APP.getVal() ;
        }
        TabSecretSetting secretSetting = secretSettingMapper.getSecretSetting(wxEnterpriseId, secretType, null);
        return EntityUtil.changeEntityByJSON(SecretSettingDTO.class, secretSetting);
    }

    @Override
    public List<SecretSettingDTO> getSecretSettingList(List<String> wxEnterpriseIdList, int secretType) {
        List<TabSecretSetting> secretSettingList = secretSettingMapper.getSecretSettingList(wxEnterpriseIdList, secretType, null);
        return EntityUtil.changeEntityListByJSON(SecretSettingDTO.class, secretSettingList);

    }

    @Override
    public SecretSettingDTO getMemberSecretSetting(String wxEnterpriseId, String enterpriseId) {
        if (StringUtils.isNotBlank(enterpriseId) && !"-1".equals(enterpriseId)) {
            TabSecretSetting secretSetting = secretSettingMapper.getSecretSetting(wxEnterpriseId, SecretTypeEnum.MEMBER_WAPP.getVal(), enterpriseId);
            return EntityUtil.changeEntityByJSON(SecretSettingDTO.class, secretSetting);
        }
        TabSecretSetting secretSetting = secretSettingMapper.getSecretSetting(wxEnterpriseId, SecretTypeEnum.MEMBER_WAPP.getVal(), null);
        return EntityUtil.changeEntityByJSON(SecretSettingDTO.class, secretSetting);
    }

    @Override
    public SecretSettingDTO getSecretSettingById(String secretId) {
        TabSecretSetting secretSetting = secretSettingMapper.selectByPrimaryKey(secretId);
        return EntityUtil.changeEntity(SecretSettingDTO.class, secretSetting);
    }
    
    @Override
    public List<SecretSettingDTO> exsitGicEidMemberApp(String wxEnterpriseId) {
        return this.secretSettingMapper.exsitGicEidMemberApp(wxEnterpriseId);
    }
}
