package com.huiju.estateagents.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiju.estateagents.channel.sysChannel.entity.TaChannelProxy;
import com.huiju.estateagents.channel.sysChannel.mapper.TaChannelProxyMapper;
import com.huiju.estateagents.common.StringUtils;
import com.huiju.estateagents.common.WxUtils;
import com.huiju.estateagents.entity.TaChannelAppRelation;
import com.huiju.estateagents.entity.TaMiniapp;
import com.huiju.estateagents.entity.TaMiniappTemplate;
import com.huiju.estateagents.mapper.TaChannelAppRelationMapper;
import com.huiju.estateagents.mapper.TaMiniappMapper;
import com.huiju.estateagents.mapper.TaMiniappTemplateMapper;
import com.huiju.estateagents.redpack.entity.TaOrgAccount;
import com.huiju.estateagents.redpack.mapper.TaOrgAccountMapper;
import com.huiju.estateagents.service.ITaMiniappService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 小程序表  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-09-11
 */
@Service
public class TaMiniappServiceImpl extends ServiceImpl<TaMiniappMapper, TaMiniapp> implements ITaMiniappService {
    @Autowired
    TaMiniappMapper taMiniappMapper;

    @Autowired
    TaMiniappTemplateMapper taMiniappTemplateMapper;

    @Autowired
    WxUtils wxUtils;

    @Autowired
    private TaChannelAppRelationMapper taChannelAppRelationMapper;

    @Autowired
    private TaChannelProxyMapper taChannelProxyMapper;

    @Autowired
    private TaOrgAccountMapper taOrgAccountMapper;

    @Override
    public TaMiniapp saveInfoWithTpls(TaMiniapp taMiniapp, Integer channelId) throws Exception {
        if (StringUtils.isEmpty(taMiniapp.getMiniappId())) {
            throw new Exception("请填写小程序 AppID");
        }

        if (StringUtils.isEmpty(taMiniapp.getSecret())) {
            throw new Exception("请填写小程序 Secret");
        }

        if (StringUtils.isEmpty(taMiniapp.getName())) {
            throw new Exception("请填写小程序 名称");
        }

        if (null == taMiniapp.getOrgId() || taMiniapp.getOrgId() == 0) {
            throw new Exception("未知的小程序所属对象");
        }

        TaMiniapp orign = this.getById(taMiniapp.getMiniappId());
        if (null != orign) {
            if (!this.updateById(taMiniapp)) {
                throw new Exception("保存小程序信息出错");
            }
        } else if (!this.save(taMiniapp)) {
            throw new Exception("保存小程序信息出错");
        }

        if (null != taMiniapp.getTpls()) {
            // 插入之前先删除
            QueryWrapper<TaMiniappTemplate> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("miniapp_id", taMiniapp.getMiniappId());
            taMiniappTemplateMapper.delete(queryWrapper);

            for(TaMiniappTemplate tpl: taMiniapp.getTpls()) {
                tpl.setMiniappId(taMiniapp.getMiniappId());
                taMiniappTemplateMapper.insert(tpl);
            }
        }

        WxMaService appService = wxUtils.getMiniApp(taMiniapp.getMiniappId());
        if (null == appService || (null != orign && !taMiniapp.getSecret().equals(orign.getSecret()))) {
            wxUtils.newService(taMiniapp, true);
        }

        QueryWrapper<TaOrgAccount> taOrgAccountQueryWrapper = new QueryWrapper<>();
        taOrgAccountQueryWrapper.eq("org_id", taMiniapp.getOrgId());
        Integer count = taOrgAccountMapper.selectCount(taOrgAccountQueryWrapper);
        if (count < 1){
            createOrgAccount(taMiniapp.getOrgId(), taMiniapp.getName());
        }else {
            TaOrgAccount taOrgAccount = new TaOrgAccount();
            taOrgAccount.setMiniappName(taMiniapp.getName());
            taOrgAccountMapper.update(taOrgAccount, taOrgAccountQueryWrapper);
        }

        return taMiniapp;
    }

    @Override
    public TaMiniapp getInfoWithTpls(String appid) {
        TaMiniapp taMiniapp = taMiniappMapper.selectById(appid);

        QueryWrapper<TaMiniappTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("miniapp_id", appid);
        queryWrapper.eq("is_subscribe", 1);
        taMiniapp.setTpls(taMiniappTemplateMapper.selectList(queryWrapper));

        return taMiniapp;
    }

    public void createOrgAccount(Integer orgId, String miniAppName) throws Exception {
        TaOrgAccount taOrgAccount = new TaOrgAccount();
        taOrgAccount.setOrgId(orgId);
        taOrgAccount.setMiniappName(miniAppName);
        if (taOrgAccountMapper.insert(taOrgAccount) < 1){
            throw new Exception("插入系统账户失败");
        }
    }
}
