
package com.lideeyunji.core.framework.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lideeyunji.core.framework.entity.LogModuleUsageRecordsEntity;
import com.lideeyunji.core.framework.mapper.LideeYunJiMapper;
import com.lideeyunji.core.framework.mapper.LogModuleUsageRecordsMapper;
import com.lideeyunji.core.framework.service.IApiLogService;
import com.lideeyunji.core.framework.service.ILideeYunJiService;
import com.lideeyunji.core.framework.utils.Func;
import com.lideeyunji.tool.framework.yunji.utils.lideeYunJiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *  低代码平台
 */
@Service
public class LideeYunJiServiceImpl implements ILideeYunJiService {

	@Autowired
	private LideeYunJiMapper lideeYunJiMapper;

    @Autowired
    private LogModuleUsageRecordsMapper logModuleUsageRecordsMapper;


    @Autowired
    private IApiLogService iApiLogService;

	//清除ExcelData日志
	@Override
	public Integer clearExcelData(Date clearDate){
		return lideeYunJiMapper.clearExcelData(clearDate);
	}

    // 保存模块使用记录
    @Override
    public Integer saveModelUsageRecords() {
        Date now = DateUtil.date();
        Date begin = DateUtil.beginOfDay(now);
        LocalDate today = LocalDate.now();
        LocalDateTime dateTimeNow = LocalDateTime.now();
        // 从日志表统计模块使用记录
        List<Map<String, Object>> records = iApiLogService.countModelUsageRecords(begin, now);
        // 写入模块使用统计表
        for (Map<String, Object> record : records) {
            String modelTitle = lideeYunJiUtils.getMap2Str(record, "modeltitle");
            Long usersNum = lideeYunJiUtils.getMap2Long(record, "usersnum");
            Long useNum = lideeYunJiUtils.getMap2Long(record, "usenum");
            Long id = getLogModuleUsageRecordsId(modelTitle, today);
            LogModuleUsageRecordsEntity moduleEntity = new LogModuleUsageRecordsEntity();
            moduleEntity.setUsersNum(usersNum);
            moduleEntity.setUseNum(useNum);
            if (Func.isEmpty(id)) {
                moduleEntity.setTenantId(1L);
                moduleEntity.setCreateUser(1L);
                moduleEntity.setCreateTime(dateTimeNow);
                moduleEntity.setCreateDept(101L);
                moduleEntity.setIsDeleted(0);
                moduleEntity.setRecordDate(today);
                moduleEntity.setModelTitle(modelTitle);
            } else {
                moduleEntity.setId(id);
                moduleEntity.setUpdateUser(1L);
                moduleEntity.setUpdateTime(dateTimeNow);
            }
            saveLogModuleUsageRecords(moduleEntity);
        }
        return records.size();
    }

    /**
     * 按模块标题和记录日期获取id
     */
    private Long getLogModuleUsageRecordsId(String modelTitle, LocalDate recordDate) {
        LambdaQueryWrapper<LogModuleUsageRecordsEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LogModuleUsageRecordsEntity::getModelTitle, modelTitle);
        wrapper.eq(LogModuleUsageRecordsEntity::getRecordDate, recordDate);
        wrapper.select(LogModuleUsageRecordsEntity::getId);
        LogModuleUsageRecordsEntity one = logModuleUsageRecordsMapper.selectOne(wrapper);
        if (Func.isEmpty(one) || Func.isEmpty(one.getId())) {
            return null;
        }
        return one.getId();
    }

    /**
     * 写入模块使用统计表
     */
    private void saveLogModuleUsageRecords(LogModuleUsageRecordsEntity entity) {
        if (Func.isEmpty(entity.getId())) {
            logModuleUsageRecordsMapper.insert(entity);
        } else {
            logModuleUsageRecordsMapper.updateById(entity);
        }
    }

}
