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

import com.yihu.wlyy.entity.patient.PatientDisease;
import com.yihu.wlyy.service.app.disease.PatientDiseaseService;
import com.yihu.wlyy.web.BaseController;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jce.provider.symmetric.ARC4;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
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.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 患者疾病控制器
 *
 * Created by lyr on 2016/09/09.
 */
@RestController
@RequestMapping(value = "/doctor/patient", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "患者疾病")
public class PatientDiseaseController extends BaseController {

    @Autowired
    PatientDiseaseService patientDiseaseService;
    @Autowired
    StringRedisTemplate redisTemplate;

    /**
     * 查询患者的疾病
     *
     * @param patient 患者
     * @return
     */
    @RequestMapping(value = "/disease")
    public String getPatientDisease(String patient) {
        try {
            if (StringUtils.isEmpty(patient)) {
                return error(-1, "患者不能为空");
            }
            if (StringUtils.isEmpty(redisTemplate.opsForValue().get("disease:" + patient))) {
                List<PatientDisease> diseases = patientDiseaseService.getPatientDisease(patient);
                return write(200, "查询成功", "data", new JSONArray(diseases));
            } else {
                return write(200, "查询成功", "data", new JSONArray(redisTemplate.opsForValue().get("disease:" + patient)));
            }
        } catch (Exception e) {
            return error(-1, "查询失败");
        }
    }

    /**
     * 查询患者的三师疾病
     *
     * @param patient 患者
     * @return
     */
    @RequestMapping(value = "/disease_sanshi")
    public String getPatientSsDisease(String patient) {
        try {
            if (StringUtils.isEmpty(patient)) {
                return error(-1, "患者不能为空");
            }

            List<PatientDisease> diseases = patientDiseaseService.getPatientSsDisease(patient);
            return write(200, "查询成功", "data", new JSONArray(diseases));
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "查询失败");
        }
    }

    /**
     * 更新患者疾病
     *
     * @param patient 患者标识
     * @param disease 疾病
     * @return
     */
    @RequestMapping(value = "/disease_update")
    public String updatePatientDisease(String patient, String disease) {
        try {
            List<PatientDisease> diseases = patientDiseaseService.getPatientDiseaseSs(patient);

            if (diseases != null && diseases.size() > 0) {
                List<String> dis = Arrays.asList(disease.split(","));

                if (dis != null && dis.size() > 0) {
                    int flag = 0;
                    for(PatientDisease dise : diseases){
                        if(dis.contains(dise.getDel())){
                            flag++;
                        }
                    }
                    if(flag != diseases.size()){
                        return error(-1, "三师签约疾病不能更改");
                    }
                } else {
                    return error(-1, "三师签约疾病不能更改");
                }
            }

            if (patientDiseaseService.updatePatientDisease(patient, disease)) {
                return write(200, "更新成功");
            } else {
                return error(-1, "更新失败");
            }
        } catch (Exception e) {
            return error(-1, "更新失败");
        }
    }
}
