package com.wechat.controller;

import java.io.IOException;
import java.math.BigDecimal;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
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 com.alibaba.fastjson.JSON;
import com.wechat.common.Constants;
import com.wechat.common.utils.MvcUtil;
import com.wechat.common.utils.StringsUtils;
import com.wechat.global.base.BaseController;
import com.wechat.global.message.InfoMsg;
import com.wechat.model.dbEntity.TdtgGoodsRejected;
import com.wechat.model.responseDto.JsonResp;
import com.wechat.service.WeiXinService;


/**
 * FAQ问答
 * 
 */
@Controller
@RequestMapping("refund")
public class RefundController extends BaseController {

    //域名
    @Value("#{configProperties['url.base']}")
    private String baseUrl;
    
    //图片根目录
    @Value("#{configProperties['url.img.base']}")
    private String imgUrl;
    
    //退款图片路径
    @Value("#{configProperties['upload.refund.path']}")
    private String refoundUrl;

    
    @Resource
    private WeiXinService wxService;


    /**

     * 获取下载图片信息（jpg）
     * 
     * @param mediaId 文件的id
     * @throws Exception
     */
    @RequestMapping(value = "saveGoodsRejected", method = RequestMethod.POST)
    public  void saveGoodsRejected(@RequestParam String orderNo, @RequestParam String refundReason,@RequestParam BigDecimal refundAmount,@RequestParam String mobile,@RequestParam String imgList, @RequestParam String oldImgList, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	TdtgGoodsRejected rejected = new TdtgGoodsRejected();
    	JsonResp<String> ret = new JsonResp<String>();
    	int imgLength = 0;
    	if(StringsUtils.isEmpty(orderNo))
    	{
         	 ret.setMsg(InfoMsg.ERROR_SAVE_FIELD);
    	}
    	else
    	{
    		//mediaid 获取微信服务器图片
    		if(!StringsUtils.isEmpty(imgList))
    		{
    			String[] imgArray = StringsUtils.split(imgList, ";");
    			imgLength = imgArray.length;
    			for(int i=0; i<imgArray.length; i++ )
        		{
        			String mediaId = imgArray[i];
        			String fileName = "";
        			if(!StringsUtils.isEmpty(mediaId))
    				{
        				//从微信服务器上获取图片至本地
        				fileName = wxService.saveImageToDisk(mediaId,"");
        				if(StringsUtils.isEmpty(fileName))
        				{
        					//系统异常
        					throw new Exception();
        				}
        				switch (i)
        				{
        					case 0 :
    	    					rejected.setVoucherImg1(fileName);
    			    		    break;
        					case 1 :
    	    					rejected.setVoucherImg2(fileName);
    			    		    break;
        					case 2 :
    	    					rejected.setVoucherImg3(fileName);
    			    		    break;
        				}
    				}	
        		}
    		}
    			
			// 原已上传的服务器图片
			if (!StringsUtils.isEmpty(oldImgList)) {
				String[] oldImgArray = StringsUtils.split(oldImgList, ";");
				for (int i = 0; i < oldImgArray.length; i++) {
					String fileName = oldImgArray[i];
					if (StringsUtils.isEmpty(fileName)) {
						// 系统异常
						throw new Exception();
					}
					switch (i+imgLength) {
					case 0:
						rejected.setVoucherImg1(fileName);
						break;
					case 1:
						rejected.setVoucherImg2(fileName);
						break;
					case 2:
						rejected.setVoucherImg3(fileName);
						break;
					}

				}
			}

    		rejected.setOrderNo(orderNo);
    		rejected.setRefundReason(refundReason);
    		rejected.setRefundAmount(refundAmount);
    		rejected.setMobile(mobile);
    		//已申请
    		rejected.setStatus(1);

    	}
    	
        
        String json = JSON.toJSONString(ret, true);
        try {
            MvcUtil.writeJson(request, response, json);
        } catch (IOException e) {
           log.error(e);
        } 
    }

    
}
