package cn.hnthyy.thmz.service.impl.thmz;

import cn.hnthyy.thmz.entity.MzException;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.entity.thmz.WorkspaceConfig;
import cn.hnthyy.thmz.mapper.thmz.WorkspaceConfigMapper;
import cn.hnthyy.thmz.service.thmz.WorkspaceConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class WorkspaceConfigServiceImpl implements WorkspaceConfigService {
    @SuppressWarnings("all")
    @Autowired
    private WorkspaceConfigMapper workspaceConfigMapper;

    @Override
    public int save(WorkspaceConfig workspaceConfig, User tokenUser) throws MzException {
        if (tokenUser == null || StringUtils.isBlank(tokenUser.getUserIdCode())) {
            throw new MzException("医生编码为空！");
        }
        if (tokenUser == null || tokenUser.getId() == null) {
            throw new MzException("医生主键为空！");
        }
        WorkspaceConfig dbWorkspaceConfig = workspaceConfigMapper.selectWorkspaceConfigByUserIdCode(tokenUser.getUserIdCode());
        if (dbWorkspaceConfig == null) {
            workspaceConfig.setUserIdCode(tokenUser.getUserIdCode());
            workspaceConfig.setCreateDate(new Date());
            workspaceConfig.setCreateId(tokenUser.getId());
            return workspaceConfigMapper.inset(workspaceConfig);
        }
        workspaceConfig.setUpdateDate(new Date());
        workspaceConfig.setUpdateId(tokenUser.getId());
        workspaceConfig.setId(dbWorkspaceConfig.getId());
        return workspaceConfigMapper.updateConfigByKey(workspaceConfig);
    }

    @Override
    public WorkspaceConfig queryWorkspaceConfigById(Long id) {
        return workspaceConfigMapper.selectWorkspaceConfigById(id);
    }

    @Override
    public WorkspaceConfig queryWorkspaceConfigByUserIdCode(String userIdCode) {
        return workspaceConfigMapper.selectWorkspaceConfigByUserIdCode(userIdCode);
    }

}
