package thyyxxk.webserver.service.outpatient.triage;

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.dao.his.outpatient.triage.TriageSettingsDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.outpatient.triage.FloorScreenSetting;
import thyyxxk.webserver.entity.outpatient.triage.MzfzZdDeptRoom;
import thyyxxk.webserver.entity.outpatient.triage.RoomIp;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;
import thyyxxk.webserver.utils.TokenUtil;

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

/**
 * @author dj
 */
@Service
public class TriageSettingsService {
    private final TriageSettingsDao dao;
    @Setter
    @Getter
    private static List<CodeName> reqArr;

    @Autowired
    public TriageSettingsService(TriageSettingsDao dao) {
        this.dao = dao;
    }

    public ResultVo<List<CodeName>> getAllMzDept() {
        return ResultVoUtil.success(dao.getAllMzDept());
    }

    public ResultVo<String[]> getChosenDept() {
        return ResultVoUtil.success(StringUtil.triageDeptString2Array(dao.getChosenDept(TokenUtil.getInstance().getTokenUserId())));
    }

    public ResultVo<String> saveChosenDept(List<String> depts) {
        String userid = TokenUtil.getInstance().getTokenUserId();
        if (null == depts || depts.isEmpty()) {
            dao.saveChosenDept(userid, null);
        } else {
            StringBuilder dept = new StringBuilder("[");
            for (String item : depts) {
                dept.append("\"").append(item).append("\",");
            }
            dept.replace(dept.length() - 1 , dept.length(), "]");
            int saved = dao.saveChosenDept(TokenUtil.getInstance().getTokenUserId(), dept.toString());
            if (saved == 0) {
                CodeName codeName = dao.selectEmployeeCodeRsName(userid);
                dao.insertChosenDept(userid, codeName.getCode(), codeName.getName(), dept.toString());
            }
        }
        return ResultVoUtil.success("保存成功。");
    }

    public ResultVo<List<MzfzZdDeptRoom>> getChosenRoom() {
        String[] deptCodes = StringUtil.triageDeptString2Array(dao.getChosenDept(TokenUtil.getInstance().getTokenUserId()));
        if (deptCodes.length == 0) {
            return ResultVoUtil.success(new ArrayList<>());
        }
        return ResultVoUtil.success(dao.getChosenRoom(deptCodes));
    }

    public ResultVo<List<CodeName>> searchDoctor(String content) {
        content = content.toUpperCase() + "%";
        return ResultVoUtil.success(dao.searchDoctor(content));
    }

    public ResultVo<List<CodeName>> getReqs() {
        List<CodeName> reqs = getReqArr();
        if (reqs == null || reqs.isEmpty()) {
            setReqArr(reqs = dao.getReqs());
        }
        return ResultVoUtil.success(reqs);
    }

    public ResultVo<String> updateRoomSettings(MzfzZdDeptRoom room) {
        dao.updateRoomSettings(room);
        return ResultVoUtil.success("已成功更新诊室信息。");
    }

    public ResultVo<List<RoomIp>> getAllRoomIp() {
        return ResultVoUtil.success(dao.getAllRoomIp());
    }

    public ResultVo<String> updateRoomIp(RoomIp param) {
        dao.updateRoomIp(param);
        return ResultVoUtil.success("更新ip地址成功。");
    }

    public FloorScreenSetting getFloorScreenSetting(String userCode) {
        FloorScreenSetting result = dao.getFloorScreenSetting(userCode);
        if (null == result) {
            result = dao.getFloorScreenSetting("system");
        } else {
            result.setRollingMessage(dao.getRollingMessage());
        }
        result.setScrollTimer(result.getScrollTimer() * 1000);
        result.setCallHighlight(result.getCallHighlight() * 1000);
        return result;
    }
}
