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

import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonAnyFormatVisitor;
import com.yihu.wlyy.service.app.family.FamilyMemberService;
import com.yihu.wlyy.web.BaseController;
import io.swagger.annotations.Api;
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;

/**
 * 家庭关系成员控制器
 * <p>
 * Created by lyr-pc on 2016/10/18.
 */
@RestController
@RequestMapping(value = "/patient/family")
@Api(description = "家庭成员")
public class FamilyMemberController extends BaseController {

    @Autowired
    FamilyMemberService familyMemberService;

    /**
     * 添加成员
     *
     * @param member   成员code
     * @param captcha  验证码
     * @param relation 关系
     * @return
     */
    @RequestMapping(value = "/member_add", method = RequestMethod.POST)
    @ApiOperation(value = "添加成员")
    public String addFamilyMember(@RequestParam("member") String member,
                                  @RequestParam(value = "captcha", required = false) String captcha,
                                  @RequestParam(value = "password", required = false) String password,
                                  @RequestParam("relation") int relation) {
        try {
            if (StringUtils.isEmpty(member)) {
                return error(-1, "添加成员不能为空");
            }
            if (StringUtils.isEmpty(captcha) && StringUtils.isEmpty(password)) {
                return error(-1, "验证码或密码不能为空");
            }
            if (relation < 0 || relation > 6) {
                return error(-1, "家庭关系无效");
            }

            int result = familyMemberService.addMember(getUID(), member, captcha, password, relation);

            if (result == -1) {
                return error(-1, "居民信息查询失败");
            } else if (result == -2) {
                return error(-1, "该成员未注册");
            } else if (result == -3) {
                return error(-1, StringUtils.isNotEmpty(captcha) ? "请输入正确的验证码" : "密码错误");
            } else if (result == -4) {
                return error(-1, "该家庭成员已存在");
            } else if (result == -5) {
                return error(-1, "该家庭关系已存在成员");
            } else if (result == -6) {
                return error(-1, "验证码已过期，请重新获取验证码");
            } else if (result == 0) {
                return error(-1, "不能添加自己");
            } else {
                return write(200, "添加成功");
            }
        } catch (Exception e) {
            return error(-1, "添加失败");
        }
    }

    /**
     * 更新家庭关系
     *
     * @param member   成员code
     * @param relation 家庭关系
     * @return
     */
    @RequestMapping(value = "/relation_update", method = RequestMethod.POST)
    @ApiOperation(value = "家庭成员关系修改")
    public String modifyFamilyRelation(@RequestParam("member") String member, @RequestParam("relation") int relation) {
        try {
            if (StringUtils.isEmpty(member)) {
                return error(-1, "添加成员不能为空");
            }
            if (relation < 0 || relation > 6) {
                return error(-1, "家庭关系无效");
            }

            int result = familyMemberService.modifyFamilyRelation(getUID(), member, relation);

            if (result == -1) {
                return error(-1, "居民信息查询失败");
            } else if (result == -2) {
                return error(-1, "该成员未注册");
            } else if (result == -3) {
                return error(-1, "与该成员的关系不存在");
            } else {
                return write(200, "更新成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "更新失败");
        }
    }

    /**
     * 删除成员
     *
     * @param member
     * @return
     */
    @RequestMapping(value = "/member_delete", method = RequestMethod.POST)
    @ApiOperation(value = "家庭成员删除")
    public String deleteMember(@RequestParam("member") String member) {
        try {
            if (StringUtils.isEmpty(member)) {
                return error(-1, "成员不能为空");
            }

            int result = familyMemberService.deleteMember(getUID(), member);

            if (result == -1) {
                return error(-1, "与该成员的关系不存在");
            } else {
                return write(200, "删除成功");
            }
        } catch (Exception e) {
            return error(-1, "删除失败");
        }
    }

    /**
     * 家庭成员查询
     *
     * @return
     */
    @RequestMapping(value = "/members", method = RequestMethod.GET)
    @ApiOperation(value = "家庭成员查询")
    public String getPatientFamilyMembers(@RequestParam(required = false) String patient) {
        try {
            JSONArray result = new JSONArray();
            if (StringUtils.isNotEmpty(patient)) {
                result = familyMemberService.getPatientMembers(patient, "", true, getUID());
            } else {
                result = familyMemberService.getPatientFamilyMembers(getUID(), "");
            }
            return write(200, "查询成功", "data", result);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "查询失败");
        }
    }

    /**
     * 根据身份证号查询居民是否注册
     *
     * @param idcard 身份证号
     * @return
     */
    @RequestMapping(value = "/is_register", method = RequestMethod.GET)
    @ApiOperation(value = "家庭成员查询")
    public String isRegister(@RequestParam("idcard") String idcard) {
        try {
            if (StringUtils.isEmpty(idcard)) {
                return error(-1, "身份证号不能为空");
            }

            JSONObject result = familyMemberService.isRegister(idcard, getUID());

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