package com.weaver.seconddev.chapanda.esign.controller;

import com.weaver.common.authority.annotation.WeaPermission;
import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.seconddev.chapanda.esign.util.CommonUtils;
import com.weaver.seconddev.chapanda.esign.service.SealTemplateService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/secondev/cbd/eseal")
public class SealTemplateController {

    private final Logger log = LoggerFactory.getLogger(SealTemplateController.class);

    @Autowired
    SealTemplateService sealTemplateService;

    @Autowired
    CommonUtils CommonUtils;

    /***
     * 电子签名模板预览接口
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @WeaPermission(publicPermission = true)
    @GetMapping("/queryPreviewUrl")
    public WeaResult<Object> queryPreviewUrl(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, Object> dataMap = new HashMap<String,Object>();
        String templateId = request.getParameter("templateId");
        Map<String,Object> paraMap = CommonUtils.requestToMap(request);

        if(StringUtils.isNotBlank(templateId)){
            dataMap = sealTemplateService.queryPreviewUrl(templateId,paraMap);
        }else{
            return WeaResult.fail(500,"模板ID为空");
        }
        return WeaResult.success(dataMap);
    }


    @WeaPermission(publicPermission = true)
    @GetMapping("/queryTemplateField")
    public WeaResult<Object> queryTemplateField(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();
        String templateId = request.getParameter("templateId");
        Map<String,Object> paraMap = CommonUtils.requestToMap(request);

        if(StringUtils.isNotBlank(templateId)){
            dataList = sealTemplateService.queryTemplateField(templateId,paraMap);
        }else{
            return WeaResult.fail(500,"模板ID为空");
        }
        return WeaResult.success(dataList);
    }

    /**
     * 测试接口
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @WeaPermission(publicPermission = true)
    @GetMapping("/test")
    public WeaResult<Object> test(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, Object> recordMap = new HashMap<String,Object>();
        recordMap.put("test","test");
        return WeaResult.success(recordMap);
    }


}
