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

import com.yihu.wlyy.aop.ObserverRequired;
import com.yihu.wlyy.entity.doctor.team.sign.SignLabel;
import com.yihu.wlyy.entity.doctor.team.sign.SignPatientLabel;
import com.yihu.wlyy.repository.doctor.SignLabelDao;
import com.yihu.wlyy.service.app.label.SignPatientLabelService;
import com.yihu.wlyy.service.app.team.AdminTeamService;
import com.yihu.wlyy.web.BaseController;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import java.util.List;

/**
 * 标签控制类
 * <p>
 * Created by lyr on 2016/10/9.
 */
@RestController
@RequestMapping(value = "/doctor/patient_label")
public class SignPatientLabelController extends BaseController {

    @Autowired
    SignPatientLabelService labelService;
    @Autowired
    AdminTeamService teamService;
    @Autowired
    SignLabelDao signLabelDao;

    /**
     * 获取大类（pc端im使用）
     * @return
     */
    @RequestMapping(value = "/signLabels", method = RequestMethod.GET)
    @ApiOperation(value = "获取标签大类")
    public String getSignLabels(){
        try {
            List<SignLabel> labels = signLabelDao.findLabels();
            return write(200, "获取成功", "data", labels);
        }catch (Exception e){
            return error(-1, "获取失败");
        }
    }

    /**
     * 添加标签
     *
     * @param labelName 标签名称
     * @param teamCode  所属团队
     * @return
     */
    @RequestMapping(value = "/add")
    @ObserverRequired
    public String addPatientLabel(String labelName, Long teamCode) {
        try {
            if (StringUtils.isEmpty(labelName)) {
                return error(-1, "标签名称不能为空");
            }
            if (teamCode == null || teamCode <= 0) {
                return error(-1, "所属团队不能为空");
            }

            if (labelName.equals("孕产妇") || labelName.equals("儿童")) {
                return error(-1, "该标签名称已存在");
            }

            SignPatientLabel labelEx = labelService.isLabelNameExist(labelName, teamCode);

            if (labelEx != null) {
                return error(-1, "该标签名称已存在");
            }

            SignPatientLabel label = labelService.addLabel(labelName, getUID(), teamCode);

            if (label != null) {
                return write(200, "添加成功", "data", label);
            } else {
                return error(-1, "添加失败");
            }
        } catch (Exception e) {
            return error(-1, "添加失败");
        }
    }

    /**
     * 添加标签
     *
     * @param labelName 标签名称
     * @param teamCode  所属团队
     * @return
     */
    @RequestMapping(value = "/add/teams")
    @ObserverRequired
    public String addTeamsLabel(@RequestParam String labelName, @RequestParam String teamCode) {
        try {
            if (StringUtils.isEmpty(labelName)) {
                return error(-1, "标签名称不能为空");
            }
            if (teamCode == null) {
                return error(-1, "所属团队不能为空");
            }

            if (labelName.equals("孕产妇") || labelName.equals("儿童")) {
                return error(-1, "该标签名称已存在");
            }

            List<SignPatientLabel> labels = labelService.addTeamsLabel(labelName, getUID(), teamCode);

            if (labels.size() > 0) {
                return write(200, "添加成功", "data", labels);
            } else {
                return error(-1, "添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "添加失败");
        }
    }

    /**
     * 更新标签
     *
     * @param labelCode 标签code
     * @param newName   新名称
     * @return
     */
    @RequestMapping(value = "/modify")
    @ObserverRequired
    public String modifyPatientLabel(String labelCode, String newName) {
        try {
            if (StringUtils.isEmpty(labelCode)) {
                return error(-1, "标签code不能为空");
            }
            if (StringUtils.isEmpty(newName)) {
                return error(-1, "标签名称不能为空");
            }

            int result = labelService.modifyLabel(labelCode, newName, getUID());

            if (result == -1) {
                return error(-1, "标签不存在");
            } else if (result == -2) {
                return error(-1, "系统标签不能修改");
            }

            return write(200, "更新成功");
        } catch (Exception e) {
            return error(-1, "更新失败");
        }
    }

    /**
     * 更新标签
     *
     * @param labelCode 标签code
     * @return
     */
    @RequestMapping(value = "/delete")
    @ObserverRequired
    public String deletePatientLabel(String labelCode) {
        try {
            if (StringUtils.isEmpty(labelCode)) {
                return error(-1, "标签code不能为空");
            }

            int result = labelService.deleteLabel(labelCode, getUID());

            if (result == -1) {
                return error(-1, "标签不存在");
            } else if (result == -2) {
                return error(-1, "系统标签不能删除");
            }

            return write(200, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "删除失败");
        }
    }

    /**
     * 获取团队所有标签
     *
     * @param teamCode 团队代码
     * @return
     */
    @RequestMapping(value = "/all_labels")
    public String getTeamAllLabels(Long teamCode) {
        try {
            if (teamCode == null || teamCode <= 0) {
                return error(-1, "团队标识不能为空");
            }

            List<SignPatientLabel> labels = labelService.getTeamAllLabels(teamCode);

            return write(200, "查询成功", "data", labels);
        } catch (Exception e) {
            return error(-1, "查询失败");
        }
    }

    /**
     * 自定义标签排序
     *
     * @param labels 自定义标签排序集合
     * @return
     */
    @RequestMapping(value = "/labels_sort")
    public String labelSort(String labels) {
        try {
            if (StringUtils.isEmpty(labels)) {
                return error(-1, "参数不能为空");
            }

            int result = labelService.customLabelSort(new JSONArray(labels));

            if (result == -1 || result == -2) {
                return error(-1, "参数有误");
            } else if (result == -3) {
                return error(-1, "系统标签不能更改");
            } else if (result == 1) {
                return write(200, "排序更新成功");
            } else {
                return error(-1, "排序更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "排序更新失败");
        }
    }

    /**
     * 获取某个标签类型所有标签
     *
     * @param labelType 标签类型
     * @param teamCode  团队代码
     * @return
     */
    @RequestMapping(value = "/labels_by_type")
    public String getLabelsByTypeAndTeam(@RequestParam String labelType, @RequestParam(required = false) String teamCode) {
        try {
            if (StringUtils.isEmpty(labelType)) {
                return error(-1, "标签不能为空");
            }
            if (labelType.equals("4") && StringUtils.isEmpty(teamCode)) {
                return error(-1, "查询自定义标签团队code不能为空");
            }

            List<SignPatientLabel> labels = labelService.getLabelsByTypeAndTeam(labelType, teamCode);

            return write(200, "查询成功", "data", labels);
        } catch (Exception e) {
            return error(-1, "查询失败");
        }
    }

    @RequestMapping(value = "/patient_teams")
    public String getTeamsLabelsByPatient(@RequestParam String patient) {
        try {
            if (StringUtils.isEmpty(patient)) {
                return error(-1, "居民不能为空");
            }

            JSONArray teams = teamService.findPatientDoctorTeam(patient, getUID());

            if(teams != null && teams.length() > 0){
                for(int i = 0; i < teams.length(); i++){
                    JSONObject team = teams.getJSONObject(i);
                    List<SignPatientLabel> labels = labelService.getLabelsByTypeAndTeam("4", String.valueOf(team.getLong("id")));

                    if(labels != null){
                        team.put("labels",new JSONArray(labels));
                    } else {
                        team.put("labels",new JSONArray());
                    }
                }
            }

            return write(200, "查询成功", "data", teams);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "查询失败");
        }
    }

}
