package com.iformall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.IdWorker;
import com.iformall.domain.po.WxAppinfo;
import com.iformall.domain.po.WxAuthorizerInfo;
import com.iformall.domain.po.WxWeappExtSet;
import com.iformall.domain.vo.WxWeappInfo;
import com.iformall.enums.EnumAppType;
import com.iformall.enums.EnumCarSupport;
import com.iformall.mapper.WxAppinfoMapper;
import com.iformall.mapper.WxAuthorizerInfoMapper;
import com.iformall.mapper.WxWeappExtSetMapper;
import com.iformall.service.WxAuthorizerInfoService;
import com.iformall.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class WxAuthorizerInfoServiceImpl implements WxAuthorizerInfoService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
    WxAuthorizerInfoMapper authorizerInfoMapper;

	@Autowired
    WxWeappExtSetMapper weappExtSetMapper;

	@Autowired
    WxAppinfoMapper appinfoMapper;


    @Override
    public PageInfo<WxAuthorizerInfo> listAsPage(WxAuthorizerInfo record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> authorizerInfoMapper.findList(record));
    }

    @Override
    public PageInfo<WxWeappInfo> listVoAsPage(WxWeappInfo record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> authorizerInfoMapper.findVoList(record));
    }

    @Override
    public List<WxWeappInfo> getList(WxWeappInfo record) {
        return authorizerInfoMapper.findVoList(record);
    }

    @Override
    public WxAuthorizerInfo getById(Long id) {
        return authorizerInfoMapper.selectById(id);
    }

    @Override
    public WxAuthorizerInfo getByAppId(String appId) {
        WxAuthorizerInfo authQ = new WxAuthorizerInfo();
        authQ.setAuthorizerAppid(appId);
        try {
            WxAuthorizerInfo authorizerInfo = authorizerInfoMapper.selectOne(new QueryWrapper(authQ));
            return authorizerInfo;
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return null;
    }

    @Override
    public void saveOrUpdate(WxAuthorizerInfo record) {
        // 1. 添加到wx_authorizer_info表中
        // 2. 同时在wx_weapp_ext_set中添加一条记录
        // 3. 同时在wx_weapp_code_status中添加一条记录
        Date curDate = new Date();
        logger.info(record.toString());
        WxAuthorizerInfo authorizerInfo = null;
        try {
            WxAuthorizerInfo aiQ = new WxAuthorizerInfo();
            aiQ.setAuthorizerAppid(record.getAuthorizerAppid());
            authorizerInfo = authorizerInfoMapper.selectOne(new QueryWrapper(aiQ));
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        if (authorizerInfo == null) {
            WxAppinfo appinfo = appinfoMapper.findByAppId(record.getAuthorizerAppid());
            if(appinfo != null) {
                record.setId(appinfo.getId());
                record.setTenantId(appinfo.getTenantId());
                record.setType(appinfo.getType());
            } else {
                //record.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                final IdWorker idWorker = IdWorker.get();
                record.setId(idWorker.nextId());
            }
        	record.setCreateTime(curDate);
        	record.setUpdateTime(curDate);
        	// 授权表 添加
            authorizerInfoMapper.insert(record);
            if(appinfo.getType() != null) {
                if (appinfo.getType().equals(EnumAppType.C.getCode()) ||
                        appinfo.getType().equals(EnumAppType.B.getCode())) {
                    // C/B 扩展配置表添加
                    WxWeappExtSet weappExtSet = new WxWeappExtSet();
                    weappExtSet.setId(record.getId());
                    weappExtSet.setAppId(record.getAuthorizerAppid());
                    if (appinfo.getType().equals(EnumAppType.C.getCode())) {
                        weappExtSet.setCarSupport(EnumCarSupport.NOCAR.getCode());
                    }
                    if (appinfo != null) {
                        weappExtSet.setType(appinfo.getType());
                    }
                    weappExtSet.setCreateDate(curDate);
                    weappExtSet.setUpdateDate(curDate);
                    weappExtSetMapper.insert(weappExtSet);
                }
            }
        } else {
            record.setId(authorizerInfo.getId());
            record.setUpdateTime(curDate);
            logger.info(record.toString());
            authorizerInfoMapper.updateById(record);
        }
    }

    @Override
    public void deleteById(Long id) {
        authorizerInfoMapper.deleteById(id);
    }

    @Override
    public int updateReleaseVersion(WxAuthorizerInfo record) {
        return authorizerInfoMapper.updateReleaseInfo(record);
    }

    @Override
    public int updateBaseVersion(WxAuthorizerInfo record) {
        Date curDate = new Date();
        WxAuthorizerInfo info = new WxAuthorizerInfo();
        info.setId(record.getId());
        info.setBaseStatus(record.getBaseStatus());
        info.setBaseTime(curDate);
        info.setUpdateTime(curDate);
        return authorizerInfoMapper.updateById(info);
    }

    @Override
    public int updateDomainUrl(WxAuthorizerInfo record) {
        Date curDate = new Date();
        WxAuthorizerInfo info = new WxAuthorizerInfo();
        info.setId(record.getId());
        info.setDomainStatus(record.getDomainStatus());
        info.setDomainTime(curDate);
        info.setUpdateTime(curDate);
        return authorizerInfoMapper.updateById(info);
    }

    @Override
    public int updateWebDomainUrl(WxAuthorizerInfo record) {
        Date curDate = new Date();
        WxAuthorizerInfo info = new WxAuthorizerInfo();
        info.setId(record.getId());
        info.setWebdomainStatus(record.getWebdomainStatus());
        info.setWebdomainTime(curDate);
        info.setUpdateTime(curDate);
        return authorizerInfoMapper.updateById(info);
    }

    @Override
    public int updateTemplate(WxAuthorizerInfo record) {
        Date curDate = new Date();
        WxAuthorizerInfo info = new WxAuthorizerInfo();
        info.setId(record.getId());
        info.setTemplateStatus(record.getTemplateStatus());
        info.setTemplateTime(curDate);
        info.setUpdateTime(curDate);
        return authorizerInfoMapper.updateById(info);
    }

    @Override
    public int updateOpenAppid(WxAuthorizerInfo record) {
        Date curDate = new Date();
        WxAuthorizerInfo info = new WxAuthorizerInfo();
        info.setId(record.getId());
        info.setOpenAppid(record.getOpenAppid());
        info.setBindOpenTime(curDate);
        return authorizerInfoMapper.updateById(info);
    }

    @Override
    public int removeOpenAppid(WxAuthorizerInfo record) {
        return authorizerInfoMapper.removeOpenAppId(record);
    }

    @Override
    public int updateAuthAppidInfo(WxAuthorizerInfo record) {
        WxAuthorizerInfo authorizerInfo = authorizerInfoMapper.findByAppid(record.getAuthorizerAppid());
        if(authorizerInfo != null) {
            record.setId(authorizerInfo.getId());
            return authorizerInfoMapper.updateById(record);
        } else {
            /// TODO
        }
        return 0;


    }

    @Override
    public int updateAuthBackInfo(WxAuthorizerInfo record) {
        WxAuthorizerInfo authorizerInfo = authorizerInfoMapper.findByAppid(record.getAuthorizerAppid());
        if(authorizerInfo != null) {
            if(DateUtils.isSameMonth(authorizerInfo.getAuditBackTime(), record.getAuditBackTime()))
            {
                // 同一个月，增加
                record.setId(authorizerInfo.getId());
                return authorizerInfoMapper.updateAuthBackInfo(record);
            } else {
                // 不是同一个月，重置authBackNum
                record.setAuditBackNum(1);
                return authorizerInfoMapper.updateById(record);
            }
        } else {
            /// TODO
        }
        return 0;
    }
    
    
    
    
    
}
