package com.ruoyi.web.work.api;

import com.google.code.kaptcha.Producer;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.web.work.api.config.BaseController;
import com.ruoyi.web.work.domain.Introduction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/app/common")
public class Api_CommonController extends BaseController {

    @Autowired
    private RedisCache redisCache;

    @Resource(name = "captchaProducerMath")
    private Producer captchaProducerMath;

    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data)) {
            data = new ArrayList<>();
        }
        return AjaxResult.success(data);
    }

    @GetMapping(value = "/introduction/{title}")
    public AjaxResult introduction(@PathVariable String title) {
        Introduction introduction = redisCache.getCacheObject(CacheConstants.APP_INFO);
        if (title.equals("用户协议")) {
            return AjaxResult.success(introduction.getAgreement());
        }
        if (title.equals("隐私政策")) {
            return AjaxResult.success(introduction.getPrivacy());
        }
        if (title.equals("医院介绍")) {
            return AjaxResult.success(introduction);
        }
        return AjaxResult.error();
    }
}

