package com.yida.data.system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.common.ModuleName;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.Dict;
import com.yida.data.common.core.entity.system.EduArea;
import com.yida.data.common.core.entity.system.EduDeptWxPublic;
import com.yida.data.common.core.entity.system.EduSysFile;
import com.yida.data.common.core.entity.system.EduWxPublicTemplateMsg;
import com.yida.data.common.core.entity.system.enums.OperationLogTypeEnum;
import com.yida.data.common.core.exception.FebsException;
import com.yida.data.log.annotation.OperationLog;
import com.yida.data.system.mapper.EduDeptWxPublicMapper;
import com.yida.data.system.mapper.EduWxPublicTemplateMsgMapper;
import com.yida.data.system.service.CommonService;
import com.yida.data.system.service.DictService;
import com.yida.data.system.service.EduAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.elasticsearch.search.fetch.subphase.FetchDocValuesContext.FieldAndFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Api(tags = "通用接口（不鉴权）")
@RestController
@RequiredArgsConstructor
@RequestMapping("/in/common")
public class InCommonController {

    private final CommonService commonService;
    private final DictService dictService;
    private final EduAreaService eduAreaService;
    private final EduDeptWxPublicMapper eduDeptWxPublicMapper;
    private final EduWxPublicTemplateMsgMapper eduWxPublicTemplateMsgMapper;

    @ApiOperation("公共上传图片接口（接收base64）")
    @PostMapping("/uploadImg")
    @OperationLog(module = ModuleName.SYSTEM, methods = "", type = OperationLogTypeEnum.INSERT)
    public ResultBean<String> uploadImg(String code) {
        return ResultBean.buildSuccess(commonService.uploadImg(code));
    }


    @ApiOperation("公共上传接口")
    @PostMapping("/upload")
    @OperationLog(module = ModuleName.SYSTEM, methods = "上传文件")
    public ResultBean upload(MultipartFile file,
        @ApiParam("限制大小(单位：MB)") @RequestParam(required = false) Integer maxSize) {
        if (file == null) {
            return ResultBean.buildSuccess();
        }
        if (maxSize != null) {
            if (maxSize * 1024 * 1024 < file.getSize()) {
                throw new FebsException(String.format("超过最大限制大小%dMB", maxSize));
            }
        }
        EduSysFile eduSysFile = commonService.upload(file);
        StringBuilder stringBuilder = new StringBuilder(eduSysFile.getUrl());
        if (StrUtil.isNotBlank(eduSysFile.getCoverUrl())) {
            stringBuilder.append(",")
                .append(eduSysFile.getCoverUrl());
        }
        return ResultBean.buildSuccess(stringBuilder.toString());
    }

    @ApiOperation("查询部门绑定的公众号信息")
    @GetMapping("/getDeptWxPublic")
    public ResultBean<EduDeptWxPublic> getDeptWxPublic(Long deptId) {
        return ResultBean.buildSuccess(eduDeptWxPublicMapper.selectOne(Wrappers.<EduDeptWxPublic>lambdaQuery()
            .eq(EduDeptWxPublic::getDeptId, deptId)));
    }

    @ApiOperation("刷入系统接收第三方消息")
    @RequestMapping("/saveReceiveMsg")
    public void saveReceiveMsg() {
        commonService.saveReceiveMsg();
    }

    @ApiOperation("查询公众号模板消息")
    @GetMapping("/getWxTemplateMsg")
    public ResultBean<EduWxPublicTemplateMsg> getWxTemplateMsg(String wxPublicAppId, Integer templateMsgType) {
        return ResultBean.buildSuccess(eduWxPublicTemplateMsgMapper.selectOne(
            Wrappers.<EduWxPublicTemplateMsg>lambdaQuery().eq(EduWxPublicTemplateMsg::getWxPublicAppId, wxPublicAppId)
                .eq(EduWxPublicTemplateMsg::getTemplateMsgType, templateMsgType)));
    }

    @ApiOperation("查询字典值")
    @GetMapping("/getDict")
    public ResultBean<List<Dict>> getDict(String type) {
        return ResultBean.buildSuccess(dictService.list(Wrappers.<Dict>lambdaQuery().eq(Dict::getType, type)));
    }

    @ApiOperation("查询区域")
    @GetMapping("/getArea")
    public ResultBean<EduArea> getArea(@ApiParam("区域id") Long id) {
        return ResultBean.buildSuccess(eduAreaService.getInfo(id));
    }
}
