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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.gic.commons.util.EntityUtil;
import com.gic.commons.util.UniqueIdUtils;
import com.gic.haoban.manage.api.dto.StaffActiveDataDTO;
import com.gic.haoban.manage.service.dao.mapper.TabHaobanWxEnterpriseActiveDataMapper;
import com.gic.haoban.manage.service.dao.mapper.fee.TabHaobanQywxFeeOrderAccountMapper;
import com.gic.haoban.manage.service.entity.TabHaobanWxEnterpriseActiveData;
import com.gic.haoban.manage.service.service.StaffService;
import com.gic.haoban.manage.service.service.WxEnterpriseActiveDataService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @description:
 * @Author: wenhua
 * @Date: 2023/2/2 10:44
 */
@Service
public class WxEnterpriseActiveDataServiceImpl implements WxEnterpriseActiveDataService {

    private static final Logger log = LogManager.getLogger(WxEnterpriseActiveDataServiceImpl.class);

    @Autowired
    private TabHaobanWxEnterpriseActiveDataMapper tabHaobanWxEnterpriseActiveDataMapper;

    @Autowired
    private TabHaobanQywxFeeOrderAccountMapper tabHaobanQywxFeeOrderAccountMapper;

    @Autowired
    private StaffService staffService;

    @Override
    public Integer saveOrUpdate(String wxEnterpriseId) {
        if (StrUtil.isBlank(wxEnterpriseId))
            return 0;
        List<HashMap<String, Integer>> feeOrderAccountMapList = tabHaobanQywxFeeOrderAccountMapper.countStatusByWxEnterpriseId(wxEnterpriseId);
        log.info("查询订单表返回的map：{}", JSONObject.toJSONString(feeOrderAccountMapList));
        if (CollUtil.isEmpty(feeOrderAccountMapList)) {
            return 0;
        }
        //初始化各状态
        Integer purchased = 0, used = 0, inactive = 0, transfer = 0, activated = 0, expired = 0,merged = 0;
        for (HashMap<String, Integer> map : feeOrderAccountMapList) {
            switch (map.get("status")) {
                case 1:
                    //这里map.get("num")是Long类型，无法直接和Integer做运算
                    purchased += Integer.valueOf(String.valueOf(map.get("num")));
                    inactive += Integer.valueOf(String.valueOf(map.get("num")));
                    break;
                case 2:
                    purchased += Integer.valueOf(String.valueOf(map.get("num")));
                    activated += Integer.valueOf(String.valueOf(map.get("num")));
                    break;
                case 3:
                    purchased += Integer.valueOf(String.valueOf(map.get("num")));
                    expired += Integer.valueOf(String.valueOf(map.get("num")));
                    break;
                case 4:
                    purchased += Integer.valueOf(String.valueOf(map.get("num")));
                    transfer += Integer.valueOf(String.valueOf(map.get("num")));
                    break;
                case 5:
                    //已合并的激活码，总数会减少
                    merged += Integer.valueOf(String.valueOf(map.get("num")));
                    break;
                default:
                    break;
            }
        }
        //已使用为已购买总数-未使用的
        used = purchased - inactive;
        TabHaobanWxEnterpriseActiveData tabHaobanWxEnterpriseActiveData = tabHaobanWxEnterpriseActiveDataMapper.getWxEnterpriseActiveDataByWxEnterpriseId(wxEnterpriseId);
        if (ObjectUtil.isNotNull(tabHaobanWxEnterpriseActiveData)) {
            //更新
            tabHaobanWxEnterpriseActiveData.setActivated(activated);
            tabHaobanWxEnterpriseActiveData.setPurchased(purchased);
            tabHaobanWxEnterpriseActiveData.setInactive(inactive);
            tabHaobanWxEnterpriseActiveData.setUsed(used);
            tabHaobanWxEnterpriseActiveData.setTransfer(transfer);
            tabHaobanWxEnterpriseActiveData.setExpired(expired);
            tabHaobanWxEnterpriseActiveData.setMerged(merged);
            return tabHaobanWxEnterpriseActiveDataMapper.updateActiveDataByWxEnterpriseId(tabHaobanWxEnterpriseActiveData);
        } else {
            //新增
            tabHaobanWxEnterpriseActiveData = new TabHaobanWxEnterpriseActiveData();
            tabHaobanWxEnterpriseActiveData.setId(UniqueIdUtils.uniqueLong());
            tabHaobanWxEnterpriseActiveData.setWxEnterpriseId(wxEnterpriseId);
            tabHaobanWxEnterpriseActiveData.setActivated(activated);
            tabHaobanWxEnterpriseActiveData.setPurchased(purchased);
            tabHaobanWxEnterpriseActiveData.setInactive(inactive);
            tabHaobanWxEnterpriseActiveData.setUsed(used);
            tabHaobanWxEnterpriseActiveData.setExpired(expired);
            tabHaobanWxEnterpriseActiveData.setTransfer(transfer);
            tabHaobanWxEnterpriseActiveData.setMerged(merged);
            return tabHaobanWxEnterpriseActiveDataMapper.insert(tabHaobanWxEnterpriseActiveData);
        }
    }

    @Override
    public StaffActiveDataDTO getWxEnterpriseActiveDataByWxEnterpriseId(String wxEnterpriseId) {
        if (StrUtil.isBlank(wxEnterpriseId))
            return null;
        StaffActiveDataDTO staffActiveDataDTO = EntityUtil.changeEntityByJSON(StaffActiveDataDTO.class, tabHaobanWxEnterpriseActiveDataMapper.getWxEnterpriseActiveDataByWxEnterpriseId(wxEnterpriseId));
        if (ObjectUtil.isNull(staffActiveDataDTO)){
            return null;
        }
        //查询其他占用许可总数
        Integer occupy=0;
        occupy=staffService.getOccupyNum(wxEnterpriseId);
        staffActiveDataDTO.setOccupy(occupy);
        //查询即将过期的许可总数
        Integer expiringNum = 0;
        expiringNum = staffService.getExpiringNum(wxEnterpriseId);
        staffActiveDataDTO.setExpiring(expiringNum);
        return staffActiveDataDTO;
    }

    @Override
    public void updateByStaffActiveDataDTO(StaffActiveDataDTO dto) {
        if (null == dto){
            return;
        }
        TabHaobanWxEnterpriseActiveData tabHaobanWxEnterpriseActiveData = EntityUtil.changeEntityByJSON(TabHaobanWxEnterpriseActiveData.class, dto);
        tabHaobanWxEnterpriseActiveDataMapper.updateActiveDataByWxEnterpriseId(tabHaobanWxEnterpriseActiveData);
    }

    @Override
    public void insert(TabHaobanWxEnterpriseActiveData data) {
        if(null == data){
            log.info("新增许可账号统计参数不能为空！");
            return;
        }
        tabHaobanWxEnterpriseActiveDataMapper.insert(data);
    }
}
