package com.education.core.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.education.business.service.education.GradeInfoService;
import com.education.business.service.education.SubjectInfoService;
import com.education.business.service.system.SystemDictValueService;
import com.education.common.base.BaseController;
import com.education.common.config.OssProperties;
import com.education.common.model.PageInfo;
import com.education.common.utils.Result;
import com.education.common.utils.StringToTxt;
import com.education.model.dto.SubjectInfoDto;
import com.education.model.entity.GradeInfo;
import com.education.model.entity.SubjectInfo;
import com.education.model.entity.SystemDictValue;
import com.education.model.request.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 字典值管理接口
 * @author zengjintao
 * @version 1.0
 * @create_at 2020/11/16 10:26
 */
@RestController
@RequestMapping("/api/dictValue")
public class DictValueController extends BaseController {

    @Autowired
    private SystemDictValueService systemDictValueService;

    @Resource
    private GradeInfoService gradeInfoService;

    @Resource
    private SubjectInfoService subjectInfoService;

    @Resource
    private OssProperties ossProperties;

    /**
     * 字典类型值列表
     * @param dictId
     * @return
     */
    @GetMapping("selectByDictId")
   // @Cacheable(cacheNames = CacheKey.SYSTEM_DICT_VALUE, key = "#dictId + ':'+ #pageParam.pageNumber + ':'+ #pageParam.pageSize")
    public Result<PageInfo<SystemDictValue>> selectByDictId(PageParam pageParam, Integer dictId) {
        LambdaQueryWrapper<SystemDictValue> queryWrapper = Wrappers.<SystemDictValue>lambdaQuery()
                .eq(SystemDictValue::getSystemDictId, dictId);
        return Result.success(systemDictValueService.selectPage(pageParam, queryWrapper));
    }

    /**
     * 根据类型获取字典值列表
     * @param dictType
     * @return
     */
    @GetMapping("selectByDictType")
    //@Cacheable(cacheNames = CacheKey.SYSTEM_DICT_VALUE, key = "#dictType")
    public Result selectByDictType(String dictType) {
        return Result.success(systemDictValueService.selectByDictType(dictType));
    }

    @GetMapping("selectByDictTypeAll")
    //@Cacheable(cacheNames = CacheKey.SYSTEM_DICT_VALUE, key = "#dictType")
    public Result selectByDictTypeAll(String dictType) {
        List<SystemDictValue> systemDictValueList = systemDictValueService.selectByDictType(dictType);
        LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(GradeInfo.class);
        PageInfo<GradeInfo> gradeInfoPage = gradeInfoService.selectPage(new PageParam(), queryWrapper);
        SystemDictValue systemDictValue = null;
        for (GradeInfo gradeInfo : gradeInfoPage.getDataList()) {
            systemDictValue = new SystemDictValue();
            systemDictValue.setValue(gradeInfo.getName());
            systemDictValue.setCode(gradeInfo.getId());
            systemDictValue.setId(gradeInfo.getId()+100);
            systemDictValueList.add(systemDictValue);
        }
        PageInfo<SubjectInfoDto> SubjectInfoDtoPage = subjectInfoService.selectPageList(new PageParam(), new SubjectInfo());
        for (SubjectInfoDto subjectInfoDto : SubjectInfoDtoPage.getDataList()) {
            systemDictValue = new SystemDictValue();
            systemDictValue.setValue(subjectInfoDto.getName());
            systemDictValue.setCode(subjectInfoDto.getId());
            systemDictValue.setId(subjectInfoDto.getId()+1000);
            systemDictValueList.add(systemDictValue);
        }
        return Result.success(systemDictValueList);
    }


