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

import com.yihu.wlyy.entity.consult.Evaluate;
import com.yihu.wlyy.entity.consult.EvaluateLabel;
import com.yihu.wlyy.service.app.consult.ConsultTeamService;
import com.yihu.wlyy.service.app.consult.EvaluateLabelService;
import com.yihu.wlyy.service.app.consult.EvaluateService;
import com.yihu.wlyy.web.BaseController;
import io.swagger.annotations.Api;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by 卓 on 2017/5/10.
 */
@Controller
@RequestMapping(value = "/patient/consult/evaluate", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "医生评价")
public class EvaluateController  extends BaseController {
    @Autowired
    ConsultTeamService consultTeamService;
    @Autowired
    EvaluateService evaluateService;
    @Autowired
    EvaluateLabelService evaluateLabelService;
    /**
     * 保存评价
     * @param evaluateJson 保存评价内容的JSON
     * @return
     */
    @RequestMapping(value = "save")
    @ResponseBody
    public String save(@RequestParam(required = true,value="evaluate") String evaluateJson){
        try{
            evaluateService.saveByJson(evaluateJson,getRepUID());
//            evaluateService.saveByJson(evaluateJson,getUID());
            return  write(200,"保存成功！");
        }catch (Exception e){
            error(e);
            return write(-1,e.getMessage());
        }
    }

    /**
     * 更新平均
     * @param evaluateJson
     * @return
     */
    @RequestMapping(value = "updete")
    @ResponseBody
    public String updete(@RequestParam(required = true,value="evaluate") String evaluateJson){
        try{
            evaluateService.updateByJson(evaluateJson,getRepUID());
            return  write(200,"保存成功！");
        }catch (Exception e){
            error(e);
            return write(-1,e.getMessage());
        }
    }

    /**
     * 获取评价
     * @param consult 咨询
     * @return
     */
    @RequestMapping(value = "list")
    @ResponseBody
    public String list(@RequestParam(required = true,value="consult") String consult,@RequestParam(required = false,value="type") Integer type){
        try{
            List<Evaluate> evaluates = null;
            List<EvaluateLabel> evaluateLabels = new ArrayList<>();
            if(type==null){
                evaluates  =  evaluateService.getEvaluateByConsult(consult);
                evaluateLabels  =  evaluateLabelService.findByConsult(consult);
                JSONArray evaluatesJson = new JSONArray(evaluates);
                JSONArray evaluateLabelsJson = new JSONArray(evaluateLabels);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("evaluate",evaluatesJson);
                jsonObject.put("label",evaluateLabelsJson);
                return  write(200,"查询成功！","data",jsonObject);
            }else{
                evaluates = evaluateService.getEvaluateByConsultAndType(consult,type);
                return  write(200,"查询成功！","list",evaluates);
            }
        }catch (Exception e){
            error(e);
            return write(-1,e.getMessage());
        }
    }



    /**
     * 保存评价
     * @param doctor 保存评价内容的JSON
     * @return
     */
    @RequestMapping(value = "doctor/label")
    @ResponseBody
    public String doctorLabel(@RequestParam(required = true,value="doctor") String doctor){
        try{
            List<Map<String,Object>> result = evaluateLabelService.findDistinctLabelByLaDoctor(doctor);
            return  write(200,"查询成功！","list",result);
        }catch (Exception e){
            error(e);
            return write(-1,e.getMessage());
        }
    }


}
