package com.yihu.wlyy.web.patient.feedback;

import com.yihu.wlyy.service.app.feedback.AppealService;
import com.yihu.wlyy.service.app.feedback.FeedbackService;
import com.yihu.wlyy.util.CommonUtil;
import com.yihu.wlyy.web.WeixinBaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Reece on 2017/5/6.
 */
@Controller
@RequestMapping(value = "/patient/feedback")
@Api(description = "居民端意见反馈与账号申诉")
public class PatientFeedbackController extends WeixinBaseController {
    @Autowired
    private FeedbackService feedbackService;
    @Autowired
    private AppealService appealService;

    /**
     * 居民端保存意见反馈
     *
     * @param description 问题描述
     * @param type        选择类型
     * @param images      图片，多图逗号分隔
     * @param contact     联系方式
     * @return
     */
    @RequestMapping(value = "/saveFeedback", method = RequestMethod.POST)
    @ApiOperation(value = "居民端保存反馈")
    @ResponseBody
    public String saveFeedback(
            @RequestParam String description,
            @RequestParam int type,
            @RequestParam(required = false) String images,
            @RequestParam(required = false) String contact) {
        try {
//        图片上传
            String photos="";
            if (StringUtils.isNotEmpty(images)) {
                String[] mediaIds = images.split(",");
                for (String mediaId : mediaIds) {
                    if (StringUtils.isEmpty(mediaId)) {
                        continue;
                    }
                    String temp = saveImageToDisk(mediaId);
                    if (StringUtils.isNotEmpty(temp)) {
                        if (photos.length() == 0) {
                            photos = temp;
                        } else {
                            photos += "," + temp;
                        }
                    }
                }
            }
            if (StringUtils.isNotEmpty(photos)) {
                photos = CommonUtil.copyTempImage(photos);
            }
//        保存到数据库
            feedbackService.saveFeedback(getUID(),1,description,type,photos,contact);
            return write(200, "保存成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "保存失败!");
        }
    }

    /**
     * 居民端保存账号申诉
     *
     * @param description 问题描述
     * @param type        选择类型
     * @param images      图片，多图逗号分隔
     * @param phone       手机号码
     * @return
     */
    @RequestMapping(value = "/saveAppeal", method = RequestMethod.POST)
    @ApiOperation(value = "居民端保存申诉")
    @ResponseBody
    public String saveAppeal(
            @RequestParam String description,
            @RequestParam int type,
            @RequestParam(required = false) String images,
            @RequestParam String phone) {
        try {
//        图片上传
            String photos="";
            if (StringUtils.isNotEmpty(images)) {
                String[] mediaIds = images.split(",");
                for (String mediaId : mediaIds) {
                    if (StringUtils.isEmpty(mediaId)) {
                        continue;
                    }
                    String temp = saveImageToDisk(mediaId);
                    if (StringUtils.isNotEmpty(temp)) {
                        if (photos.length() == 0) {
                            photos = temp;
                        } else {
                            photos += "," + temp;
                        }
                    }
                }
            }
            if (StringUtils.isNotEmpty(photos)) {
                photos = CommonUtil.copyTempImage(photos);
            }
//             保存到数据库
            appealService.saveAppeal(getUID(),description,type,photos,phone,1);
            return write(200, "保存成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "保存失败!");
        }
    }
    /**
     * 获取微信服务器图片
     *
     * @return
     */
    public String fetchWxImages() {
        String photos = "";
        try {
            String images = request.getParameter("mediaIds");
            if (StringUtils.isEmpty(images)) {
                return photos;
            }
            String[] mediaIds = images.split(",");
            for (String mediaId : mediaIds) {
                if (StringUtils.isEmpty(mediaId)) {
                    continue;
                }
                String temp = saveImageToDisk(mediaId);
                if (StringUtils.isNotEmpty(temp)) {
                    if (photos.length() == 0) {
                        photos = temp;
                    } else {
                        photos += "," + temp;
                    }
                }
            }
        } catch (Exception e) {
            error(e);
        }
        return photos;
    }



}
