package com.yiqi.api.controller.system;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.R;
import com.yiqi.system.domain.*;
import com.yiqi.system.domain.dto.DataGroupFiled;
import com.yiqi.system.domain.dto.DataGroupRules;
import com.yiqi.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "基础通用接口")
@RestController
@RequestMapping(value = UrlConstants.wxappApi + "/common")
public class WeAppCommonController extends BaseController {

    @Autowired
    private ISysConfigRecordService sysConfigRecordService;

    @Autowired
    private ISysConfigTabService sysConfigTabService;

    @Autowired
    private ISysDataGroupService sysDataGroupService;

    @Autowired
    private ISysDataGroupInfoService sysDataGroupInfoService;


    @ApiOperation("通过参数获取参数配置，多个参数用英文逗号隔开")
    @GetMapping(value = "getConfigValueByKey")
    public R getConfigValueByKey(@RequestParam String configKeys) {
        String[] keys = configKeys.split(";");
        List<SysConfigRecord> sysConfigRecordList = sysConfigRecordService.getConfigRecordByKey(keys);
        Map<String, Object> map = new HashMap<>();
        for (SysConfigRecord record : sysConfigRecordList) {
            map.put(record.getMenuName(), record.getValue());
        }
        return R.ok(map);
    }

    @ApiOperation("通过分组关键词获取参数配置")
    @GetMapping(value = "getConfigValueByTabKey")
    public R getConfigValueByTabKey(@RequestParam String tabKey) {
        SysConfigTab sysConfigTab = sysConfigTabService.selectSysConfigTabByTabKey(tabKey);
        if (sysConfigTab == null) {
            return R.fail("分组不存在");
        }
        List<SysConfigRecord> sysConfigRecordList = sysConfigRecordService.list(new QueryWrapper<SysConfigRecord>().eq("config_tab_id", sysConfigTab.getId()).eq("status", 1).orderByDesc("sort"));
        List<Map> list = new ArrayList<>();
        for (SysConfigRecord record : sysConfigRecordList) {
            Map<String, Object> map = new HashMap<>();
            map.put("key", record.getMenuName());
            map.put("value", record.getValue());
            list.add(map);
        }
        return R.ok(list);
    }

    @ApiOperation("通过分组关键词获取组合列表数据")
    @GetMapping(value = "getGroupDataByGroupKey")
    public R getGroupDataByGroupKey(@RequestParam String groupKey) {
        SysDataGroup sysDataGroup = sysDataGroupService.selectSysDataGroupByKey(groupKey);
        if (sysDataGroup == null) {
            return R.fail("分组不存在");
        }
        List<SysDataGroupInfo> list = sysDataGroupInfoService.getDataList(sysDataGroup.getId(), 1);
        List<DataGroupFiled> dataGroupFileds = JSONUtil.toList(JSONUtil.parseArray(sysDataGroup.getFields()), DataGroupFiled.class);
        List<Map<String, Object>> objectList = new ArrayList<>();
        for (SysDataGroupInfo sysDataGroupInfo : list) {
            JSONObject jsonObject = JSONUtil.parseObj(sysDataGroupInfo.getValue());
            Map<String, Object> map = new HashMap<>();
            for (DataGroupFiled dataGroupFiled : dataGroupFileds) {
                if (jsonObject.get(dataGroupFiled.getTitle()) != null) {
                    if (dataGroupFiled.getType().equals("input") || dataGroupFiled.getType().equals("textarea")) {
                        map.put(dataGroupFiled.getTitle(), jsonObject.get(dataGroupFiled.getTitle()));
                    } else if (dataGroupFiled.getType().equals("radio") || dataGroupFiled.getType().equals("select")) {
                        map.put(dataGroupFiled.getTitle(), DataGroupRules.getSigleOptionValue(jsonObject.get(dataGroupFiled.getTitle()).toString(), dataGroupFiled.getParam()));
                    } else if (dataGroupFiled.getType().equals("checkbox")) {
                        map.put(dataGroupFiled.getTitle(), DataGroupRules.getMuliteOptionValue(jsonObject.get(dataGroupFiled.getTitle()).toString(), dataGroupFiled.getParam()));
                    } else if (dataGroupFiled.getType().equals("upload")) {
                        map.put(dataGroupFiled.getTitle(), jsonObject.get(dataGroupFiled.getTitle()));
                    }
                }
            }
            map.put("sort", sysDataGroupInfo.getSort());
            map.put("id", sysDataGroupInfo.getId());
            map.put("status", sysDataGroupInfo.getStatus());
            map.put("createTime", sysDataGroupInfo.getCreateTime());
            objectList.add(map);
        }
        return R.ok(objectList);
    }
}