    @GetMapping("generateByDictTypeAll")
    //@Cacheable(cacheNames = CacheKey.SYSTEM_DICT_VALUE, key = "#dictType")
    public Result generateByDictTypeAll(String dictType) {
        List<SystemDictValue> systemDictValueList = systemDictValueService.selectByDictType("school_type");
        String content = "这是要写入文件的字符串内容。";
        String title = "现有一份教学系统的数据字典，具体信息如下：\n";
        String template = "\n" +
                "${name} ，属性名为${type}\n" +
                "属性值有${value}，对应的ID是${id}";
        Map<String, String> replacements = new HashMap<>();
        // 所属阶段 schoolType
        String systemDictIdStr = systemDictValueList.stream()
                .map(SystemDictValue::getCode)
                .map(String::valueOf)
                .collect(Collectors.joining("、"));
        String systemDictNameIdStr = systemDictValueList.stream()
                .map(SystemDictValue::getValue)
                .map(String::valueOf)
                .collect(Collectors.joining("、"));
        replacements.put("name","所属阶段");
        replacements.put("type","schoolType");
        replacements.put("value",systemDictNameIdStr);
        replacements.put("id",systemDictIdStr);

        content = StringToTxt.replacePlaceholders(template,replacements) +"\n";
        // 所属年级 gradeInfoId
        LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(GradeInfo.class);
        PageInfo<GradeInfo> gradeInfoPage = gradeInfoService.selectPage(new PageParam(), queryWrapper);
        String gradeInfoIdStr = gradeInfoPage.getDataList().stream()
                .map(GradeInfo::getId)
                .map(String::valueOf)
                .collect(Collectors.joining("、"));
        String gradeNameIdStr = gradeInfoPage.getDataList().stream()
                .map(GradeInfo::getName)
                .map(String::valueOf)
                .collect(Collectors.joining("、"));
        replacements.put("name","所属年级");
        replacements.put("type","gradeInfoId");
        replacements.put("value",gradeNameIdStr);
        replacements.put("id",gradeInfoIdStr);

        content += StringToTxt.replacePlaceholders(template,replacements) +"\n";
        // 所属科目 subjectId
        PageInfo<SubjectInfoDto> SubjectInfoDtoPage = subjectInfoService.selectPageList(new PageParam(), new SubjectInfo());
        String subjectInfoIdStr = SubjectInfoDtoPage.getDataList().stream()
                .map(SubjectInfoDto::getId)
                .map(String::valueOf)
                .collect(Collectors.joining("、"));
        String subjectInfoNameIdStr = SubjectInfoDtoPage.getDataList().stream()
                .map(SubjectInfoDto::getName)
                .map(String::valueOf)
                .collect(Collectors.joining("、"));
        replacements.put("name","所属科目");
        replacements.put("type","subjectId");
        replacements.put("value",subjectInfoNameIdStr);
        replacements.put("id",subjectInfoIdStr);

        content += StringToTxt.replacePlaceholders(template,replacements) +"\n";

        replacements.put("name","试题类型");
        replacements.put("type","questionType");
        replacements.put("value","单选题、多选题、填空题、综合题、不定项题、判断题、计算题");
        replacements.put("id","1、2、3、4、5、6、7");

        content += StringToTxt.replacePlaceholders(template,replacements) +"\n";
        StringToTxt.writeStringToFile(title + content, ossProperties.getBucketName()+"output.txt");
        return Result.success(systemDictValueList);
    }


    /**
     * 添加或修改字典类型值
     * @param systemDictValue
     * @return
     */
    @PostMapping
   // @CacheEvict(cacheNames = CacheKey.SYSTEM_DICT_VALUE, allEntries = true)
    public Result saveOrUpdate(@RequestBody SystemDictValue systemDictValue) {
        systemDictValueService.saveOrUpdate(systemDictValue);
        return Result.success();
    }

    /**
     * 删除字典类型值
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
  //  @CacheEvict(cacheNames = CacheKey.SYSTEM_DICT_VALUE, allEntries = true)
    public Result deleteById(@PathVariable Integer id) {
        systemDictValueService.removeById(id);
        return Result.success();
    }
}
