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

import java.util.Date;

//import io.swagger.annotations.Api;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
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;

import com.yihu.wlyy.entity.consult.ConsultCommunication;
import com.yihu.wlyy.entity.consult.ConsultCommunicationReply;
import com.yihu.wlyy.service.app.consult.ConsultCommunicationService;
import com.yihu.wlyy.task.PushMsgTask;
import com.yihu.wlyy.util.CommonUtil;
import com.yihu.wlyy.util.DateUtil;
import com.yihu.wlyy.util.MessageType;
import com.yihu.wlyy.util.MyStringUtil;
import com.yihu.wlyy.web.WeixinBaseController;

@Controller
@RequestMapping(value = "/patient/consult/communication", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "患者端-病友圈")
public class ConsultCommunicationController extends WeixinBaseController {

	@Autowired
	private ConsultCommunicationService consultCommunicationService;

	/**
	 * 添加病友圈帖子
	 * @param question 问题
	 * @param content 问题描述
	 * @param images 图片URL，多图以逗号分隔
	 * @param disease 疾病类型标识
	 * @param diseaseName 疾病
	 * @return
	 */
	@RequestMapping(value = "add")
	@ResponseBody
	public String add(String question, String content, @RequestParam(required = false) String images, @RequestParam(required = false) String disease, @RequestParam(required = false) String diseaseName) {
		try {
			ConsultCommunication cp = new ConsultCommunication();
			// 回复数默认为0
			cp.setAmount(0);
			// 设置问题描述
			cp.setContent(content);
			// 设置图片URL
			if (StringUtils.isEmpty(images)) {
				images = fetchWxImages();
			}
			if (StringUtils.isNotEmpty(images)) {
				images = CommonUtil.copyTempImage(images);
			}
			cp.setImages(images);
			// 设置患者标识
			cp.setPatient(getUID());
			// 设置问题标题
			cp.setTitle(question);
			// 设置疾病类型
			cp.setDisease(disease);
			// 设置疾病类型名称
			cp.setDiseaseName(diseaseName);
			// 未读数量默认为0
			cp.setUnread(0);
			// 保存到数据库
			ConsultCommunication temp = consultCommunicationService.addConsult(cp);
			if (temp == null) {
				return error(-1, "保存失败！");
			}
			return success("保存成功！");
		} catch (Exception e) {
			error(e);
			return error(-1, "保存失败！");
		}
	}

	/**
	 * 查询病友圈帖子列表
	 * @param type 类型：0全部，1我的提问，2我的回答
	 * @param disease 疾病类型标识
	 * @param pagesize 分页大小
	 * @return
	 */
	@RequestMapping(value = "list")
	@ResponseBody
	public String list(int type, @RequestParam(required = false) String disease, long id, int pagesize) {
		try {
			JSONArray list = new JSONArray();
			Page<ConsultCommunication> datas = consultCommunicationService.findConsultByType(getUID(), type, disease, id, pagesize);
			if (datas != null) {
				for (ConsultCommunication cp : datas) {
					if (cp == null) {
						continue;
					}
					JSONObject json = new JSONObject();
					json.put("id", cp.getId());
					// 设置问题标识
					json.put("consult", cp.getConsult());
					// 设置患者姓名
					json.put("name", MyStringUtil.userNameReplaceWithStar(cp.getName()));
					// 设置患者头像
					json.put("photo", cp.getPhoto());
					// 设置问题标题
					json.put("title", cp.getTitle());
					// 设置问题描述
					json.put("content", cp.getContent());
					// 设置是否包含图片
					json.put("hasImg", StringUtils.isNoneEmpty(cp.getImages()));
					// 设置回复总数
					json.put("amount", cp.getAmount());
					// 设置疾病标识
					json.put("disease", cp.getDisease());
					// 设置疾病名称
					json.put("diseaseName", cp.getDiseaseName());
					// 咨询时间
					json.put("czrq",cp.getCzrq());
					list.put(json);
				}
			}
			return write(200, "查询成功！", "list", datas, list);
		} catch (Exception e) {
			error(e);
			return error(-1, "查询失败！");
		}
	}

