package com.yihu.wlyy.service.app.label;

import com.yihu.wlyy.entity.doctor.team.sign.SignPatientLabel;
import com.yihu.wlyy.entity.doctor.team.sign.SignPatientLabelInfo;
import com.yihu.wlyy.entity.doctor.team.sign.SignPatientLabelLog;
import com.yihu.wlyy.entity.patient.Patient;
import com.yihu.wlyy.repository.doctor.SignPatientLabelDao;
import com.yihu.wlyy.repository.doctor.SignPatientLabelInfoDao;
import com.yihu.wlyy.repository.doctor.SignPatientLabelLogDao;
import com.yihu.wlyy.repository.patient.PatientDao;
import com.yihu.wlyy.service.BaseService;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 标签服务类
 * <p>
 * Created by lyr on 2016/10/9.
 */
@Service
@Transactional
public class SignPatientLabelService extends BaseService {

    @Autowired
    SignPatientLabelDao labelDao;
    @Autowired
    SignPatientLabelLogDao labelLogDao;
    @Autowired
    SignPatientLabelInfoDao labelInfoDao;

    /**
     * 新增自定义标签
     *
     * @param lableName 标签名称
     * @param creator   创建人
     * @param teamCode  所属团队
     * @return
     */
    public SignPatientLabel addLabel(String lableName, String creator, long teamCode) {
        SignPatientLabel label = new SignPatientLabel();

        label.setLabelCode(getCode());
        label.setLabelName(lableName);
        label.setLabelType("4");
        label.setIsSystem(0);
        label.setCreator(creator);
        label.setTeamCode(teamCode);
        label.setStatus(1);
        label.setCzrq(new Date());

        return labelDao.save(label);
    }

    /**
     * 新增自定义标签
     *
     * @param lableName 标签名称
     * @param creator   创建人
     * @param teamCode  所属团队
     * @return
     */
    public List<SignPatientLabel> addTeamsLabel(String lableName, String creator, String teamCode) throws Exception {
        String[] teams = teamCode.split(",");
        List<SignPatientLabel> result = new ArrayList<>();

        if (teams.length > 0) {
            for (String team : teams) {
                SignPatientLabel lbEx = isLabelNameExist(lableName, Long.valueOf(team));

                if (lbEx != null) {
                    throw new Exception("团队下该标签名称已经存在");
                }

                SignPatientLabel label = new SignPatientLabel();

                label.setLabelCode(getCode());
                label.setLabelName(lableName);
                label.setLabelType("4");
                label.setIsSystem(0);
                label.setCreator(creator);
                label.setTeamCode(Long.valueOf(team));
                label.setStatus(1);
                label.setCzrq(new Date());

                result.add(labelDao.save(label));
            }
        }

        return result;
    }

    /**
     * 查询某个标签名称是否存在
     *
     * @param labelName
     * @param teamCode
     * @return
     */
    public SignPatientLabel isLabelNameExist(String labelName, long teamCode) {
        return labelDao.findByLabelNameAndLabelTypeAndTeamCode(labelName, "4", teamCode);
    }

    /**
     * 修改某个自定义标签
     *
     * @param labelCode 标签code
     * @param newName   新名称
     * @return
     */
    public int modifyLabel(String labelCode, String newName, String modifyBy) {
        SignPatientLabel label = labelDao.findByLabelCodeAndLabelTypeAndStatus(labelCode, "4", 1);

        if (label == null) {
            // 标签不存在
            return -1;
        }
        if (label.getIsSystem() == 1) {
            // 系统标签不能修改
            return -2;
        }

        SignPatientLabelLog log = new SignPatientLabelLog();

        log.setLabelCode(labelCode);
        log.setOldName(label.getLabelName());
        log.setNewName(newName);
        log.setModifyType(1);
        log.setModifyBy(modifyBy);
        log.setCzrq(new Date());

        label.setLabelName(newName);
        label.setCzrq(new Date());

        labelLogDao.save(log);
        labelInfoDao.updateLabelName(newName, labelCode);

        return 1;
    }

    /**
     * 删除某个自定义标签
     *
     * @param labelCode 标签code
     * @return
     */
    public int deleteLabel(String labelCode, String modifyBy) {
        SignPatientLabel label = labelDao.findByLabelCodeAndLabelTypeAndStatus(labelCode, "4", 1);

        if (label == null) {
            // 标签不存在
            return -1;
        }
        if (label.getIsSystem() == 1) {
            // 系统标签不能删除
            return -2;
        }

        labelInfoDao.deleteByLabel(labelCode);

        SignPatientLabelLog log = new SignPatientLabelLog();

        log.setLabelCode(labelCode);
        log.setModifyType(2);
        log.setModifyBy(modifyBy);
        log.setCzrq(new Date());

        label.setStatus(0);
        label.setCzrq(new Date());

        labelLogDao.save(log);

        return 1;
    }

    /**
     * 查询某个团队的标签
     *
     * @param teamCode 团队code
     * @return
     */
    public List<SignPatientLabel> getTeamAllLabels(long teamCode) {
        List<SignPatientLabel> labels = new ArrayList<>();

        // 非自定义系统标签
        List<SignPatientLabel> systemLabels = labelDao.findSystemLabels();

        if (systemLabels.size() > 0) {
            labels.addAll(systemLabels);
        }

        // 自定义标签
        List<SignPatientLabel> customLabels = labelDao.findCustomLabels(teamCode);

        if (customLabels.size() > 0) {
            labels.addAll(customLabels);
        }

        return labels;
    }

    /**
     * 查询某个团队的自定义标签
     *
     * @param teamCode 团队code
     * @return
     */
    public List<SignPatientLabel> getLabelsByTypeAndTeam(String labelType, String teamCode) {
        List<SignPatientLabel> labels = new ArrayList<>();

        if (labelType.equals("4")) {
            String[] teams = teamCode.split(",");

            for (String team : teams) {
                // 自定义标签
                labels.addAll(labelDao.findCustomLabels(Long.valueOf(team)));
            }
        } else {
            labels = labelDao.findByLabelTypeAndStatus(labelType, 1);
        }

        return labels;
    }

    /**
     * 查询某个标签
     *
     * @param labelCode 标签code
     * @param labelType 标签类型
     * @return
     */
    public SignPatientLabel getLabelByCodeAndType(String labelCode, String labelType) {
        return labelDao.findByLabelCodeAndLabelTypeAndStatus(labelCode, labelType, 1);
    }

    /**
     * 标签排序
     *
     * @param labels 标签集合
     * @return
     */
    public int customLabelSort(JSONArray labels) {

        for (int i = 0; i < labels.length(); i++) {
            JSONObject json = labels.getJSONObject(i);
            String labelCode = "";
            int sort = 0;

            try {
                labelCode = json.getString("labelCode");
                sort = json.getInt("sort");
            } catch (Exception e) {
                e.printStackTrace();
                return -1;
            }

            if (StringUtils.isEmpty(labelCode)) {
                return -2;
            }

            if (labelCode.equals("1") || labelCode.equals("2")) {
                return -3; //系统标签不能修改
            }

            SignPatientLabel label = labelDao.findByLabelCodeAndLabelTypeAndStatus(labelCode, "4", 1);

            if (label == null) {
                return -4;
            }

            label.setSort(sort);
            labelDao.save(label);
        }

        return 1;
    }
}
