package io.renren.modules.levelManage.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.levelManage.entity.LevelEntity;
import io.renren.modules.levelManage.entity.LevelTableEntity;
import io.renren.modules.levelManage.service.LevelService;
import io.renren.modules.levelManage.service.LevelTableService;
import io.renren.modules.sys.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-03-30 16:57:16
 */
@RestController
@RequestMapping("/leveltable")
public class LevelTableController extends AbstractController {
    @Autowired
    private LevelTableService levelTableService;
    @Autowired
    private LevelService levelService;
    @Autowired
    private LevelController levelController;


    @RequestMapping("/createTable")
    public R createTable(@RequestBody Map<String,Object> map)
    {
        String tableName= (String) map.get("tableName");
        if (tableName==null)
            return R.error().put("msg","未输入表格名称");
        List<Map<String,String>> colName= ( List<Map<String,String>>) map.get("col");
        Long levelId = Long.valueOf(map.get("id").toString()) ;
        Long userId = getUserId();

        //无效数据的清洗
        List<Map<String,String>> trueCols=new ArrayList<>();
        for(Map<String,String> item :colName)
        {
            String type = item.get("type");
            String comment = item.get("remark");
            String name = item.get("name");
            if(comment==null)
                comment=name;
            if(name==null && type==null)
                continue;
            if(type==null || name==null)
                return R.error().put("msg","表格属性输入错误");
            HashMap<String,String> temp = new HashMap<>();
            temp.put("name",name);
            temp.put("type",trans(type));
            temp.put("comment",comment);
            trueCols.add(temp);
        }
        //表格的建立，以及对应关系的插入
        if(levelTableService.createTable(tableName,trueCols)==0)
        {
            for(Map<String,String> item : trueCols)
            {
                LevelTableEntity levelTable=new LevelTableEntity();
                levelTable.setLevelId(levelId);
                levelTable.setCreateDate(new Date());
                levelTable.setCreateUser(userId);
                levelTable.setName(item.get("name"));
                levelTable.setType(item.get("type"));
                levelTable.setRemark(item.get("comment"));
                if(!levelTableService.save(levelTable))
                {
                    return R.error().put("msg","表格创建成功，关联关系记录失败");
                }
            }
            LevelEntity levelEntity=levelService.getById(levelId);
            levelEntity.setCreateTable(1);
            levelEntity.setTableName(tableName);

            levelController.update(levelEntity);

            return R.ok();
        }
        return R.error().put("msg","创建表格失败");
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = levelTableService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/listByLevelId")
    public R list(@RequestParam("levelId") Long id)
    {
        List<Map<String,Object>> data=levelTableService.listByLevelId(id);
        LevelEntity level=levelService.getById(id);
        String tablename=level.getTableName();
        return R.ok().put("data",data).put("tableName",tablename);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		LevelTableEntity levelTable = levelTableService.getById(id);

        return R.ok().put("levelTable", levelTable);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody LevelTableEntity levelTable){
		levelTableService.save(levelTable);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody LevelTableEntity levelTable){
		levelTableService.updateById(levelTable);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		levelTableService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    private String trans(String type)
    {
        if(type.equals("varchar"))
            type="varchar(255)";
        return type;
    }
}