	/**
	 * 回复病友圈
	 * @param consult 咨询标识
	 * @param content 回复内容
	 * @param images 回复图片，多图以逗号分隔
	 * @return
	 */
	@RequestMapping(value = "reply")
	@ResponseBody
	public String reply(String consult, String content, @RequestParam(required = false) String images) {
		try {
			ConsultCommunicationReply reply = new ConsultCommunicationReply();
			reply.setConsult(consult);
			reply.setContent(content);
			reply.setCzrq(new Date());
			reply.setDel("1");
			// 设置图片URL
			if (StringUtils.isEmpty(images)) {
				images = fetchWxImages();
			}
			if (StringUtils.isNotEmpty(images)) {
				images = CommonUtil.copyTempImage(images);
			}
			reply.setImages(images);
			reply.setPatient(getUID());
			String patient = consultCommunicationService.addReply(reply);
			if (!StringUtils.equals(patient, getUID())) {
				// 推送消息给患者
				PushMsgTask.getInstance().put(patient, MessageType.MESSAGE_TYPE_PATIENT_CONSULT_COMMUNICATION_REPLY.P_CC_01.name(), MessageType.MESSAGE_TYPE_PATIENT_CONSULT_COMMUNICATION_REPLY.病友圈.name(), MessageType.MESSAGE_TYPE_PATIENT_CONSULT_COMMUNICATION_REPLY.您有新的回复内容.name(), consult);
			}
			return success("回复成功！");
		} catch (Exception e) {
			error(e);
			return error(-1, "回复失败！");
		}
	}

	/**
	 * 查询回复列表
	 * @param consult 咨询标识
	 * @param pagesize 分页大小
	 * @return
	 */
	@RequestMapping(value = "reply_list")
	@ResponseBody
	public String replyList(String consult, long id, int pagesize) {
		try {
			JSONArray list = new JSONArray();
			Page<ConsultCommunicationReply> datas = consultCommunicationService.findReplyListByConsult(consult, id, pagesize);
			if (datas != null) {
				for (ConsultCommunicationReply cpr : datas) {
					if (cpr == null) {
						continue;
					}
					JSONObject json = new JSONObject();
					// 设置回复标识
					json.put("id", cpr.getId());
					// 设置回复者姓名
					json.put("name",  MyStringUtil.userNameReplaceWithStar(cpr.getName()));
					// 设置回复者头像
					json.put("photo", cpr.getPhoto());
					// 设置回复内容
					json.put("content", cpr.getContent());
					// 设置回复图片
					json.put("images", cpr.getImages());
					// 设置回复时间
					json.put("czrq", DateUtil.dateToStrLong(cpr.getCzrq()));
					list.put(json);
				}
			}
			if (id <= 0) {
				// 查询咨询信息
				ConsultCommunication temp = consultCommunicationService.findByConsult(consult);
				if (StringUtils.equals(temp.getPatient(), getUID())) {
					// 自己的帖子，清空未读数
					// 未读数+1
					consultCommunicationService.clearUnread(consult);
				}
			}
			return write(200, "查询成功！", "list", datas, list);
		} catch (Exception e) {
			error(e);
			return error(-1, "查询失败！");
		}
	}

	/**
	 * 查询公共咨询详细信息
	 * @param consult
	 * @return
	 */
	@RequestMapping(value = "info")
	@ResponseBody
	public String info(String consult) {
		try {
			JSONObject json = new JSONObject();
			ConsultCommunication cp = consultCommunicationService.findByConsult(consult);
			if (cp != null) {
				// 设置咨询标识
				json.put("consult", cp.getConsult());
				// 设置姓名
				json.put("name", MyStringUtil.userNameReplaceWithStar(cp.getName()));
				// 设置头像
				json.put("photo", cp.getPhoto());
				// 设置问题
				json.put("question", cp.getTitle());
				// 设置问题描述
				json.put("content", cp.getContent());
				// 设置回复图片
				json.put("images", cp.getImages());
				// 设置回复时间
				json.put("czrq", DateUtil.dateToStrLong(cp.getCzrq()));
				// 设置疾病标识
				json.put("disease", cp.getDisease());
				// 设置疾病名称
				json.put("diseaseName", cp.getDiseaseName());
			}
			return write(200, "查询成功！", "data", json);
		} catch (Exception e) {
			error(e);
			return error(-1, "查询失败！");
		}
	}

}
