package com.boman.gen.controller;

import com.boman.common.core.web.controller.BaseController;
import com.boman.common.core.web.domain.AjaxResult;
import com.boman.common.redis.RedisKey;
import com.boman.common.redis.service.RedisService;
import com.boman.gen.domain.GenTable;
import com.boman.gen.domain.GenTableColumn;
import com.boman.gen.domain.GenTableRelation;
import com.boman.gen.service.IGenTableColumnService;
import com.boman.gen.service.IGenTableRelationService;
import com.boman.gen.service.IGenTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static com.boman.common.core.utils.obj.ObjectUtils.requireNonNull;
import static com.boman.common.core.utils.obj.ObjectUtils.requiredNonNull;

/**
 * @author shiqian
 * @description 获取gen_table
 * @date 2021年03月17日 16:44
 **/
@RestController
@RequestMapping("/my")
public class MyController extends BaseController {

    @Autowired
    private IGenTableService genTableService;
    @Autowired
    private IGenTableColumnService genTableColumnService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private IGenTableRelationService genTableRelationService;

    /**
     * 功能描述: 查询代码生成列表，table中封装columnList
     *
     * @param genTable 查询条件
     * @return AjaxResult
     */
    @GetMapping("/loadTable")
    public AjaxResult loadTable(GenTable genTable) {
        List<GenTable> tableList = genTableService.selectGenTableList(genTable);
        requiredNonNull(tableList);

        // load table and tableColumn
        List<Long> tableIdList = tableList.stream().map(GenTable::getTableId).collect(Collectors.toList());
        List<GenTableColumn> genTableColumns = genTableColumnService.listByTableIdList(tableIdList);
        requiredNonNull(genTableColumns);
        packTableAndInsertToRedis(tableList, genTableColumns);

        // load tableRelation
        List<GenTableRelation> relationList = genTableRelationService.selectGenTableRelationList(new GenTableRelation());
        requiredNonNull(relationList);
        packRelationAndInsertToRedis(tableList, relationList);

        return AjaxResult.success(tableList);
    }

    /**
     * 功能描述: 把主表关联的附表的信息和附表的字段信息存进redis
     *
     * @param tableList    tableList
     * @param relationList 主表和附表的关联信息
     */
    private void packRelationAndInsertToRedis(List<GenTable> tableList, List<GenTableRelation> relationList) {
        for (GenTable table : tableList) {
            List<GenTable> tableListTemp = new ArrayList<>(16);
            for (GenTableRelation relation : relationList) {
                if (relation.getRelationParentId().equals(table.getTableId())) {
                    Long childId = relation.getRelationChildId();
                    // 存了附表的建表信息和表的所有字段信息
                    List<GenTable> collect = tableList.stream()
                            .filter(genTable -> genTable.getTableId().equals(childId))
                            .collect(Collectors.toList());
                    tableListTemp.add(requireNonNull(collect).get(0));
                }
            }
            table.setRelationList(tableListTemp);
            redisService.setCacheObject(RedisKey.RELATION + table.getTableName(), table, 12L, TimeUnit.DAYS);
        }
    }

    /**
     * 功能描述: 把table中塞入对应的column， 同时把所有的信息塞到redis中
     *
     * @param tableList       tableList
     * @param genTableColumns 所有的列
     */
    private void packTableAndInsertToRedis(List<GenTable> tableList, List<GenTableColumn> genTableColumns) {
        for (GenTable table : tableList) {
            List<GenTableColumn> columnList = new ArrayList<>(16);
            for (GenTableColumn tableColumn : genTableColumns) {
                if (table.getTableId().equals(tableColumn.getTableId())) {
                    columnList.add(tableColumn);
                }
            }

            table.setColumns(columnList);
            redisService.setCacheObject(RedisKey.TABLE_INFO + table.getTableName(), table, 12L, TimeUnit.DAYS);
        }
    }


    /**手动更新redis缓存表数据
    * @Description
    * @author tjf
    * @Date 2021/3/29
    */
    public AjaxResult updateRedisTable(){
        //获取所有的表和表字段,更新到redis中
        List<GenTable> tableList = genTableService.selectGenTableList(new GenTable());
        if (null == tableList || tableList.isEmpty()) {
            return AjaxResult.error("当前没有可更新数据");
        }
        List<Long> tableIdList = tableList.stream().map(GenTable::getTableId).collect(Collectors.toList());
        List<GenTableColumn> genTableColumns = genTableColumnService.listByTableIdList(tableIdList);
        if (null == genTableColumns || genTableColumns.isEmpty()) {
            return AjaxResult.error("当前没有可更新数据");
        }
        packTableAndInsertToRedis(tableList, genTableColumns);
        return AjaxResult.success();
    }
}
