package com.yihu.wlyy.web.doctor.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 = "/doctor/consult/evaluate", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "医生评价")
public class DoctorEvaluateController extends BaseController {
    @Autowired
    ConsultTeamService consultTeamService;
    @Autowired
    EvaluateService evaluateService;
    @Autowired
    EvaluateLabelService evaluateLabelService;
    /**
     * 获取评价
     * @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());
        }
    }

    /**
     * 获取去重的标签
     * @return
     */
    @RequestMapping(value = "label")
    @ResponseBody
    public String label(){
        try{
            List<Map<String,Object>> result = evaluateLabelService.findDistinctLabelByLaDoctor(getUID());
            return write(200,"查询成功！","list",result);
        }catch (Exception e){
            error(e);
            return write(-1,e.getMessage());
        }
    }

    /**
     * 获取医生评价列表
     * @param type
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "getDoctorSorceList")
    @ResponseBody
    public String getDoctorSorceList(@RequestParam(required = true)String type,
                                     @RequestParam(required = false)Integer page,
                                     @RequestParam(required = false)Integer size){
        try{
            JSONArray rs =evaluateService.getDoctorSorceList(getUID(),type,page,size);
            //JSONArray rs =evaluateService.getDoctorSorceList("zbqD201703150226",type,page,size);

            return  write(200,"查询成功！","data",rs);
        }catch (Exception e){
            error(e);
            return write(-1,e.getMessage());
        }
    }

    /**
     * 获取医生各个类型评价数
     * @return
     */
    @RequestMapping(value = "getScoreListTitle")
    @ResponseBody
    public String  getScoreListTitle(){
        try{
            JSONArray rs =evaluateService.getScoreListTitle(getUID());
            //JSONArray rs =evaluateService.getScoreListTitle("zbqD201703150226");

            return  write(200,"查询成功！","data",rs);
        }catch (Exception e){
            error(e);
            return write(-1,e.getMessage());
        }
    }

    /**
     * 获取医生各个评分头部信息
     * @return
     */
    @RequestMapping(value = "getScoreTitle")
    @ResponseBody
    public String getScoreTitle(){
        try{
            JSONArray rs =evaluateService.getScoreTitle(getUID());
            //JSONArray rs =evaluateService.getScoreTitle("zbqD201703150226");

            return  write(200,"查询成功！","data",rs);
        }catch (Exception e){
            error(e);
            return write(-1,e.getMessage());
        }
    }

}
