package com.sptg.gateway.controller;

import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.sptg.common.core.util.JsonUtils;
import com.sptg.estate.api.dto.UserFeedbackDto;
import com.sptg.estate.api.entity.Member;
import com.sptg.estate.api.entity.UserFeedback;
import com.sptg.estate.api.feigns.MemberFeign;
import com.sptg.estate.api.feigns.UserFeedbackFeign;
import com.sptg.gateway.util.DataCheck;
import com.sptg.permissions.api.feigns.ApiDockingConfFeign;
import com.sptg.permissions.api.feigns.OrgFeign;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/app/userFeedback")
public class UserFeedbackController {
    private final OrgFeign orgFeign;
    private final ApiDockingConfFeign apiDockingConfFeign;
    private final UserFeedbackFeign userFeedbackFeign;
    private final FastFileStorageClient fastFileStorageClient;
    private final MemberFeign memberFeign;

    /**
     * 新增反馈
     * @param map{
     *           type           :   反馈类型
     *           question       :   问题描述
     *           image_base64   :   截图或图片
     *           member_id      :   反馈用户
     *           org_id         :   机构授权org_id，机构id
     *           token          :   对接令牌
     *           }
     * @return hashMap
     */
    @PostMapping(value = "/addFeedback")
    public Map<String,Object> addFeedback(@RequestBody Map<String,Object> map){
        Map<String,Object> hashMap = new HashMap<>();
        //result_code:0-失败  1-成功
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "反馈失败");
        String org_id = (String)map.get("org_id");
        String token = (String)map.get("token");
        //反馈类型 1：功能异常 2：产品建议 3：安全问题 4：商品终端异常 5：其他问题
        String type = (String)map.get("type");
        String question = (String)map.get("question");
        Object image_base64 = map.get("image_base64");
        String member_id = map.get("member_id").toString();
        try {
            if (DataCheck.checkOrgId(hashMap, org_id, orgFeign)) {
                return hashMap;
            }
            if(DataCheck.checkToken(hashMap, org_id, token,apiDockingConfFeign)){
                return hashMap;
            }
            if (StringUtils.isBlank(type)){
                hashMap.put("result_msg","请选择反馈类型");
                return hashMap;
            }
            Member member = this.memberFeign.getMemberByMemberId(Long.valueOf(member_id));
            if (member == null){
                hashMap.put("result_msg","反馈用户不存在");
                return hashMap;
            }
            UserFeedbackDto userFeedbackDto = new UserFeedbackDto();
            userFeedbackDto.setType(Integer.parseInt(type));
            userFeedbackDto.setQuestion(question);
            userFeedbackDto.setMemberId(member.getId());
            userFeedbackDto.setMemberName(member.getName());
            userFeedbackDto.setOrgId(Long.valueOf(org_id));
            log.warn("-----------" + JsonUtils.objectToJson(userFeedbackDto) + "---------------");
            String image_base64_json = JsonUtils.objectToJson(image_base64);
            if (StringUtils.isNotBlank(image_base64_json)){
                List<String> image_base64List = JsonUtils.jsonToList(image_base64_json, String.class);
//                String[] image_base64List = image_base64.split(",");
                if(image_base64List != null){
                    for (String imgBase64 : image_base64List){
                        DataCheck.uploadImage(imgBase64,userFeedbackDto,this.fastFileStorageClient);
                    }
                }
            }
            if (userFeedbackFeign.add(userFeedbackDto).getData()){
                hashMap.put("result_code",1);
                hashMap.put("result_msg", "反馈成功");
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }
}
