package com.iformall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.domain.po.WxAuthorizerInfo;
import com.iformall.domain.po.WxWeappReleaseStatus;
import com.iformall.domain.vo.WxWeappReleaseStatusVo;
import com.iformall.enums.EnumWeappReleaseStatus;
import com.iformall.mapper.WxAuthorizerInfoMapper;
import com.iformall.mapper.WxWeappReleaseStatusMapper;
import com.iformall.service.WxWeappReleaseStatusService;
import org.omg.CORBA.INTERNAL;
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;
import java.util.Map;

@Service
public class WxWeappReleaseStatusServiceImpl implements WxWeappReleaseStatusService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
    WxWeappReleaseStatusMapper weappReleaseStatusMapper;

	@Autowired
    WxAuthorizerInfoMapper authorizerInfoMapper;


    @Override
    public PageInfo<WxWeappReleaseStatusVo> listAsPage(WxWeappReleaseStatusVo record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> weappReleaseStatusMapper.findVoList(record));
    }

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

    @Override
    public List<Map> getVersionList(Integer type) {
        return weappReleaseStatusMapper.findUserVersions(type);
    }

    @Override
    public WxWeappReleaseStatus getById(Long id) {
        return weappReleaseStatusMapper.selectByPrimaryKey(id);
    }

    @Override
    public WxWeappReleaseStatus getByAppId(String appId) {
        WxWeappReleaseStatus weappCodeStatusQ = new WxWeappReleaseStatus();
        weappCodeStatusQ.setAppId(appId);
        return weappReleaseStatusMapper.selectOne(weappCodeStatusQ);
    }

    @Override
    public void saveStatus(WxWeappReleaseStatus record) {
        logger.info("RELEASE: " + record.toString());
        weappReleaseStatusMapper.insertSelective(record);
    }

    @Override
    public void updateStatus(WxWeappReleaseStatus record) {
        Date curDate = new Date();
        logger.info("RELEASE: " + record.toString());
        // 更新 小程序 状态
        if(record.getReleaseStatus().equals(EnumWeappReleaseStatus.SUCCESS.getCode())) { // 发布成功
            WxAuthorizerInfo updateInfo = new WxAuthorizerInfo();
            updateInfo.setAuthorizerAppid(record.getAppId());
            updateInfo.setCurrentVersion(record.getUserVersion());
            updateInfo.setCurrentDesc(record.getVersionDesc());
            updateInfo.setReleaseTime(curDate);
            updateInfo.setUpdateTime(curDate);
            authorizerInfoMapper.updateReleaseInfo(updateInfo);
        }
        // 发布回退，退回上次发布状态
        if(record.getReleaseStatus().equals(EnumWeappReleaseStatus.BACK.getCode())) {
            try {
                WxAuthorizerInfo appQ = new WxAuthorizerInfo();
                appQ.setAuthorizerAppid(record.getAppId());
                WxAuthorizerInfo authorizerInfo = authorizerInfoMapper.selectOne(appQ);
                if(authorizerInfo != null) {
                    record.setUserVersion(authorizerInfo.getCurrentVersion());
                    record.setVersionDesc(authorizerInfo.getCurrentDesc());
                    record.setReleaseTime(authorizerInfo.getReleaseTime());
                }
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
        }
        weappReleaseStatusMapper.updateStatus(record);
    }

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